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.