pub struct ContextWindowManager {
pub max_tokens: usize,
pub keep_first_n: usize,
pub keep_last_n: usize,
}Fields§
§max_tokens: usize§keep_first_n: usizeAlways keep first N messages (typically system prompt)
keep_last_n: usizeAlways keep last N messages
Implementations§
Source§impl ContextWindowManager
impl ContextWindowManager
pub fn new(max_tokens: usize) -> Self
pub fn with_keep_first_n(self, n: usize) -> Self
pub fn with_keep_last_n(self, n: usize) -> Self
Sourcepub fn estimate_tokens(text: &str) -> usize
pub fn estimate_tokens(text: &str) -> usize
Simple token estimation: ~4 chars/token for Latin, ~1.5 for CJK Mixed text uses a compromise of 3 chars/token
Sourcepub fn trim(&self, messages: &mut Vec<ChatMessage>)
pub fn trim(&self, messages: &mut Vec<ChatMessage>)
Trim message list to keep total tokens under max_tokens。
Trimming strategy:
- Always keep the first
keep_first_nmessages (typically system prompt) - Always keep the last
keep_last_nmessages (recent conversation) - Remove oldest messages from the middle until within budget
Trait Implementations§
Source§impl Clone for ContextWindowManager
impl Clone for ContextWindowManager
Source§fn clone(&self) -> ContextWindowManager
fn clone(&self) -> ContextWindowManager
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ContextWindowManager
impl Debug for ContextWindowManager
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