Skip to main content

TokenCounter

Trait TokenCounter 

Source
pub trait TokenCounter: Send + Sync {
    // Required methods
    fn count(&self, text: &str) -> usize;
    fn tokenize(&self, text: &str) -> Vec<u32>;
    fn decode(&self, tokens: &[u32]) -> String;
    fn model(&self) -> TokenizerModel;
    fn is_exact(&self) -> bool;

    // Provided method
    fn count_for_model(&self, text: &str, model: TokenizerModel) -> usize { ... }
}
Expand description

Trait for token counting implementations

Required Methods§

Source

fn count(&self, text: &str) -> usize

Count tokens in text

Source

fn tokenize(&self, text: &str) -> Vec<u32>

Tokenize text (returns token IDs)

Source

fn decode(&self, tokens: &[u32]) -> String

Decode tokens back to text

Source

fn model(&self) -> TokenizerModel

Get the model being used

Source

fn is_exact(&self) -> bool

Check if this counter uses exact tokenization

Provided Methods§

Source

fn count_for_model(&self, text: &str, model: TokenizerModel) -> usize

Count tokens with model hint

Implementors§