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) -> Result<Self, MemoryError>
pub fn new(max_tokens: usize) -> Result<Self, MemoryError>
Creates a new ContextWindow with the Truncate strategy and default TiktokenCounter.
P1-4: 返回 Result 而非 panic——tiktoken 模型下载/加载失败(离线/缺模型)
时返回 MemoryError,库构造器不再因本地环境崩溃。
Sourcepub fn with_strategy(
max_tokens: usize,
strategy: Strategy<M>,
) -> Result<Self, MemoryError>
pub fn with_strategy( max_tokens: usize, strategy: Strategy<M>, ) -> Result<Self, MemoryError>
Creates a new ContextWindow with a specific strategy and default TiktokenCounter.
Sourcepub fn with_max_tokens(max_tokens: usize) -> Result<Self, MemoryError>
pub fn with_max_tokens(max_tokens: usize) -> Result<Self, MemoryError>
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).
§Budget semantics (P1-4 契约)
Strategy::Truncate: System 消息恒保留且不占预算;若 System 消息
自身超过 max_tokens,原样返回(结果可能超预算)。调用方不应假设 fit
的返回结果一定在预算内。
Strategy::Summarize: 摘要占位计入预算,但 LLM 实际产出的摘要 token
数未知——预算语义与 Truncate 不同,两者在 System 消息上口径不一致是有意为之,
各策略自行定义。
§Arguments
messages- The conversation messages to fit.
§Returns
A vector of messages that fits within the token budget.