Skip to main content

ContextWindow

Struct ContextWindow 

Source
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>

Source

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,库构造器不再因本地环境崩溃。

Source

pub fn with_strategy( max_tokens: usize, strategy: Strategy<M>, ) -> Result<Self, MemoryError>

Creates a new ContextWindow with a specific strategy and default TiktokenCounter.

Source

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.

Source

pub fn with_counter(self, counter: Arc<dyn TokenCounter>) -> Self

Sets a custom token counter.

Source

pub fn max_tokens(&self) -> usize

Returns the max token limit.

Source

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.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more