输入“/”快速插入内容

用LLamaFactory微调CharacterGLM

1月7日修改
本文讨论了CharacterGLM-6b模型的背景信息,并详细介绍了如何使用LLamaFactory对其进行微调,包括环境准备、数据处理、模板设置和训练命令等内容。关键要点包括:
1.
模型背景:CharacterGLM-6b是基于chatglm2-6b微调的角色扮演模型,由清华CoAI、聆心和智谱合作开发。
2.
环境准备:参考LLamaFactory项目仓库(https://github.com/hiyouga/LLaMA-Factory)的文档,自行配置环境。
3.
数据准备:从github仓库下载数据(https://github.com/thu-coai/CharacterGLM-6B/blob/main/CharacterDial_data/CharacterDial_bilingual.json),处理成openai风格的对话。
4.
再次预处理:在'content'中插入user/assistant的名字,并构造出characterglm风格的system prompt。
5.
注册模板:在LLaMA-Factory/src/llamafactory/data/template.py中插入代码,注册characterglm的chat_template。
6.
更新文件:修改LLaMA-Factory/data/dataset_info.json文件,在文件末尾添加数据集信息。
7.
训练命令:可自行调整超参,运行训练命令进行微调。基于glm3、glm4、qwen微调与基于chatglm2-6b微调类似,但基座模型支持system prompt时可跳过部分处理。
简介
CharacterGLM-6b是基于chatglm2-6b微调的角色扮演模型,由清华CoAI、聆心和智谱合作开发。相关链接
CharacterGLM-6B github仓库:https://github.com/thu-coai/CharacterGLM-6B
CharacterGLM paper:https://arxiv.org/abs/2311.16832
chatglm2-6b模型仓库: https://huggingface.co/THUDM/chatglm2-6b
CharacterGLM-6B模型仓库: https://huggingface.co/thu-coai/CharacterGLM-6B
角色扮演模型的输入一般是system prompt + 对话历史。system prompt描述了模型所扮演的角色的人设信息,对话历史包含user和assistant之间的交互对话。
然而,在开发CharacterGLM-6b的时候,chatglm2-6b还未支持system prompt,huggingface transformers 也还未实现chat_template特性(https://huggingface.co/docs/transformers/main/chat_templating) 。因此,我们基于当时的LLamaFactory(https://github.com/hiyouga/ChatGLM-Efficient-Tuning)自行实现了类似chat_template的功能,编写了训练代码。
但现在LLamaFactory已经更加完善,chat_template也被各开源工具支持。本文档将介绍如何用最新的LLamaFactory,微调CharacterGLM。
LLamaFactory环境准备
请参考LLamaFactory项目仓库(https://github.com/hiyouga/LLaMA-Factory)的文档,自行配置
基于chatglm2-6b微调
数据准备
首先将其处理成openai风格的对话
代码块
def convert_to_openai_style(session: dict):
# 将CharacterDial转换成openai风格
# 注意:
# 1. 本函数并未构造出system prompt,而是保留了user_name, user_profile, assistant_name, assistant_profile等信息
# 2. 这里只处理了中文对话。若您想利用英文对话,只需用同样的代码处理session["language_en"]即可
user_name = session["user_name"]
user_profile = session.get("user_profile", "")
assistant_name = session["character_name"]
assistant_profile = session.get("character_profile", "")
# 首先将dialogue转换成openai风格
messages = []
for msg in session["dialogue"]:
speaker = msg["speaker"]
if speaker == "user":
role = "user"
elif speaker == "character":
role = "assistant"
else:
raise ValueError(f"Unknown speaker: {speaker}")
messages.append({
"role": role,
"content": msg["utterance"]
})
assert all(messages[i]["role"] != messages[i-1]["role"] for i in range(1, len(messages)))
return {
"meta": {
"user_name": user_name,
"user_profile": user_profile,
"assistant_name": assistant_name,
"assistant_profile": assistant_profile
},
"messages": messages
}