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>
impl<M: BaseChatModel> ConversationSummaryMemory<M>
Sourcepub fn from_messages(llm: M, messages: Vec<Message>) -> Self
pub fn from_messages(llm: M, messages: Vec<Message>) -> Self
Create from existing messages
Sourcepub fn with_input_key(self, key: impl Into<String>) -> Self
pub fn with_input_key(self, key: impl Into<String>) -> Self
Set input key name
Sourcepub fn with_output_key(self, key: impl Into<String>) -> Self
pub fn with_output_key(self, key: impl Into<String>) -> Self
Set output key name
Sourcepub fn with_memory_key(self, key: impl Into<String>) -> Self
pub fn with_memory_key(self, key: impl Into<String>) -> Self
Set memory variable name
Sourcepub fn with_summary_prompt(self, prompt: impl Into<String>) -> Self
pub fn with_summary_prompt(self, prompt: impl Into<String>) -> Self
Set summary prompt
Sourcepub fn with_return_messages(self, return_messages: bool) -> Self
pub fn with_return_messages(self, return_messages: bool) -> Self
Set whether to return message objects
Sourcepub fn with_max_recent_turns(self, max: usize) -> Self
pub fn with_max_recent_turns(self, max: usize) -> Self
H29: Set maximum recent turns to keep in chat_memory after summarization
Sourcepub fn chat_memory(&self) -> &ChatMessageHistory
pub fn chat_memory(&self) -> &ChatMessageHistory
Get chat history
Sourcepub fn last_summary_error(&self) -> Option<&str>
pub fn last_summary_error(&self) -> Option<&str>
P2-4: 最近一次摘要失败的原因(无失败则 None)。
Sourcepub fn pending_lines(&self) -> &str
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。
impl<M: BaseChatModel + Send + Sync + 'static> BaseChatMemory for ConversationSummaryMemory<M>
P0-1: ConversationSummaryMemory 实现 BaseChatMemory。
Source§impl<M: BaseChatModel + Send + Sync + 'static> BaseMemory for ConversationSummaryMemory<M>
impl<M: BaseChatModel + Send + Sync + 'static> BaseMemory for ConversationSummaryMemory<M>
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,
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,
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
Auto Trait Implementations§
impl<M> Freeze for ConversationSummaryMemory<M>where
M: Freeze,
impl<M> RefUnwindSafe for ConversationSummaryMemory<M>where
M: RefUnwindSafe,
impl<M> Send for ConversationSummaryMemory<M>
impl<M> Sync for ConversationSummaryMemory<M>
impl<M> Unpin for ConversationSummaryMemory<M>where
M: Unpin,
impl<M> UnsafeUnpin for ConversationSummaryMemory<M>where
M: UnsafeUnpin,
impl<M> UnwindSafe for ConversationSummaryMemory<M>where
M: UnwindSafe,
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