pub struct ContextWindow<M: BaseChatModel> { /* private fields */ }Expand description
Context window manager for fitting messages within a token budget.
Counts tokens using a TokenCounter (defaults to TiktokenCounter),
and applies a Strategy when messages exceed max_tokens.
Implementations§
Source§impl<M: BaseChatModel> ContextWindow<M>
impl<M: BaseChatModel> ContextWindow<M>
Sourcepub fn new(max_tokens: usize) -> Selfwhere
TiktokenCounter: TokenCounter,
pub fn new(max_tokens: usize) -> Selfwhere
TiktokenCounter: TokenCounter,
Creates a new ContextWindow with the Truncate strategy and default TiktokenCounter.
Sourcepub fn with_strategy(max_tokens: usize, strategy: Strategy<M>) -> Selfwhere
TiktokenCounter: TokenCounter,
pub fn with_strategy(max_tokens: usize, strategy: Strategy<M>) -> Selfwhere
TiktokenCounter: TokenCounter,
Creates a new ContextWindow with a specific strategy and default TiktokenCounter.
Sourcepub fn with_max_tokens(max_tokens: usize) -> Selfwhere
TiktokenCounter: TokenCounter,
pub fn with_max_tokens(max_tokens: usize) -> Selfwhere
TiktokenCounter: TokenCounter,
Creates a new ContextWindow with a custom max token limit and default counter/strategy.
Sourcepub fn with_counter(self, counter: Arc<dyn TokenCounter>) -> Self
pub fn with_counter(self, counter: Arc<dyn TokenCounter>) -> Self
Sets a custom token counter.
Sourcepub fn max_tokens(&self) -> usize
pub fn max_tokens(&self) -> usize
Returns the max token limit.
Sourcepub async fn fit(
&self,
messages: Vec<Message>,
) -> Result<Vec<Message>, MemoryError>
pub async fn fit( &self, messages: Vec<Message>, ) -> Result<Vec<Message>, MemoryError>
Fits messages within the token limit by applying the configured strategy.
- If total tokens are within
max_tokens, returns messages as-is. - If over, applies the
Strategy(truncate or summarize).
§Arguments
messages- The conversation messages to fit.
§Returns
A vector of messages that fits within the token budget.
Auto Trait Implementations§
impl<M> !RefUnwindSafe for ContextWindow<M>
impl<M> !UnwindSafe for ContextWindow<M>
impl<M> Freeze for ContextWindow<M>
impl<M> Send for ContextWindow<M>
impl<M> Sync for ContextWindow<M>
impl<M> Unpin for ContextWindow<M>
impl<M> UnsafeUnpin for ContextWindow<M>
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