pub struct ConversationSummaryBufferMemory<M: BaseChatModel> { /* private fields */ }Expand description
Conversation Summary Buffer Memory
Combines summary and full conversation:
- Keeps the last k rounds of full conversation (ensuring fluency)
- Summarizes older conversations (saving tokens)
§Example
ⓘ
use lc_memory::ConversationSummaryBufferMemory;
use lc_providers::OpenAIChat;
let llm = OpenAIChat::new(config);
let memory = ConversationSummaryBufferMemory::new(llm, 5); // Keep last 5 rounds
// After 20 rounds:
// - First 15 rounds -> summary
// - Last 5 rounds -> full conversationImplementations§
Source§impl<M: BaseChatModel> ConversationSummaryBufferMemory<M>
impl<M: BaseChatModel> ConversationSummaryBufferMemory<M>
Sourcepub fn with_input_key(self, key: impl Into<String>) -> Self
pub fn with_input_key(self, key: impl Into<String>) -> Self
设置输入 key,save_context 用它从 inputs 中取出用户输入。
Sourcepub fn with_output_key(self, key: impl Into<String>) -> Self
pub fn with_output_key(self, key: impl Into<String>) -> Self
设置输出 key,save_context 用它从 outputs 中取出 AI 输出。
Sourcepub fn with_memory_key(self, key: impl Into<String>) -> Self
pub fn with_memory_key(self, key: impl Into<String>) -> Self
设置记忆 key,加载的历史将暴露在该 key 下。
Sourcepub fn with_summary_prompt(self, prompt: impl Into<String>) -> Self
pub fn with_summary_prompt(self, prompt: impl Into<String>) -> Self
设置用于生成摘要的提示词模板。
Sourcepub fn with_return_messages(self, return_messages: bool) -> Self
pub fn with_return_messages(self, return_messages: bool) -> Self
设置加载的历史是否以消息列表(而非文本)形式返回。
Sourcepub fn with_counter(self, counter: Arc<dyn TokenCounter>) -> Self
pub fn with_counter(self, counter: Arc<dyn TokenCounter>) -> Self
注入自定义 token 计数器。
默认 TiktokenCounter(BPE 口径,与 ContextWindow 一致);需要零依赖
快路径时注入 CharRatioCounter::new(4)。
Sourcepub fn set_summary(&mut self, summary: impl Into<String>)
pub fn set_summary(&mut self, summary: impl Into<String>)
P1-3: 从持久化存储回灌摘要状态,保证续写会话摘要链连续。
Sourcepub fn set_max_token_limit(&mut self, max_token_limit: usize)
pub fn set_max_token_limit(&mut self, max_token_limit: usize)
P1-3: 设置 token 预算(持久化 config 的单一来源)。
Sourcepub fn chat_memory(&self) -> &ChatMessageHistory
pub fn chat_memory(&self) -> &ChatMessageHistory
返回底层聊天消息历史的不可变引用。
Sourcepub fn chat_memory_mut(&mut self) -> &mut ChatMessageHistory
pub fn chat_memory_mut(&mut self) -> &mut ChatMessageHistory
返回底层聊天消息历史的可变引用。
Sourcepub fn max_token_limit(&self) -> usize
pub fn max_token_limit(&self) -> usize
返回当前配置的 token 预算。
Sourcepub fn last_summary_error(&self) -> Option<&str>
pub fn last_summary_error(&self) -> Option<&str>
P2-4: 最近一次摘要失败的原因(无失败则 None)。
Trait Implementations§
Source§impl<M: BaseChatModel + Send + Sync + 'static> BaseChatMemory for ConversationSummaryBufferMemory<M>
P0-1: ConversationSummaryBufferMemory 实现 BaseChatMemory。
impl<M: BaseChatModel + Send + Sync + 'static> BaseChatMemory for ConversationSummaryBufferMemory<M>
P0-1: ConversationSummaryBufferMemory 实现 BaseChatMemory。
Source§impl<M: BaseChatModel + Send + Sync + 'static> BaseMemory for ConversationSummaryBufferMemory<M>
impl<M: BaseChatModel + Send + Sync + 'static> BaseMemory for ConversationSummaryBufferMemory<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> !RefUnwindSafe for ConversationSummaryBufferMemory<M>
impl<M> !UnwindSafe for ConversationSummaryBufferMemory<M>
impl<M> Freeze for ConversationSummaryBufferMemory<M>where
M: Freeze,
impl<M> Send for ConversationSummaryBufferMemory<M>
impl<M> Sync for ConversationSummaryBufferMemory<M>
impl<M> Unpin for ConversationSummaryBufferMemory<M>where
M: Unpin,
impl<M> UnsafeUnpin for ConversationSummaryBufferMemory<M>where
M: 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
Mutably borrows from an owned value. Read more