pub struct TokenBudgetBatcher { /* private fields */ }Expand description
Batch input accumulator that respects a total token budget instead of a fixed item count.
Current approach: estimates token count as text.len() / 4 (chars per token approximation).
A production deployment can inject a real tokeniser via TokenBudgetBatcher::with_token_fn.
§Example
use inference::batch::TokenBudgetBatcher;
let mut batcher = TokenBudgetBatcher::new(2048);
batcher.push("short text".to_string());
batcher.push("another short text".to_string());
let batches = batcher.finish();
assert_eq!(batches.len(), 1);Implementations§
Source§impl TokenBudgetBatcher
impl TokenBudgetBatcher
Sourcepub fn new(token_budget: usize) -> Self
pub fn new(token_budget: usize) -> Self
Create a batcher with the given token budget.
Defaults to character-count estimation (len / 4).
Use with_token_fn to inject a real tokeniser.
Sourcepub fn with_token_fn(
self,
f: impl Fn(&str) -> usize + Send + Sync + 'static,
) -> Self
pub fn with_token_fn( self, f: impl Fn(&str) -> usize + Send + Sync + 'static, ) -> Self
Replace the default character-count estimator with a real token counter.
Sourcepub fn push(&mut self, text: String)
pub fn push(&mut self, text: String)
Add a text to the current batch.
If adding text would exceed the token budget, the current batch is flushed first and a
new batch starting with text is begun.
Sourcepub fn push_all(&mut self, texts: impl IntoIterator<Item = String>)
pub fn push_all(&mut self, texts: impl IntoIterator<Item = String>)
Add multiple texts at once.
Sourcepub fn finish(&mut self) -> Vec<Vec<String>>
pub fn finish(&mut self) -> Vec<Vec<String>>
Flush any pending batch and return all accumulated batches.
The batcher is reset after this call and can be reused.
Sourcepub fn pending_count(&self) -> usize
pub fn pending_count(&self) -> usize
Number of texts accumulated in the current (unflushed) batch.
Sourcepub fn pending_tokens(&self) -> usize
pub fn pending_tokens(&self) -> usize
Total tokens accumulated in the current (unflushed) batch.
Auto Trait Implementations§
impl Freeze for TokenBudgetBatcher
impl !RefUnwindSafe for TokenBudgetBatcher
impl Send for TokenBudgetBatcher
impl Sync for TokenBudgetBatcher
impl Unpin for TokenBudgetBatcher
impl UnsafeUnpin for TokenBudgetBatcher
impl !UnwindSafe for TokenBudgetBatcher
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