指令數據集架構: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.