LLaMA-Factory 训练数据默认使用 instruction、input、output 三个 key

在 LLaMA-Factory 进行 SFT(Directive supervision fine-tuning 指令监督微调)时,训练数据的格式 非常重要,因为大模型依赖标准化的数据结构来学习 指令-响应 模式。

identity.json 文件的数据采用了 "instruction"、"input"、"output" 这三个 key,它们的作用如下:

Key 作用 示例
"instruction" 代表用户给 AI 的指令(问题或任务描述)。 "instruction": "Who are you?"
"input" 可选,表示提供给 AI 的额外输入信息。 "input": ""(如果不需要额外输入,可以留空)
"output" 期望 AI 生成的回答(AI 的理想输出)。 "output": "I am {``{name}}, an AI assistant developed by {``{author}}."

1. LLaMA-Factory 训练数据的格式要求

如果你要换用自己的数据集,格式应该符合 以下 JSON 结构

identity.json文件

json 复制代码
[
  {
    "instruction": "请介绍一下你自己。",
    "input": "",
    "output": "我是Llama-Chinese,由LLaMA Factory开发。我可以帮助您回答问题、提供信息和进行对话。"
  },
  {
    "instruction": "翻译下面的句子:",
    "input": "Hello, how are you?",
    "output": "你好,你怎么样?"
  },
  {
    "instruction": "请用Python写一个计算斐波那契数列的函数。",
    "input": "",
    "output": "```python\ndef fibonacci(n):\n    if n <= 1:\n        return n\n    return fibonacci(n-1) + fibonacci(n-2)\n```"
  }
]

格式要求

  1. JSON 格式的数组 ,每个数据点是一个字典 {}
  2. 必须包含 instructionoutputinput 可选。
  3. instruction 是给 AI 的指令,output 是期望的 AI 输出
  4. input 只有在额外信息需要提供时才填(例如机器翻译任务)。

2. 你可以使用哪些 key?如何让大模型正确学习?

(1) 基础的三元结构

  • 最通用的格式

    json 复制代码
    {
      "instruction": "请翻译这句话:",
      "input": "The weather is great today.",
      "output": "今天天气很好。"
    }
  • 适用于:对话数据、翻译、问答任务


(2) 只有 instructionoutput

  • 如果任务不需要额外输入,可以省略 input

    json 复制代码
    {
      "instruction": "写一个Python函数,计算平方数。",
      "output": "```python\ndef square(x):\n    return x * x\n```"
    }
  • 适用于:代码生成、开放性问题、一般问答任务


(3) 只有 inputoutput(极少数情况)

  • 某些任务 instruction 可以省略:

    json 复制代码
    {
      "input": "What is the capital of France?",
      "output": "Paris"
    }
  • 适用于:部分问答任务、无明确指令的情况

  • 但在 LLaMA-Factory 里,通常 instruction 是必须的,所以不建议这么做。


3. 如何自定义数据集 key?

如果你想改 key 的名字,比如你想用 "query" 代替 "instruction",那你需要 LLaMA-Factory 里修改解析代码,否则模型不会正确识别你的数据。

最简单的做法保持 instructioninputoutput 这三个 key,因为 LLaMA-Factory 默认支持这种格式,不需要额外修改代码。


4. 如何确认你的数据格式正确?

训练之前,可以用 Python 代码检查 JSON 格式:

python 复制代码
import json

with open("your_dataset.json", "r", encoding="utf-8") as f:
    dataset = json.load(f)

for sample in dataset[:3]:  # 只检查前3条数据
    assert "instruction" in sample, "数据缺少 instruction 字段"
    assert "output" in sample, "数据缺少 output 字段"
    print(sample)

如果没有报错,说明格式正确。


5. 其他数据格式示例

(1) 翻译任务

json 复制代码
[
  {
    "instruction": "请把以下文本翻译成法语。",
    "input": "Hello, how are you?",
    "output": "Bonjour, comment ça va?"
  }
]

适用于 机器翻译微调


(2) 代码生成任务

json 复制代码
[
  {
    "instruction": "写一个 Python 函数,判断一个数是否是素数。",
    "input": "",
    "output": "```python\ndef is_prime(n):\n    if n < 2:\n        return False\n    for i in range(2, int(n ** 0.5) + 1):\n        if n % i == 0:\n            return False\n    return True```"
  }
]

适用于 代码生成模型微调


(3) 文本摘要任务

json 复制代码
[
  {
    "instruction": "请总结以下文章的主要内容。",
    "input": "最近人工智能技术取得了重大突破,许多公司开始投入大规模模型的研究。",
    "output": "人工智能技术发展迅速,公司纷纷加大对大模型的投入。"
  }
]

适用于 文本摘要微调


6. 总结

LLaMA-Factory 训练数据默认使用 instructioninputoutput 三个 key

确保 JSON 格式正确,否则模型无法正确学习

如果要自定义 key,建议修改 LLaMA-Factory 的代码(不推荐) ,或保持默认格式。

数据结构要根据任务类型调整,比如:

  • 问答、闲聊:instruction + output
  • 翻译任务:instruction + input + output
  • 代码生成:instruction + output
  • 文本摘要:instruction + input + output

💡 结论:如果你想换数据集,只需要用 instructioninputoutput 结构组织数据,并替换内容,就可以直接用于 LLaMA-Factory 训练! 🚀

相关推荐
boooo_hhh几秒前
第32周———Tensorflow|LSTM-火灾温度预测
人工智能·tensorflow·lstm
广州山泉婚姻7 分钟前
Python函数编写指南
人工智能·python·深度学习
张晓丽-33 分钟前
ReAct
人工智能
非优秀程序员43 分钟前
知识库切片模型架构设计:提升AI问答准确率的创新方法
人工智能
沛沛老爹1 小时前
NumPy玩转数据科学
人工智能·python·机器学习·numpy·数据科学·多维数组·python库
非优秀程序员1 小时前
10 个最佳开源 ChatGPT 替代方案,100% 本地运行
前端·人工智能·后端
中國龍在廣州1 小时前
AI之初,性本?
人工智能·科技·深度学习·机器学习·chatgpt·机器人
liliangcsdn1 小时前
标书生成过程中的prompt解读
人工智能·python·信息可视化·语言模型·prompt
非优秀程序员1 小时前
作为开发者,我最喜欢的 Github 开源工具(2025 年上半年榜单)
人工智能·开源·全栈
少林码僧1 小时前
14.2 《3小时从零搭建企业级LLaMA3语言助手:GitHub配置+私有化模型集成全实战》
人工智能·机器学习·语言模型·langchain