pub struct ChatTemplate { /* private fields */ }Expand description
A compiled Hugging Face chat template, ready to render.
Construction compiles the Jinja source and installs the transformers-compatible
globals/filters. render borrows &self, so a single instance is
cheap to reuse and shareable across threads.
Implementations§
Source§impl ChatTemplate
impl ChatTemplate
Sourcepub fn from_str(source: &str) -> Result<Self, Error>
pub fn from_str(source: &str) -> Result<Self, Error>
Compile from a raw Jinja chat-template string, using the default
transformers-compatible settings (see ChatTemplateBuilder to customize).
An inherent from_str (not just the FromStr impl) so callers
can write ChatTemplate::from_str(s) without importing the trait.
Sourcepub fn from_tokenizer_config(config: &TokenizerConfig) -> Result<Self, Error>
pub fn from_tokenizer_config(config: &TokenizerConfig) -> Result<Self, Error>
Compile from a parsed tokenizer_config.json, resolving the default template and
injecting the config’s special tokens (bos_token, …) into the render context.
For named-template selection (tool_use, rag) or custom options, use
builder_from_config.
Sourcepub fn builder(source: &str) -> ChatTemplateBuilder
pub fn builder(source: &str) -> ChatTemplateBuilder
Start a builder to compile source with non-default options (clock, undefined policy…).
Sourcepub fn builder_from_config(
config: &TokenizerConfig,
) -> Result<ChatTemplateBuilder, Error>
pub fn builder_from_config( config: &TokenizerConfig, ) -> Result<ChatTemplateBuilder, Error>
Start a builder from a tokenizer_config.json, carrying its special tokens. Use
template_name to pick a named sub-template.
Fails with Error::Config if the config has no chat_template field at all.
Sourcepub fn from_hub(repo_id: &str) -> Result<Self, Error>
pub fn from_hub(repo_id: &str) -> Result<Self, Error>
Fetch tokenizer_config.json for a Hub repo (default branch) and compile its default
template, injecting the config’s special tokens. Requires the hub feature.
Authentication uses hf-hub’s discovery (the HF_TOKEN env var or the cached
huggingface-cli login token); gated repos need a token with access. For a pinned
commit or branch, use from_hub_revision.
use hf_chat_template::{ChatTemplate, Message};
let tmpl = ChatTemplate::from_hub("Qwen/Qwen2.5-0.5B-Instruct")?;
let prompt = tmpl.render_messages(&[Message::user("Hi")], true)?;Sourcepub fn from_hub_revision(repo_id: &str, revision: &str) -> Result<Self, Error>
pub fn from_hub_revision(repo_id: &str, revision: &str) -> Result<Self, Error>
Like from_hub, but pins a specific revision (a branch,
tag, or commit SHA). Requires the hub feature.
Sourcepub fn render(&self, input: &RenderInput) -> Result<String, Error>
pub fn render(&self, input: &RenderInput) -> Result<String, Error>
Render the typed input model to the final prompt string. Special tokens from the
source config are injected first; any matching key in RenderInput::extra wins.
Sourcepub fn render_messages(
&self,
messages: &[Message],
add_generation_prompt: bool,
) -> Result<String, Error>
pub fn render_messages( &self, messages: &[Message], add_generation_prompt: bool, ) -> Result<String, Error>
Convenience: render with only messages and the generation-prompt flag.
Sourcepub fn render_value(&self, ctx: Value) -> Result<String, Error>
pub fn render_value(&self, ctx: Value) -> Result<String, Error>
Render with an arbitrary minijinja context value — the low-level escape hatch for callers who need to pass template variables we don’t model with typed structs.
Unlike render, this does not inject the config’s special
tokens; the caller’s context is used as-is.