TokenCounter

Struct TokenCounter 

Source
pub struct TokenCounter { /* private fields */ }
Expand description

Main tokenizer interface for accurate token counting

Implementations§

Source§

impl TokenCounter

Source

pub fn new(config: TokenizerConfig) -> Result<Self>

Create a new token counter with the specified configuration

Source

pub fn default() -> Result<Self>

Create a new token counter with default configuration (GPT-4)

Source

pub fn global() -> &'static TokenCounter

Get a reference to the shared global token counter instance This is highly optimized and avoids re-initialization costs

Source

pub fn count_tokens(&self, content: &str) -> Result<usize>

Count tokens in the given text content

Source

pub fn count_tokens_batch(&self, contents: &[&str]) -> Result<usize>

Count tokens in multiple content strings and return the total

Source

pub fn estimate_file_tokens( &self, content: &str, file_path: &Path, ) -> Result<usize>

Estimate tokens for a file based on its content and metadata

Source

pub fn fits_budget(&self, content: &str) -> Result<bool>

Check if content fits within the token budget

Source

pub fn remaining_budget(&self, used_tokens: usize) -> Option<usize>

Calculate remaining budget after accounting for content

Source

pub fn chunk_content( &self, content: &str, chunk_size: usize, ) -> Result<Vec<String>>

Split content into chunks that fit within a token limit

Source

pub fn config(&self) -> &TokenizerConfig

Get the current tokenizer configuration

Source

pub fn set_token_budget(&mut self, budget: Option<usize>)

Update the token budget

Trait Implementations§

Source§

impl Debug for TokenCounter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.