Skip to main content

RunnableWithMessageHistory

Struct RunnableWithMessageHistory 

Source
pub struct RunnableWithMessageHistory<L> { /* private fields */ }
Expand description

带记忆的 LLM 封装,作为单个 Runnable 参与 LCEL 组合。

Implementations§

Source§

impl<L> RunnableWithMessageHistory<L>

Source

pub fn new(llm: L, memory: impl BaseMemory + 'static) -> Self

用 LLM + 单个记忆对象构造封装(所有调用共享这一份记忆)。

Source

pub fn with_session_history<F>(llm: L, factory: F) -> Self
where F: Fn(&str) -> SharedMemory + Send + Sync + 'static,

用 LLM + session 回调构造封装(对齐 Python RunnableWithMessageHistory(llm, get_session_history))。

factory(session_id) 为一个 session 槽建出记忆对象;每次调用按 config.configurable["session_id"] 选槽,同一 session 复用已建槽。

§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?;
Source

pub fn with_max_sessions(self, max: usize) -> Self

设置 session 缓存上限(Sessions 模式)。超过上限后,新 session 会淘汰最旧 的槽,防 session_id 无限增长的内存 DoS(M2a)。默认 [DEFAULT_MAX_SESSIONS]。

Source

pub fn memory(&self) -> SharedMemory

暴露内部记忆句柄,便于读取已保存的历史(调试、展示、验证写回等)。

Sessions 模式下槽不唯一,返回的是占位记忆(不会参与管道读写); 要检查真实历史请从管道内部或业务侧记忆对象读取。

Trait Implementations§

Source§

impl<L> Runnable<String, LLMResult> for RunnableWithMessageHistory<L>
where L: BaseChatModel + 'static, L::Error: Into<LcelError>,

Source§

type Error = LcelError

Error type.
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,

Transforms single input to output. Read more
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,

Batch processing - transforms multiple inputs to outputs. Read more
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,

Batch processing that returns results in completion order. Read more
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,

Streaming output - for real-time responses (LLM, etc). Read more
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>>, Self::Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Stream-to-stream transformation - the core of LCEL streaming. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<I, O, R> RunnableExt<I, O> for R
where I: Send + Sync + 'static, O: Send + Sync + 'static, R: Runnable<I, O> + 'static, <R as Runnable<I, O>>::Error: Into<LcelError>,

Source§

fn pipe<O2, R2>(self, other: R2) -> RunnableSequence<Input, O2>
where O2: Send + Sync + 'static, R2: Runnable<Output, O2> + Send + Sync + 'static, <R2 as Runnable<Output, O2>>::Error: Into<LcelError>,

Pipe the output of this runnable into another runnable. Read more
Source§

fn into_sequence(self) -> RunnableSequence<Input, Output>

Create a RunnableSequence from this runnable as a single step. Read more
Source§

fn with_fallbacks<R>( self, fallbacks: Vec<R>, ) -> RunnableWithFallbacks<Input, Output>
where Input: Clone, R: Runnable<Input, Output> + Send + Sync + 'static, <R as Runnable<Input, Output>>::Error: Into<LcelError>,

Add fallback runnables that are tried if this one fails. Read more
Source§

fn with_retry(self, retry_config: RetryConfig) -> RunnableRetry<Input, Output>
where Input: Clone,

Wrap this runnable with retry logic using exponential backoff. Read more
Source§

fn configurable_alternatives<K, R>( self, which: impl Into<String>, default_key: impl Into<String>, alternatives: Vec<(K, R)>, ) -> RunnableConfigurable<Input, Output>
where K: Into<String>, R: Runnable<Input, Output> + Send + Sync + 'static, <R as Runnable<Input, Output>>::Error: Into<LcelError>,

Route between a default runnable and named alternatives at invoke time. Read more
Source§

fn configurable_fields(self) -> RunnableConfigurableFields<Input, Output>

Override recognized config fields at invoke time from config.configurable (Python’s Runnable.configurable_fields). Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more