pub struct TokenCounter { /* private fields */ }
Expand description
Main tokenizer interface for accurate token counting
Implementations§
Source§impl TokenCounter
impl TokenCounter
Sourcepub fn new(config: TokenizerConfig) -> Result<Self>
pub fn new(config: TokenizerConfig) -> Result<Self>
Create a new token counter with the specified configuration
Sourcepub fn global() -> &'static TokenCounter
pub fn global() -> &'static TokenCounter
Get a reference to the shared global token counter instance This is highly optimized and avoids re-initialization costs
Sourcepub fn count_tokens(&self, content: &str) -> Result<usize>
pub fn count_tokens(&self, content: &str) -> Result<usize>
Count tokens in the given text content
Sourcepub fn count_tokens_batch(&self, contents: &[&str]) -> Result<usize>
pub fn count_tokens_batch(&self, contents: &[&str]) -> Result<usize>
Count tokens in multiple content strings and return the total
Sourcepub fn estimate_file_tokens(
&self,
content: &str,
file_path: &Path,
) -> Result<usize>
pub fn estimate_file_tokens( &self, content: &str, file_path: &Path, ) -> Result<usize>
Estimate tokens for a file based on its content and metadata
Sourcepub fn fits_budget(&self, content: &str) -> Result<bool>
pub fn fits_budget(&self, content: &str) -> Result<bool>
Check if content fits within the token budget
Sourcepub fn remaining_budget(&self, used_tokens: usize) -> Option<usize>
pub fn remaining_budget(&self, used_tokens: usize) -> Option<usize>
Calculate remaining budget after accounting for content
Sourcepub fn chunk_content(
&self,
content: &str,
chunk_size: usize,
) -> Result<Vec<String>>
pub fn chunk_content( &self, content: &str, chunk_size: usize, ) -> Result<Vec<String>>
Split content into chunks that fit within a token limit
Sourcepub fn config(&self) -> &TokenizerConfig
pub fn config(&self) -> &TokenizerConfig
Get the current tokenizer configuration
Sourcepub fn set_token_budget(&mut self, budget: Option<usize>)
pub fn set_token_budget(&mut self, budget: Option<usize>)
Update the token budget
Trait Implementations§
Auto Trait Implementations§
impl Freeze for TokenCounter
impl RefUnwindSafe for TokenCounter
impl Send for TokenCounter
impl Sync for TokenCounter
impl Unpin for TokenCounter
impl UnwindSafe for TokenCounter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more