Skip to main content

ContextWindowManager

Struct ContextWindowManager 

Source
pub struct ContextWindowManager { /* private fields */ }
Expand description

上下文窗口管理器

负责将检索到的文档片段组装到 LLM 上下文窗口中, 根据 token 预算进行截断和丢弃,确保总 token 数不超过限制。

Implementations§

Source§

impl ContextWindowManager

Source

pub fn new(config: ContextWindowConfig) -> Self

创建新的上下文窗口管理器

Source

pub fn with_defaults() -> Self

使用默认配置创建

Source

pub fn config(&self) -> &ContextWindowConfig

获取配置引用

Source

pub fn count_tokens(&self, text: &str) -> usize

估算文本的 token 数

Source

pub fn assemble(&self, results: Vec<RagSearchResult>) -> ContextWindowResult

组装上下文窗口

将检索结果按得分排序,根据 token 预算组装上下文。

§参数
  • results: 检索结果列表(将按 score 降序排序)
Source

pub fn assemble_uniform( &self, results: Vec<RagSearchResult>, ) -> ContextWindowResult

按 Uniform 策略组装(每个片段截断相同比例)

Source

pub fn build_prompt( &self, system_prompt: &str, context_result: &ContextWindowResult, user_query: &str, ) -> String

构建 LLM 提示词(系统提示 + 上下文 + 查询)

Source

pub fn estimate_prompt_tokens( &self, system_prompt: &str, context_result: &ContextWindowResult, user_query: &str, ) -> usize

估算完整提示词的 token 数

Trait Implementations§

Source§

impl Default for ContextWindowManager

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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, 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, 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.