大模型微调数据格式说明
大模型微调数据格式
Instruction(指令式)微调
一、数据格式
每条数据是一个 单轮任务指令 + 可选输入 + 期望输出 的组合,结构如下:
{
"instruction": "请将下面的句子翻译成英文。",
"input": "我今天很开心。",
"output": "I am very happy today."
}
当任务不需要额外输入时,input 可以为空:
{
"instruction": "讲个笑话。",
"input": "",
"output": "为什么科学家不相信原子?因为它们什么都能编。"
}
二、适用场景
-
单轮问答任务(问一句、答一句)
-
明确指令执行(如:翻译、总结、分类、改写等)
-
更适合工具型模型(如 AI 助手、写作助手)
三、训练特点
-
每条数据是独立样本,不依赖上下文
-
模型训练时只关注最后的
output,前面的instruction + input作为上下文 -
通常使用模板拼接成训练文本,如:
Instruction:
讲个笑话。
Response:
为什么科学家不相信原子?因为它们什么都能编。
四、工具支持
-
Hugging Face Trainer + LoRA(PEFT)
-
LLaMA-Factory(开启
mode=instruction) -
支持模型:LLaMA2-Instruct、Mistral-Instruct、Gemma-Instruct 等
五、优点 & 缺点
| 优点 | 缺点 |
|---|---|
| 简单明了,适合任务式训练 | 不适合多轮对话 |
| 数据易构造,格式统一 | 无法建模上下文记忆 |
| 训练快、收敛快 | 不支持角色区分(如 user/assistant) |
Chat(对话式)微调
一、数据格式
每条数据表示一次完整的多轮对话,使用 messages 字段表示对话历史:
{
"messages": [
{"role": "system", "content": "你是一个乐于助人的 AI 助手。"},
{"role": "user", "content": "你好!"},
{"role": "assistant", "content": "你好!很高兴为你服务。"},
{"role": "user", "content": "你能给我讲个笑话吗?"},
{"role": "assistant", "content": "当然可以!为什么原子不会吵架?因为它们没有脾气!"}
]
}
多个对话信息
[
{
"messages": [
{"role": "user", "content": "Hi!"},
{"role": "assistant", "content": "Hello! How can I help you?"},
{"role": "user", "content": "Tell me a joke."},
{"role": "assistant", "content": "Why don’t skeletons fight each other? They don’t have the guts."}
]
},
{
"messages": [
{"role": "user", "content": "What's the capital of France?"},
{"role": "assistant", "content": "The capital of France is Paris."}
]
},
{
"messages": [
{"role": "user", "content": "Write a poem about the ocean."},
{"role": "assistant", "content": "The waves crash down, a mighty roar..."}
]
}
]
-
role可取值:system、user、assistant -
system是可选的开场角色设定,通常放在最前面 -
每条消息是一轮发言
二、适用场景
-
多轮对话训练(如 ChatGPT、助手对话)
-
强化角色意识(模型知道谁是 user,谁是 assistant)
-
适合聊天机器人、智能客服、角色扮演 AI
三、训练特点
-
训练时将整个
messages拼接成完整对话文本 -
仅对最后一个 assistant 回复计算 loss
-
模型学会:理解对话上下文、保持一致语气、连续记忆
训练时的输入例子(ChatML 格式):
<|im_start|>system
你是一个乐于助人的 AI 助手。
<|im_end|>
<|im_start|>user
你好
<|im_end|>
<|im_start|>assistant
你好!很高兴为你服务。
<|im_end|>
<|im_start|>user
讲个冷笑话。
<|im_end|>
<|im_start|>assistant
为什么程序员不怕黑?因为他们总在调试夜间模式。
<|im_end|>
四、工具支持
-
LLaMA-Factory(使用
mode=chat,支持chat_template) -
Axolotl
-
FastChat
-
支持模型:LLaMA2-Chat、Mistral-OpenOrca、Zephyr、ChatGLM 等
五、优点 & 缺点
| 优点 | 缺点 |
|---|---|
| 支持多轮对话训练 | 数据准备复杂,需要上下文正确拼接 |
| 模型具备记忆和角色意识 | 训练成本高,推理 prompt 更长 |
| 更适合对话型助手、AI 角色系统 | 模板格式需严格统一(如 ChatML) |
[
{"role": "system", "content": "你是一个专业的英语老师。"},
{"role": "user", "content": "请问什么是时态?"},
{"role": "assistant", "content": "时态是动词的一种变化形式..."},
{"role": "user", "content": "那过去完成时呢?"}
]
模型将生成对应的 assistant 回复。
总结对比
| 项目 | 指令式微调(Instruction) | 对话式微调(Chat) |
|---|---|---|
| 数据结构 | instruction + input + output | messages(包含 role 和 content) |
| 是否支持多轮对话 | ❌ 不支持 | ✅ 支持 |
| 是否区分角色 | ❌ 不支持 | ✅ 支持(user / assistant / system) |
| 场景适配 | 工具类、问答类、任务型模型 | 聊天机器人、AI 助手、客服系统 |
| 模型行为 | 单轮响应 | 多轮记忆、上下文理解 |
| 推理方式 | 指令 + 输入 → 输出 | 构造完整对话历史 → 响应 |
| 数据准备难度 | 简单 | 中等偏高 |
更多推荐



所有评论(0)