Skip to main content

ConversationSummaryBufferMemory

ConversationSummaryBufferMemory结合了前两个想法。它在内存中保留了最近的交互缓冲区,但不仅仅是完全清除旧的交互,而是将它们编译成摘要并同时使用。与先前的实现不同的是,它使用令牌长度而不是交互次数来确定何时清除交互。

让我们首先了解如何使用这些工具

from langchain.memory import ConversationSummaryBufferMemory  
from langchain.llms import OpenAI

llm = OpenAI()

API 参考:

memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=10)  
memory.save_context({"input": "hi"}, {"output": "whats up"})
memory.save_context({"input": "not much you"}, {"output": "not much"})
memory.load_memory_variables({})  

{'history': 'System: \nThe human says "hi", and the AI responds with "whats up".\nHuman: not much you\nAI: not much'}

我们还可以将历史记录作为消息列表获取(如果您正在与聊天模型一起使用,这将非常有用)。

memory = ConversationSummaryBufferMemory(  
llm=llm, max_token_limit=10, return_messages=True
)
memory.save_context({"input": "hi"}, {"output": "whats up"})
memory.save_context({"input": "not much you"}, {"output": "not much"})

我们还可以直接使用predict_new_summary方法。

messages = memory.chat_memory.messages  
previous_summary = ""
memory.predict_new_summary(messages, previous_summary)

'\nThe human and AI state that they are not doing much.'

在链中使用​

让我们通过一个示例来了解,再次设置verbose=True以便查看提示。

from langchain.chains import ConversationChain  

conversation_with_summary = ConversationChain(
llm=llm,
# We set a very low max_token_limit for the purposes of testing.
memory=ConversationSummaryBufferMemory(llm=OpenAI(), max_token_limit=40),
verbose=True,
)
conversation_with_summary.predict(input="Hi, what's up?")

API 参考:

> 进入新的 ConversationChain 链...
格式化后的提示:
以下是人类和 AI 之间友好对话的示例。AI 健谈并提供了许多来自其上下文的具体细节。如果 AI 不知道问题的答案,它会真实地说自己不知道。
当前对话:
人类:Hi, what's up?
AI:

> 链结束。
" Hi there! I'm doing great. I'm learning about the latest advances in artificial intelligence. What about you?"
conversation_with_summary.predict(input="Just working on writing some documentation!")  
> 进入新的 ConversationChain 链...
格式化后的提示:
以下是人类和 AI 之间友好对话的示例。AI 健谈并提供了许多来自其上下文的具体细节。如果 AI 不知道问题的答案,它会真实地说自己不知道。
当前对话:
人类:Hi, what's up?
AI: Hi there! I'm doing great. I'm spending some time learning about the latest developments in AI technology. How about you?
人类: Just working on writing some documentation!
AI:

> 链结束。
' That sounds like a great use of your time. Do you have experience with writing documentation?'

我们可以看到这里有对话的摘要,然后是一些先前的交互。

conversation_with_summary.predict(input="For LangChain! Have you heard of it?")  
> 进入新的 ConversationChain 链...
格式化后的提示:
以下是人类和 AI 之间友好对话的示例。AI 健谈并提供了许多来自其上下文的具体细节。如果 AI 不知道问题的答案,它会真实地说自己不知道。
当前对话:
系统:
人类问 AI 它在做什么,AI 回答说它正在学习最新的 AI 技术发展。
人类:Just working on writing some documentation!
AI: That sounds like a great use of your time. Do you have experience with writing documentation?
人类:For LangChain! Have you heard of it?
AI:

> 链结束。
" No, I haven't heard of LangChain. Can you tell me more about it?"

我们可以看到这里有摘要和缓冲区的更新。

conversation_with_summary.predict(  
input="Haha nope, although a lot of people confuse it for that"
)
> 进入新的 ConversationChain 链...
格式化后的提示:
以下是人类和 AI 之间友好对话的示例。AI 健谈并提供了许多来自其上下文的具体细节。如果 AI 不知道问题的答案,它会真实地说自己不知道。
当前对话:
系统:
人类问 AI 它在做什么,AI 回答说它正在学习最新的 AI 技术发展。人类随后提到他们正在写文档,AI 回答说这听起来是他们时间的很好利用,并问他们是否有写文档的经验。
人类:For LangChain! Have you heard of it?
AI: No, I haven't heard of LangChain. Can you tell me more about it?
人类:Haha nope, although a lot of people confuse it for that
AI:

> 链结束。
' Oh, okay. What is LangChain?'