Skip to main content

Tokenizer

Trait Tokenizer 

Source
pub trait Tokenizer {
    // Required methods
    fn apply_chat_template(
        &self,
        messages: &[Message],
        chat_template: &str,
        tokenize: bool,
        add_generation_prompt: bool,
        enable_thinking: bool,
    ) -> String;
    fn encode(&self, texts: &[String]) -> Vec<usize>;
    fn decode(&self, ids: &[usize]) -> String;
}
Expand description

Text tokenization and chat-templating, implemented per model/tokenizer family.

Required Methods§

Source

fn apply_chat_template( &self, messages: &[Message], chat_template: &str, tokenize: bool, add_generation_prompt: bool, enable_thinking: bool, ) -> String

Renders messages through chat_template (a Jinja-style template string, per the Hugging Face chat-template convention), optionally tokenizing the result (tokenize), appending a generation prompt (add_generation_prompt), and enabling “thinking”/reasoning mode (enable_thinking) for models that support it.

Source

fn encode(&self, texts: &[String]) -> Vec<usize>

Encodes texts into a flat sequence of token IDs.

Source

fn decode(&self, ids: &[usize]) -> String

Decodes a sequence of token IDs back into text.

Implementors§