Skip to main content

TokenCounter

Trait TokenCounter 

Source
pub trait TokenCounter {
    // Required method
    fn count_tokens(&self, text: &str) -> usize;
}
Expand description

Trait for counting tokens in text. Allows swapping word count for a real tokenizer (e.g. HuggingFace tokenizers) later.

Required Methods§

Source

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

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T> TokenCounter for &T
where T: TokenCounter + ?Sized,

Blanket implementation so &dyn TokenCounter can be used anywhere TokenCounter is required.

Source§

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

Source§

impl<T> TokenCounter for Box<T>
where T: TokenCounter + ?Sized,

Blanket implementation so Box<dyn TokenCounter + Send + Sync> can be passed to functions that accept impl TokenCounter (like chunk_text).

Source§

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

Implementors§

Source§

impl TokenCounter for HuggingFaceTokenCounter

Available on crate feature hf-tokenizer only.
Source§

impl TokenCounter for TikTokenCounter

Available on crate feature tiktoken only.
Source§

impl TokenCounter for WordCounter