61 / 90

第61日: 指示データフォーマット、スキーマとトークン境界アライメント

Alpaca、ChatML、ShareGPTスキーマの標準化、特殊トークン境界の整合、プロンプトインジェクション防止。

モデルと学習 480 分(8時間)
01 120 分

指示データスキーマ: Alpaca、ChatML、ShareGPTマルチターン規約

エージェント支援による探究

前提条件

第60日の事前学習評価修了およびPython JSONデータモデルの基礎。

学習目標

指示データセットの構造スキーマを習得し、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:')が抱えるプロンプトインジェクション脆弱性と、専用境界トークンによる安全なロール隔離の重要性を理解する。

演習と予測記録

Alpaca形式またはShareGPT形式を自動判別し、標準的なChatMLメッセージ辞書リスト [{'role': ..., 'content': ...}] に変換するPython関数 normalize_to_chatml(record) を実装せよ。

独立習得基準

AlpacaおよびShareGPTの両形式を、欠損なく規範的ChatML構造に100%変換できること。

公式ソースリソースと権利証跡

linking Apache-2.0

Hugging Face LLM Course: Fine-tuning LLMs

著者・提供機関: Hugging Face course contributors | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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 | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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 | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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と完全に一致することを検証するテストスクリプトを作成せよ。

独立習得基準

ChatML区切り文字のサブワード断片化を完全に排除し、特殊トークン境界を正しく設定できること。

公式ソースリソースと権利証跡

linking Apache 2.0

Hugging Face Transformers: Templates for Chat Models

著者・提供機関: Hugging Face Documentation Team | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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. | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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. 各ターンをChatML区切り文字で包括するJinja2チャットテンプレートの構文を定義する。
2. tokenizer.apply_chat_template の挙動と、推論時に add_generation_prompt=True が '<|im_start|>assistant\n' を付与する仕組みを把握する。
3. 文脈長超過問題(例: 2048トークン制限)に対処するためのインテリジェントな対話切り詰め戦略を設計する。
4. システムプロンプト(第0ターン)は常に厳格に保持し、最も古いユーザー・アシスタント対話ペアから順に切り詰めるロジックを実装する。

演習と予測記録

システムプロンプトを維持しつつ、最大トークン数 max_tokens に収まるよう最古のターンペアを切り詰める関数 truncate_dialogue(messages, max_tokens, tokenizer) を作成せよ。

独立習得基準

システムプロンプトの完全性を維持しながらトークン制限を遵守する安全な対話切り詰めを実装できること。

公式ソースリソースと権利証跡

linking MIT

OpenAI ChatML (Chat Markup Language) Specification and Guide

著者・提供機関: OpenAI Documentation Team | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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 | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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正規化とトークン境界バリデータ

独立評価(支援なし)

前提条件

第61日アクティビティ1〜3の全内容。

学習目標

雑多な指示データ入力を正規化し、特殊トークン境界を検証し、スキーマ不正を検出する本番品質のバリデーションパイプラインを構築する。

手順

1. ChatMLDatasetValidator クラスを設計・実装する。
2. Alpaca、ShareGPT、ChatMLが混在するJSONLバッチを受け入れる。
3. ロール順序の正当性(システムプロンプトは先頭のみ、ユーザーとアシスタントの交互性、末尾がアシスタントであること)を検証する。
4. 不正なレコード(連続した同ロール、空コンテンツ等)を隔離し、監査レポートを出力する。
5. 合格したレコードのみを標準ChatML形式で直列化する。

演習と予測記録

不正データを含むテストバッチに対してバリデータを実行し、異常レコードを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 | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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 | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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.