pub struct TokenCounterFactory;Expand description
Factory for creating token counters for different providers.
Use this factory to get the appropriate token counter for your provider and model. The factory handles selecting the correct tokenizer and context window size.
§Example
use multi_llm::{TokenCounterFactory, TokenCounter};
// Create counter for OpenAI
let openai = TokenCounterFactory::create_counter("openai", "gpt-4")?;
// Create counter for Anthropic
let anthropic = TokenCounterFactory::create_counter("anthropic", "claude-3-5-sonnet")?;
// Create counter with custom limit
let custom = TokenCounterFactory::create_counter_with_limit("openai", "gpt-4", 4096)?;§Supported Providers
openai: Uses tiktoken for exact countsanthropic: Uses approximation with safety marginollama: Uses cl100k_base (approximation)lmstudio: Uses cl100k_base (approximation)
Implementations§
Source§impl TokenCounterFactory
impl TokenCounterFactory
Sourcepub fn create_counter(
provider: &str,
model: &str,
) -> LlmResult<Arc<dyn TokenCounter>>
pub fn create_counter( provider: &str, model: &str, ) -> LlmResult<Arc<dyn TokenCounter>>
Create token counter for specific provider and model
Sourcepub fn create_counter_with_limit(
provider: &str,
model: &str,
max_tokens: u32,
) -> LlmResult<Arc<dyn TokenCounter>>
pub fn create_counter_with_limit( provider: &str, model: &str, max_tokens: u32, ) -> LlmResult<Arc<dyn TokenCounter>>
Create counter with custom context window size
Auto Trait Implementations§
impl Freeze for TokenCounterFactory
impl RefUnwindSafe for TokenCounterFactory
impl Send for TokenCounterFactory
impl Sync for TokenCounterFactory
impl Unpin for TokenCounterFactory
impl UnwindSafe for TokenCounterFactory
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