pub struct CachedTokenizer { /* private fields */ }Expand description
Implementations§
Source§impl CachedTokenizer
impl CachedTokenizer
Sourcepub fn new(
inner: Arc<dyn Tokenizer>,
special_tokens: Vec<String>,
max_memory_bytes: usize,
) -> Self
pub fn new( inner: Arc<dyn Tokenizer>, special_tokens: Vec<String>, max_memory_bytes: usize, ) -> Self
Construct a cached tokenizer.
special_tokens is the list of atomic special-token strings the inner
tokenizer recognizes (typically extracted via the HuggingFace tokenizer’s
get_added_tokens_decoder() filtering by special == true). An empty list
disables L1 — encode/encode_batch short-circuit to the inner tokenizer
without touching the cache or its counters.
max_memory_bytes is the L1 cache byte budget.
Sourcepub fn with_extend(self, enabled: bool) -> Self
pub fn with_extend(self, enabled: bool) -> Self
Enable partial-hit extension. When on, a partial cache hit also caches the freshly-tokenized suffix at its deepest special-token boundary, so each turn of a growing multi-turn conversation hits deeper than the last and per-turn tokenization cost stops growing with conversation length. Default off.
Sourcepub fn with_observer(self, on_hit: CacheEventFn, on_miss: CacheEventFn) -> Self
pub fn with_observer(self, on_hit: CacheEventFn, on_miss: CacheEventFn) -> Self
Install hit/miss callbacks so each L1 lookup pushes an event into the
supplied closures (e.g. Prometheus::Counter::inc). Replaces any
previously-set observer.
Sourcepub fn with_token_observer(self, observer: CacheTokenUsageFn) -> Self
pub fn with_token_observer(self, observer: CacheTokenUsageFn) -> Self
Install a callback that receives exact cached and uncached token counts after each successful encode while L1 is active. A partial hit reports both categories, which lets consumers maintain token-level cache totals and derive a reuse ratio. Replaces any previously-set token observer.
This observer is not called when the special-token set is empty (and L1 is therefore disabled) or when encoding returns an error.
Sourcepub fn cache_stats(&self) -> L1CacheStats
pub fn cache_stats(&self) -> L1CacheStats
Snapshot of L1 cache statistics (cumulative hits/misses/entries/memory).
Sourcepub fn clear_cache(&self)
pub fn clear_cache(&self)
Clear all cached entries and reset counters.
Trait Implementations§
Source§impl Decoder for CachedTokenizer
impl Decoder for CachedTokenizer
fn decode( &self, token_ids: &[TokenIdType], skip_special_tokens: bool, ) -> Result<DecodeResult>
Source§impl Encoder for CachedTokenizer
impl Encoder for CachedTokenizer
Source§impl Tokenizer for CachedTokenizer
impl Tokenizer for CachedTokenizer
Source§fn with_options(self, options: TokenizerOptions) -> Selfwhere
Self: Sized,
fn with_options(self, options: TokenizerOptions) -> Selfwhere
Self: Sized,
TokenizerOptions. Read moreAuto Trait Implementations§
impl !Freeze for CachedTokenizer
impl !RefUnwindSafe for CachedTokenizer
impl !UnwindSafe for CachedTokenizer
impl Send for CachedTokenizer
impl Sync for CachedTokenizer
impl Unpin for CachedTokenizer
impl UnsafeUnpin for CachedTokenizer
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
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>
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>
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