Skip to main content

ConversationSummaryMemory

Struct ConversationSummaryMemory 

Source
pub struct ConversationSummaryMemory<M: BaseChatModel> { /* private fields */ }
Expand description

Conversation Summary Memory

Uses LLM to automatically summarize conversation history, avoiding overly long context.

§Example

use lc_memory::ConversationSummaryMemory;
use lc_providers::OpenAIChat;

let llm = OpenAIChat::new(config);
let memory = ConversationSummaryMemory::new(llm);

// Automatically generates summary after each conversation round
memory.save_context(&inputs, &outputs).await?;

// Returns summary instead of full history when loading
let vars = memory.load_memory_variables(&HashMap::new()).await?;

Implementations§

Source§

impl<M: BaseChatModel> ConversationSummaryMemory<M>

Source

pub fn new(llm: M) -> Self

Create a new summary memory

Source

pub fn from_messages(llm: M, messages: Vec<Message>) -> Self

Create from existing messages

Source

pub fn with_input_key(self, key: impl Into<String>) -> Self

Set input key name

Source

pub fn with_output_key(self, key: impl Into<String>) -> Self

Set output key name

Source

pub fn with_memory_key(self, key: impl Into<String>) -> Self

Set memory variable name

Source

pub fn with_summary_prompt(self, prompt: impl Into<String>) -> Self

Set summary prompt

Source

pub fn with_return_messages(self, return_messages: bool) -> Self

Set whether to return message objects

Source

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

H29: Set maximum recent turns to keep in chat_memory after summarization

Source

pub fn chat_memory(&self) -> &ChatMessageHistory

Get chat history

Source

pub async fn buffer(&self) -> String

Get current summary

Source

pub fn last_summary_error(&self) -> Option<&str>

P2-4: 最近一次摘要失败的原因(无失败则 None)。

Source

pub fn pending_lines(&self) -> &str

P2-4: 摘要失败后累计的待总结增量行。

Trait Implementations§

Source§

impl<M: BaseChatModel + Send + Sync + 'static> BaseChatMemory for ConversationSummaryMemory<M>

P0-1: ConversationSummaryMemory 实现 BaseChatMemory

Source§

fn messages(&self) -> &[Message]

Get chat message list
Source§

fn add_message(&mut self, message: Message)

Add message
Source§

fn add_user_message(&mut self, content: &str)

Add user message
Source§

fn add_ai_message(&mut self, content: &str)

Add AI message
Source§

impl<M: BaseChatModel + Send + Sync + 'static> BaseMemory for ConversationSummaryMemory<M>

Source§

fn memory_variables(&self) -> Vec<&str>

Get memory variable names Read more
Source§

fn load_memory_variables<'life0, 'life1, 'async_trait>( &'life0 self, _inputs: &'life1 HashMap<String, String>, ) -> Pin<Box<dyn Future<Output = Result<HashMap<String, Value>, MemoryError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Load memory variables Read more
Source§

fn save_context<'life0, 'life1, 'life2, 'async_trait>( &'life0 mut self, inputs: &'life1 HashMap<String, String>, outputs: &'life2 HashMap<String, String>, ) -> Pin<Box<dyn Future<Output = Result<(), MemoryError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Save context Read more
Source§

fn clear<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = Result<(), MemoryError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Clear memory

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<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