大模型指令微调中的 Prompt 设计与数据集构建指南
1. 指令微调数据集形式与质量策略
在大型语言模型(LLM)的微调过程中,Prompt 的设计对模型的训练效果及推理表现有着至关重要的影响。许多开发者在推理阶段发现,若不使用特定的 Prompt 格式直接输入,模型性能会显著下降。这引发了一个核心问题:如果在训练阶段未包含 Prompt,测试时是否可以直接输入?此外,多轮对话与单轮对话的构造方式也直接影响最终模型的能力。
目前市面上的指令微调数据格式繁多,导致选择困难。针对这一问题,我们提出以下核心观点:
- 质量优先:单次实验微调所用的指令微调数据集应选取'高质量、高多样性'的数据。低质量的噪声数据会严重干扰模型收敛。
- 资源利用:在训练资源充足的情况下,可以加入数量更多、长度更大的数据集,以增强模型的泛化能力。
- 统一格式:建议基于多个高质量数据源,制作一份格式统一的多样性数据用于 SFT(Supervised Fine-Tuning)。一次性微调通常优于多次微调,后者可能导致灾难性遗忘或效果折扣。
- 增量微调方案:如果必须进行多次微调,建议采用 LoRA 或 QLoRA 等参数高效微调方法。将训练好的 LoRA 权重合并到原始底座模型中,可以有效减轻多次微调对模型原有能力的负面影响。
2. 常见指令微调模板分析
通过观测 Hugging Face 排行榜靠前和主流开源项目的指令微调数据集,我们可以总结出几种常见的 Prompt 模板结构。不同的模型架构往往对应特定的模板格式,混用会导致生成效果不佳。
2.1 Stanford Alpaca 模板
这是最经典的指令微调模板之一,适用于大多数基础指令跟随任务。
PROMPT_DICT = {
"prompt_input": (
"Below is an instruction that describes a task, paired with an input that provides further context. "
"Write a response that appropriately completes the request.\n\n"
"### Instruction:\n{instruction}\n\n### Input:\n{input}\n\n### Response:"
),
"prompt_no_input": (
"Below is an instruction that describes a task. "
"Write a response that appropriately completes the request.\n\n"
"### Instruction:\n{instruction}\n\n### Response:"
),
}
2.2 Llama2 模板
Llama2 引入了 System Prompt 机制,增强了模型的安全性和角色设定能力。
instruction = """[INST] <<SYS>>\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.\n<</SYS>>\n\n{} [/INST]"""


