pub struct TokenCounter { /* private fields */ }
Implementations§
Source§impl TokenCounter
impl TokenCounter
pub fn new(model: &str) -> Result<Self, Box<dyn Error>>
pub fn count_tokens(&self, text: &str) -> u32
pub fn count_messages_tokens(&self, messages: &[ChatMessage]) -> u32
Sourcepub fn calculate_usage(&self, prompt: &str, completion: &str) -> Usage
pub fn calculate_usage(&self, prompt: &str, completion: &str) -> Usage
Creates a Usage struct with token counts for prompt and completion
Sourcepub fn truncate_to_tokens(&self, text: &str, max_tokens: u32) -> String
pub fn truncate_to_tokens(&self, text: &str, max_tokens: u32) -> String
Truncates text to approximately fit within max_tokens
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