pub struct ContextWindowManager { /* private fields */ }Expand description
Context window management for RAG
Helps manage token limits when constructing prompts with retrieved documents.
Implementations§
Source§impl ContextWindowManager
impl ContextWindowManager
Sourcepub fn new(max_tokens: usize) -> Self
pub fn new(max_tokens: usize) -> Self
Create new context window manager
§Arguments
max_tokens- Maximum tokens available for context (e.g., 4096 for GPT-3.5)
Sourcepub fn fit_documents<F>(
&self,
documents: Vec<Neighbor>,
token_estimator: F,
reserved_tokens: usize,
) -> Vec<Neighbor>
pub fn fit_documents<F>( &self, documents: Vec<Neighbor>, token_estimator: F, reserved_tokens: usize, ) -> Vec<Neighbor>
Fit documents into context window
Greedily adds documents until hitting token limit.
§Arguments
documents- Ranked documents to includetoken_estimator- Function to estimate tokens in textreserved_tokens- Tokens reserved for prompt template, query, etc.
§Returns
Documents that fit within the context window
Sourcepub fn simple_token_estimator(text: &str) -> usize
pub fn simple_token_estimator(text: &str) -> usize
Simple token estimator (words * 1.3)
Rough approximation. For production, use tiktoken or similar.
Auto Trait Implementations§
impl Freeze for ContextWindowManager
impl RefUnwindSafe for ContextWindowManager
impl Send for ContextWindowManager
impl Sync for ContextWindowManager
impl Unpin 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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more