pub struct ContextWindowManager { /* private fields */ }Expand description
上下文窗口管理器
负责将检索到的文档片段组装到 LLM 上下文窗口中, 根据 token 预算进行截断和丢弃,确保总 token 数不超过限制。
Implementations§
Source§impl ContextWindowManager
impl ContextWindowManager
Sourcepub fn new(config: ContextWindowConfig) -> Self
pub fn new(config: ContextWindowConfig) -> Self
创建新的上下文窗口管理器
Sourcepub fn with_defaults() -> Self
pub fn with_defaults() -> Self
使用默认配置创建
Sourcepub fn config(&self) -> &ContextWindowConfig
pub fn config(&self) -> &ContextWindowConfig
获取配置引用
Sourcepub fn count_tokens(&self, text: &str) -> usize
pub fn count_tokens(&self, text: &str) -> usize
估算文本的 token 数
Sourcepub fn assemble(&self, results: Vec<RagSearchResult>) -> ContextWindowResult
pub fn assemble(&self, results: Vec<RagSearchResult>) -> ContextWindowResult
Sourcepub fn assemble_uniform(
&self,
results: Vec<RagSearchResult>,
) -> ContextWindowResult
pub fn assemble_uniform( &self, results: Vec<RagSearchResult>, ) -> ContextWindowResult
按 Uniform 策略组装(每个片段截断相同比例)
Sourcepub fn build_prompt(
&self,
system_prompt: &str,
context_result: &ContextWindowResult,
user_query: &str,
) -> String
pub fn build_prompt( &self, system_prompt: &str, context_result: &ContextWindowResult, user_query: &str, ) -> String
构建 LLM 提示词(系统提示 + 上下文 + 查询)
Sourcepub fn estimate_prompt_tokens(
&self,
system_prompt: &str,
context_result: &ContextWindowResult,
user_query: &str,
) -> usize
pub fn estimate_prompt_tokens( &self, system_prompt: &str, context_result: &ContextWindowResult, user_query: &str, ) -> usize
估算完整提示词的 token 数
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ContextWindowManager
impl RefUnwindSafe for ContextWindowManager
impl Send for ContextWindowManager
impl Sync for ContextWindowManager
impl Unpin for ContextWindowManager
impl UnsafeUnpin for ContextWindowManager
impl UnwindSafe for ContextWindowManager
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