Skip to main content

Module chat_template

Module chat_template 

Source
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 ChatTemplateEngine API
  • Poka-Yoke: Validate templates before application
  • Muda Elimination: Use minijinja instead 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§

AlpacaTemplate
Alpaca Template
ChatMLTemplate
ChatML Template (Qwen2, OpenHermes, Yi)
ChatMessage
Chat message structure
HuggingFaceTemplate
Jinja2-based Chat Template Engine
Llama2Template
LLaMA 2 Template (TinyLlama, Vicuna, LLaMA 2)
MistralTemplate
Mistral Template (Mistral, Mixtral)
PhiTemplate
Phi Template (Phi-2, Phi-3)
RawTemplate
Raw Template (Fallback - no formatting)
SpecialTokens
Special tokens used in chat templates

Enums§

TemplateFormat
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§

ChatTemplateEngine
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.