61 / 90

第 61 天:指令数据规范、Schema 架构与标记边界对齐

规范化 Alpaca、ChatML 与 ShareGPT 多轮对话模式,精准对齐特殊标记边界,杜绝提示注入漏洞。

模型与训练 480 分钟有效研习(8 小时)
01 120 分钟

指令数据集架构: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 与 ShareGPT 记录并输出 100% 合规 ChatML 结构的规范化管线。

权威源资源与权利证明

linking Apache-2.0

Hugging Face LLM Course: Fine-tuning LLMs

作者 / 机构: Hugging Face course contributors | 原始出处链接 ↗

对应章节 / 时间戳: 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)
LoRA original paper ↗ compared_alternative [arXiv.org perpetual non-exclusive license / Open Access]

Retained for initialization and the low-rank method, not a beginner teaching sequence.

Hugging Face PEFT ↗ compared_alternative [Apache-2.0]

Retained for actual API and implementation defaults; reference documentation is not a complete teaching course.

linking Apache 2.0

Stanford Alpaca: An Instruction-following LLaMA Model

作者 / 机构: Rohan Taori, Ishaan Gulrajani, Tianyi Zhang, Yann Dubois, Xuechen Li, Carlos Guestrin, Percy Liang, Tatsunori B. Hashimoto | 原始出处链接 ↗

对应章节 / 时间戳: 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.

Databricks Dolly 15k Dataset ↗ compared_alternative [CC-BY-SA 3.0]

Commercially permissive instruction dataset; Alpaca establishes the architectural schema archetype.

linking MIT

OpenAI ChatML (Chat Markup Language) Specification and Guide

作者 / 机构: OpenAI Documentation Team | 原始出处链接 ↗

对应章节 / 时间戳: 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)
Anthropic Claude Prompt Engineering Guidelines ↗ compared_alternative [Proprietary Documentation]

Uses Human:/Assistant: plain text delimiters; ChatML provides formal token-level demarcation with dedicated boundary tokens.

Vicuna ShareGPT Multi-Turn Schema ↗ compared_alternative [Apache 2.0]

FastChat conversation template; ChatML is the universal token-delimited primitive.

02 120 分钟

分词器特殊标记、定界符与标记边界精准对齐

独立实战探究

前置要求

第 61 天活动 1 及字节对编码(BPE)分词原理。

学习目标

向分词器正确注册特殊标记(<|im_start|>、<|im_end|>),杜绝子词碎片化切分,排查并修复标记边界对齐故障。

操作指引

1. 观察普通分词器在遇到未注册定界符时将其切碎为多个子词碎片的情况。
2. 使用 tokenizer.add_special_tokens({'additional_special_tokens': ['<|im_start|>', '<|im_end|>']}) 注册定界符并理解嵌入层大小同步调整。
3. 验证标记边界对齐:确保定界符被编码为单一独立的标记 ID,解码时无多余前缀空格干扰。
4. 剖析 BPE 中的空格前缀陷阱(' hello' 与 'hello' 的标记差异),规范换行符与定界符排版。

自主演练与记录

编写验证脚本:向分词器添加特殊标记,编码字符串 '<|im_start|>user\nHello<|im_end|>',断言首尾标记恰好是对应注册标记的单一 ID 且长度为 1。

独立掌握标准

消除 ChatML 定界符的子词拆分现象,实现特殊标记边界的精准对齐与验证。

权威源资源与权利证明

linking Apache 2.0

Hugging Face Transformers: Templates for Chat Models

作者 / 机构: Hugging Face Documentation Team | 原始出处链接 ↗

对应章节 / 时间戳: Using apply_chat_template: add_generation_prompt

验证版本与日期: Hugging Face Documentation (Verified: 2026-10-30)

入选理由:

Widely adopted system for mapping list-of-dict messages into tokenized strings with tokenizer.apply_chat_template().

来源前置条件:

Python, Jinja2 template basics, Hugging Face PreTrainedTokenizer.

安全回退方案(离线弹性):

Complete Jinja2 template implementations and boundary token unit tests are self-contained.

对比候选资源与选型分析 (2)
Axolotl Prompt Formats Guide ↗ compared_alternative [Apache 2.0]

Configuration-driven formatting framework; Hugging Face chat templates are the direct tokenizer-level standard.

vLLM Online Serving: Chat Template ↗ compared_alternative [Apache 2.0]

High-performance inference chat templating; HF chat templating is the training-time reference implementation.

linking Open Access

Llama 2: Open Foundation and Fine-Tuned Chat Models

作者 / 机构: Hugo Touvron, Louis Martin, Kevin Stone, Peter Albert, Amjad Almahairi, et al. | 原始出处链接 ↗

对应章节 / 时间戳: Section 3.1 Supervised Fine-Tuning (SFT)

验证版本与日期: arXiv:2307.09288v2 (Verified: 2026-10-30)

入选理由:

Defines the standard LLaMA-2 instruction format ([INST] <<SYS>> ... <</SYS>> [/INST]) and analyzes token boundary alignment impact on generation stability.

来源前置条件:

Transformer architecture, RLHF/SFT pipelines.

安全回退方案(离线弹性):

The LLaMA-2 prompt structure, token boundary rules, and comparative analysis are fully worked out in curriculum lessons.

对比候选资源与选型分析 (2)

Original foundation model paper without conversational chat tuning details; LLaMA-2 formalizes dialogue formatting.

Jiang et al. Mistral 7B ↗ compared_alternative [Open Access]

Adopts similar [INST] formatting; LLaMA-2 provides the foundational ablation on system prompt integration.

03 120 分钟

多轮对话序列化与系统提示词条件约束

智能体辅助实践

前置要求

第 61 天活动 1、2 及 Jinja2 模板引擎语法。

学习目标

利用 Jinja2 模板将多轮交互列表线性序列化为规范标记流,并在上下文超长截断时严格锁定并保留系统提示词。

操作指引

1. 编写 Jinja2 模板将 system、user、assistant 对话逐轮封装为 ChatML 语法块。
2. 理解 tokenizer.apply_chat_template 机制,及在推理阶段启用 add_generation_prompt=True 追加助手引导符的原理。
3. 解决长对话超出模型最大序列长度(如 2048 标记)时的截断难题。
4. 设计安全截断策略:无论对话如何截断,系统提示词必须完好保留,优先从最早的普通用户/助手对开始成对淘汰。

自主演练与记录

构建函数 truncate_dialogue(messages, max_tokens, tokenizer),在保证序列长度 <= max_tokens 的前提下淘汰历史最久轮次,并严格保留系统提示词。

独立掌握标准

成功实现兼顾系统提示词完整性与严格长度约束的对话智能截断算法。

权威源资源与权利证明

linking MIT

OpenAI ChatML (Chat Markup Language) Specification and Guide

作者 / 机构: OpenAI Documentation Team | 原始出处链接 ↗

对应章节 / 时间戳: 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)
Anthropic Claude Prompt Engineering Guidelines ↗ compared_alternative [Proprietary Documentation]

Uses Human:/Assistant: plain text delimiters; ChatML provides formal token-level demarcation with dedicated boundary tokens.

Vicuna ShareGPT Multi-Turn Schema ↗ compared_alternative [Apache 2.0]

FastChat conversation template; ChatML is the universal token-delimited primitive.

linking Apache 2.0

Hugging Face Transformers: Templates for Chat Models

作者 / 机构: Hugging Face Documentation Team | 原始出处链接 ↗

对应章节 / 时间戳: Using apply_chat_template

验证版本与日期: Hugging Face Documentation (Verified: 2026-10-30)

入选理由:

Widely adopted system for mapping list-of-dict messages into tokenized strings with tokenizer.apply_chat_template().

来源前置条件:

Python, Jinja2 template basics, Hugging Face PreTrainedTokenizer.

安全回退方案(离线弹性):

Complete Jinja2 template implementations and boundary token unit tests are self-contained.

对比候选资源与选型分析 (2)
Axolotl Prompt Formats Guide ↗ compared_alternative [Apache 2.0]

Configuration-driven formatting framework; Hugging Face chat templates are the direct tokenizer-level standard.

vLLM Online Serving: Chat Template ↗ compared_alternative [Apache 2.0]

High-performance inference chat templating; HF chat templating is the training-time reference implementation.

04 120 分钟

独立考核:生产级 ChatML 规范化与标记边界校验器

独立实战考核(无 AI 辅助)

前置要求

第 61 天活动 1 至 3 的全部技能。

学习目标

独立构建生产级微调数据校验清洗模块,规范化异构输入、严格校验特殊标记边界并排查对话结构畸变。

操作指引

1. 封装 ChatMLDatasetValidator 校验器类。
2. 接入混合格式的 JSONL 批次数据(包含合规与恶意/畸变样本)。
3. 验证角色序列合法性:系统提示词仅能出现在首位、角色严格交替、末尾必须为助手应答。
4. 剔除连续相同角色、空内容等脏数据,并输出审计统计数据。
5. 确保合格样本均能生成零标记断裂的合规 ChatML 标记流。

自主演练与记录

对包含 2 个畸变样本的测试数据批次运行你的校验器,断言 100% 捕获违规样本并仅放行合规样本。

独立掌握标准

独立交付生产级微调数据格式校验器,精准检出 100% 结构违规记录且无假阳性。

权威源资源与权利证明

linking Apache 2.0

Stanford Alpaca: An Instruction-following LLaMA Model

作者 / 机构: Rohan Taori, Ishaan Gulrajani, Tianyi Zhang, Yann Dubois, Xuechen Li, Carlos Guestrin, Percy Liang, Tatsunori B. Hashimoto | 原始出处链接 ↗

对应章节 / 时间戳: README 'Data Release'

验证版本与日期: 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.

Databricks Dolly 15k Dataset ↗ compared_alternative [CC-BY-SA 3.0]

Commercially permissive instruction dataset; Alpaca establishes the architectural schema archetype.

linking Apache 2.0

Hugging Face Transformers: Templates for Chat Models

作者 / 机构: Hugging Face Documentation Team | 原始出处链接 ↗

对应章节 / 时间戳: Model training

验证版本与日期: Hugging Face Documentation (Verified: 2026-10-30)

入选理由:

Widely adopted system for mapping list-of-dict messages into tokenized strings with tokenizer.apply_chat_template().

来源前置条件:

Python, Jinja2 template basics, Hugging Face PreTrainedTokenizer.

安全回退方案(离线弹性):

Complete Jinja2 template implementations and boundary token unit tests are self-contained.

对比候选资源与选型分析 (2)
Axolotl Prompt Formats Guide ↗ compared_alternative [Apache 2.0]

Configuration-driven formatting framework; Hugging Face chat templates are the direct tokenizer-level standard.

vLLM Online Serving: Chat Template ↗ compared_alternative [Apache 2.0]

High-performance inference chat templating; HF chat templating is the training-time reference implementation.