Expand description
Chat Template Engine
Implements APR Chat Template Specification v1.1.0
This module provides a generic, model-agnostic chat template system supporting:
- ChatML (Qwen2, OpenHermes, Yi)
- LLaMA2 (TinyLlama, Vicuna)
- Mistral/Mixtral
- Alpaca
- Phi-2/Phi-3
- Custom Jinja2 templates
§Toyota Way Principles
- Jidoka: Auto-detect template format; stop on invalid template
- Standardized Work: Unified
ChatTemplateEngineAPI - Poka-Yoke: Validate templates before application
- Muda Elimination: Use
minijinjainstead of custom parsing
§Example
use aprender::text::chat_template::{ChatMessage, ChatMLTemplate, ChatTemplateEngine};
let template = ChatMLTemplate::new();
let messages = vec![
ChatMessage::new("user", "Hello!"),
];
let formatted = template.format_conversation(&messages).expect("format conversation should succeed");
assert!(formatted.contains("<|im_start|>user"));§References
- Touvron et al. (2023) - “Llama 2” (arXiv:2307.09288)
- Bai et al. (2023) - “Qwen Technical Report” (arXiv:2309.16609)
- docs/specifications/chat-template-improvement-spec.md
Structs§
- Alpaca
Template - Alpaca Template
- ChatML
Template - ChatML Template (Qwen2, OpenHermes, Yi)
- Chat
Message - Chat message structure
- Hugging
Face Template - Jinja2-based Chat Template Engine
- Llama2
Template - LLaMA 2 Template (TinyLlama, Vicuna, LLaMA 2)
- Mistral
Template - Mistral Template (Mistral, Mixtral)
- PhiTemplate
- Phi Template (Phi-2, Phi-3)
- RawTemplate
- Raw Template (Fallback - no formatting)
- Special
Tokens - Special tokens used in chat templates
Enums§
- Template
Format - Template format enumeration
Constants§
- MAX_
LOOP_ ITERATIONS - Maximum loop iterations (CTC-05)
- MAX_
RECURSION_ DEPTH - Maximum recursion depth for templates (CTC-04)
- MAX_
TEMPLATE_ SIZE - Maximum template size in bytes (100KB per spec CTC-03)
Traits§
- Chat
Template Engine - Chat template engine trait
Functions§
- auto_
detect_ template - Auto-detect and create template from model name
- contains_
injection_ patterns - Check if content contains potential injection patterns.
- create_
template - Create a template engine for a given format
- detect_
format_ from_ name - Auto-detect template format from model name or path
- detect_
format_ from_ tokens - Auto-detect template format from special tokens
- sanitize_
user_ content - Sanitize user content to prevent prompt injection attacks.