pub struct RunnableWithMessageHistory<L> { /* private fields */ }Expand description
LLM wrapper with memory, participating in LCEL composition as a single Runnable.
Implementations§
Source§impl<L> RunnableWithMessageHistory<L>
impl<L> RunnableWithMessageHistory<L>
Sourcepub fn new(llm: L, memory: impl BaseMemory + 'static) -> Self
pub fn new(llm: L, memory: impl BaseMemory + 'static) -> Self
Builds the wrapper from an LLM + a single memory object (all calls share this memory).
Sourcepub fn with_session_history<F>(llm: L, factory: F) -> Self
pub fn with_session_history<F>(llm: L, factory: F) -> Self
Builds the wrapper from an LLM + a session callback (mirrors Python’s
RunnableWithMessageHistory(llm, get_session_history)).
factory(session_id) builds a memory object for one session slot; each call selects the
slot by config.configurable["session_id"], and the same session reuses its built slot.
§Example
let pipe = RunnableWithMessageHistory::with_session_history(llm, |session_id| {
Arc::new(Mutex::new(Box::new(
ConversationBufferMemory::new().with_return_messages(true),
) as Box<dyn BaseMemory>))
});
let cfg = RunnableConfig::new().with_configurable("session_id", json!("s1"));
pipe.invoke("我叫小明".into(), Some(cfg)).await?;Sourcepub fn with_max_sessions(self, max: usize) -> Self
pub fn with_max_sessions(self, max: usize) -> Self
Sets the session-cache cap (Sessions mode). When exceeded, a new session evicts the
oldest slot, preventing a memory DoS from unbounded session_id growth (M2a).
Defaults to DEFAULT_MAX_SESSIONS.
Sourcepub fn memory(&self) -> SharedMemory
pub fn memory(&self) -> SharedMemory
Exposes the internal memory handle for reading saved history (debugging, display, verifying write-back, etc.).
In Sessions mode the slot is not unique, so a placeholder memory is returned (it does not participate in pipeline reads/writes); to inspect real history, read from the pipeline internals or the business-side memory object.
Trait Implementations§
Source§impl<L> Runnable<String, LLMResult> for RunnableWithMessageHistory<L>
impl<L> Runnable<String, LLMResult> for RunnableWithMessageHistory<L>
Source§fn invoke<'life0, 'async_trait>(
&'life0 self,
input: String,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<LLMResult, LcelError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn invoke<'life0, 'async_trait>(
&'life0 self,
input: String,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<LLMResult, LcelError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Source§fn batch<'life0, 'async_trait>(
&'life0 self,
inputs: Vec<Input>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Vec<Output>, Self::Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn batch<'life0, 'async_trait>(
&'life0 self,
inputs: Vec<Input>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Vec<Output>, Self::Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
Source§fn batch_as_completed<'life0, 'async_trait>(
&'life0 self,
inputs: Vec<Input>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Vec<(usize, Output)>, Self::Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn batch_as_completed<'life0, 'async_trait>(
&'life0 self,
inputs: Vec<Input>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Vec<(usize, Output)>, Self::Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
Source§fn stream<'life0, 'async_trait>(
&'life0 self,
input: Input,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<Output, Self::Error>> + Send>>, Self::Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn stream<'life0, 'async_trait>(
&'life0 self,
input: Input,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<Output, Self::Error>> + Send>>, Self::Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
Source§fn transform<'life0, 'async_trait>(
&'life0 self,
input: Pin<Box<dyn Stream<Item = Result<Input, Self::Error>> + Send>>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<Output, Self::Error>> + Send + 'life0>>, Self::Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn transform<'life0, 'async_trait>(
&'life0 self,
input: Pin<Box<dyn Stream<Item = Result<Input, Self::Error>> + Send>>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<Output, Self::Error>> + Send + 'life0>>, Self::Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
Auto Trait Implementations§
impl<L> !Freeze for RunnableWithMessageHistory<L>
impl<L> !RefUnwindSafe for RunnableWithMessageHistory<L>
impl<L> !UnwindSafe for RunnableWithMessageHistory<L>
impl<L> Send for RunnableWithMessageHistory<L>
impl<L> Sync for RunnableWithMessageHistory<L>
impl<L> Unpin for RunnableWithMessageHistory<L>
impl<L> UnsafeUnpin for RunnableWithMessageHistory<L>where
Arc<L>: UnsafeUnpin,
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
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> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<I, O, R> RunnableExt<I, O> for R
impl<I, O, R> RunnableExt<I, O> for R
Source§fn pipe<O2, R2>(self, other: R2) -> RunnableSequence<Input, O2>
fn pipe<O2, R2>(self, other: R2) -> RunnableSequence<Input, O2>
Source§fn into_sequence(self) -> RunnableSequence<Input, Output>
fn into_sequence(self) -> RunnableSequence<Input, Output>
RunnableSequence from this runnable as a single step. Read moreSource§fn with_fallbacks<R>(
self,
fallbacks: Vec<R>,
) -> RunnableWithFallbacks<Input, Output>
fn with_fallbacks<R>( self, fallbacks: Vec<R>, ) -> RunnableWithFallbacks<Input, Output>
Source§fn with_retry(self, retry_config: RetryConfig) -> RunnableRetry<Input, Output>where
Input: Clone,
fn with_retry(self, retry_config: RetryConfig) -> RunnableRetry<Input, Output>where
Input: Clone,
Source§fn configurable_alternatives<K, R>(
self,
which: impl Into<String>,
default_key: impl Into<String>,
alternatives: Vec<(K, R)>,
) -> RunnableConfigurable<Input, Output>
fn configurable_alternatives<K, R>( self, which: impl Into<String>, default_key: impl Into<String>, alternatives: Vec<(K, R)>, ) -> RunnableConfigurable<Input, Output>
Source§fn configurable_fields(self) -> RunnableConfigurableFields<Input, Output>
fn configurable_fields(self) -> RunnableConfigurableFields<Input, Output>
config.configurable (Python’s Runnable.configurable_fields). Read more