pub struct CachedLLM { /* private fields */ }Expand description
Wraps an LLM and answers repeated requests from an LLMCache instead of calling
the model again.
The cache key is the namespace, the messages and the call options set through
add_options (temperature, stop words, tools, …). The wrapper
can’t see settings inside the model itself, such as the model name, so give each model
configuration its own namespace when they share a cache.
By default the namespace is the wrapped type’s name.
- A hit returns the stored result with
tokens: None: it cost nothing. If astreaming_funcis set, it gets the whole text at once. - Errors are never cached.
generate_streamcaches the final result of a streamed response and replays hits as oneTextevent plusDone. The legacystreamreplays hits but doesn’t store misses.- Concurrent identical misses each call the model.
ⓘ
let cache = Arc::new(InMemoryLLMCache::new().with_ttl(Duration::from_secs(3600)));
let llm = CachedLLM::new(OpenAI::default(), cache).with_namespace("gpt-4o-mini");
let first = llm.invoke("Say hi").await?; // calls OpenAI
let again = llm.invoke("Say hi").await?; // from the cacheImplementations§
Trait Implementations§
Source§impl LLM for CachedLLM
impl LLM for CachedLLM
fn generate<'life0, 'life1, 'async_trait>(
&'life0 self,
messages: &'life1 [Message],
) -> Pin<Box<dyn Future<Output = Result<GenerateResult, LLMError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn stream<'life0, 'life1, 'async_trait>(
&'life0 self,
messages: &'life1 [Message],
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<StreamData, LLMError>> + Send>>, LLMError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Source§fn generate_stream<'a>(&'a self, messages: &'a [Message]) -> LLMEventStream<'a>
fn generate_stream<'a>(&'a self, messages: &'a [Message]) -> LLMEventStream<'a>
Streams the response as
LLMEvents: Text chunks while the
model writes, then one Done with the complete GenerateResult (including typed
tool_calls). Errors, including the initial request failing, come as stream items. Read moreSource§fn add_options(&mut self, options: CallOptions)
fn add_options(&mut self, options: CallOptions)
This is usefull when you want to create a chain and override
LLM options
fn messages_to_string(&self, messages: &[Message]) -> String
fn invoke<'life0, 'life1, 'async_trait>(
&'life0 self,
prompt: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<String, LLMError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Auto Trait Implementations§
impl !RefUnwindSafe for CachedLLM
impl !UnwindSafe for CachedLLM
impl Freeze for CachedLLM
impl Send for CachedLLM
impl Sync for CachedLLM
impl Unpin for CachedLLM
impl UnsafeUnpin for CachedLLM
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
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