pub struct TokenCounter { /* private fields */ }Expand description
Token counter with caching and batch support
For repeated counting with the same encoding, this struct provides a cleaner interface than the free functions.
§Example
use m2m::tokenizer::TokenCounter;
use m2m::models::Encoding;
let counter = TokenCounter::new(Encoding::O200kBase);
let tokens1 = counter.count("Hello, world!");
let tokens2 = counter.count("Another message");
let total = counter.count_many(&["Hello", "World"]);Implementations§
Source§impl TokenCounter
impl TokenCounter
Sourcepub fn default_encoding() -> Self
pub fn default_encoding() -> Self
Create a token counter for the default encoding (cl100k_base)
Sourcepub fn count_many(&self, texts: &[&str]) -> usize
pub fn count_many(&self, texts: &[&str]) -> usize
Count tokens in multiple texts
Sourcepub fn count_json(&self, value: &Value) -> usize
pub fn count_json(&self, value: &Value) -> usize
Count tokens in JSON value (serialized)
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more