templates
Chat-template helpers - thin wrappers over tokenizer.apply_chat_template.
We deliberately do NOT reproduce tinker_cookbook's Renderer class hierarchy
- Hugging Face's tokenizers already implement the template via
apply_chat_template, and the cookbook'sRenderersubclasses just wrap that call with per-model defaults (enable_thinking=Falsefor Qwen3.5-disable variants, etc.). We forward the kwarg directly when set, which keeps every HF chat-template-aware tokenizer working uniformly.
The functions here return tinker.ModelInput so they're drop-in for any
sampling / forward call.
attributeMessage= dict[str, Any]Role-tagged chat message - \{"role": "system" | "user" | "assistant", "content": "..."\}.
Matches the HF apply_chat_template input shape.
attribute__all__= ['Message', 'apply_template', 'messages_to_model_input', 'text_to_model_input']funcapply_template(tokenizer, messages, *, add_generation_prompt, enable_thinking=None) -> strCall apply_chat_template with enable_thinking forwarded only
when explicitly set.
Why the gate: Qwen3.5 tokenizers accept enable_thinking, but most
others do NOT and raise on the unknown kwarg. Forwarding only when set
keeps the helper tokenizer-agnostic - same approach already in
ChatTemplatedInference (inference/chat_templated.py).
paramtokenizerAnyparammessagesSequence[Message]paramadd_generation_promptboolparamenable_thinkingbool | None= NoneReturns
strfuncmessages_to_model_input(tokenizer, messages, *, add_generation_prompt=True, enable_thinking=None) -> tinker.ModelInputApply chat template → encode → build tinker.ModelInput.
Use add_generation_prompt=True for sampling prompts (the assistant
turn isn't included yet; the model writes it). False when building
a completed sequence (e.g. SFT teacher-forcing).
paramtokenizerAnyparammessagesSequence[Message]paramadd_generation_promptbool= Trueparamenable_thinkingbool | None= NoneReturns
tinker.tinker.ModelInputfunctext_to_model_input(tokenizer, text) -> tinker.ModelInputBypass chat-templating; tokenize text verbatim. For callers that
already have a fully rendered prompt string.
paramtokenizerAnyparamtextstrReturns
tinker.tinker.ModelInput