指令数据集架构:Alpaca、ChatML 与 ShareGPT 多轮对话规范
智能体辅助探究
前置要求
完成第 60 天预训练综合评估及 Python 字典/JSON 数据建模基础。
学习目标
掌握主流微调指令数据集的底层 Schema 架构,深刻理解 Alpaca 单轮格式、ChatML 角色定界符与 ShareGPT 多轮对话树的组织原理。
操作指引
学习来源:在本活动现有的阅读与练习时段内,参考 Hugging Face LLM Course: Fine-tuning LLMs 的 Chapter 11.2: Common Template Formats; Hands-on Exercise,用自己的话解释一处与本地任务的联系。保留现有练习与验收检查;若外部资料无法访问,使用课程自写的解释。1. 审视 Stanford Alpaca 规范:顶层包含 'instruction'(任务指令)、'input'(可选上下文)与 'output'(目标响应)。
2. 对比 ChatML 标记语言格式:通过显式角色定界标记(<|im_start|>system...<|im_end|>、<|im_start|>user...<|im_end|>、<|im_start|>assistant...<|im_end|>)包裹文本。
3. 分析 ShareGPT 多轮模式:在 'conversations' 列表中按 'from'(human/gpt)与 'value' 交替存储交互。
4. 辨析自然语言分隔符(如 'User: ... Assistant:')易被恶意提示注入截断劫持的缺陷,领会专用定界标记实现角色沙箱隔离的必要性。
自主演练与记录
编写 Python 规范化函数 normalize_to_chatml(record),能够自动识别传入记录为 Alpaca 还是 ShareGPT 格式,并输出符合规范的 ChatML 消息字典列表 [{'role': ..., 'content': ...}]。
若 Alpaca 记录包含非空 'input',可将 instruction 与 input 组合为 user 提示词,或将 instruction 置于 system 角色。
def normalize_to_chatml(record):
messages = []
if 'instruction' in record and 'output' in record:
if record.get('input', '').strip():
content = f"{record['instruction']}\n\nInput:\n{record['input']}"
else:
content = record['instruction']
messages.append({'role': 'user', 'content': content})
messages.append({'role': 'assistant', 'content': record['output']})
elif 'conversations' in record:
role_map = {'human': 'user', 'gpt': 'assistant', 'system': 'system'}
for turn in record['conversations']:
messages.append({'role': role_map.get(turn['from'], turn['from']), 'content': turn['value']})
return messages
实现无缝解析 Alpaca 与 ShareGPT 记录并输出 100% 合规 ChatML 结构的规范化管线。
权威源资源与权利证明
Hugging Face LLM Course: Fine-tuning LLMs
对应章节 / 时间戳: Chapter 11.2: Common Template Formats; Hands-on Exercise
验证版本与日期: Public edition checked 2026-11-01; moving pages may change
Sequenced templates, supervised fine-tuning and evaluation teaching with exercises/quizzes. LoRA method and memory claims are checked against the original paper, PEFT and the local parameter ledger, not accepted wholesale from Chapter 11.4.
Python, PyTorch, language-model training and the preceding chat-format lessons.
Use the original localized explanations, exercises and checks already present in this activity; no external account or purchase is required for that fallback.
对比候选资源与选型分析 (2)
Retained for initialization and the low-rank method, not a beginner teaching sequence.
Retained for actual API and implementation defaults; reference documentation is not a complete teaching course.
Stanford Alpaca: An Instruction-following LLaMA Model
对应章节 / 时间戳: README 'Data Release' (alpaca_data.json fields instruction, input, output)
验证版本与日期: Stanford CRFM Release (Verified: 2026-10-30)
Pioneering instruction fine-tuning dataset schema establishing the instruction/input/output format widely used across open source LLMs.
LLM fine-tuning concepts, Python dictionary data structures.
Standard Alpaca schemas and data transformation scripts are completely specified in lesson materials.
对比候选资源与选型分析 (2)
Pioneered academic instruction tuning across NLP benchmarks; Alpaca format provides the standardized single-turn JSON schema for consumer fine-tuning.
Commercially permissive instruction dataset; Alpaca establishes the architectural schema archetype.
OpenAI ChatML (Chat Markup Language) Specification and Guide
对应章节 / 时间戳: chatml.md: ChatML v0 message format (<|im_start|>role ... <|im_end|>)
验证版本与日期: openai-python tag v0.28.1, last MIT-licensed release containing chatml.md (Verified: 2026-10-31)
Standardized token delimiter schema (<|im_start|>, <|im_end|>) separating system, user, and assistant roles to prevent prompt injection and role confusion.
Tokenizer special token concepts, conversation tree data structures.
Full ChatML delimiter specs, role headers, and token stream representations are embedded in the curriculum.
对比候选资源与选型分析 (2)
Uses Human:/Assistant: plain text delimiters; ChatML provides formal token-level demarcation with dedicated boundary tokens.
FastChat conversation template; ChatML is the universal token-delimited primitive.