ConversationTokenBufferMemory
ConversationTokenBufferMemory
在内存中保留最近交互的缓冲区,并使用令牌长度而不是交互次数来确定何时刷新交互。
首先让我们看一下如何使用这些工具
from langchain.memory import ConversationTokenBufferMemory
from langchain.llms import OpenAI
llm = OpenAI()
API 参考:
- ConversationTokenBufferMemory 来自
langchain.memory
- OpenAI 来自
langchain.llms
memory = ConversationTokenBufferMemory(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': 'Human: not much you\nAI: not much'}
我们还可以将历史记录作为消息列表获取(如果您正在与聊天模型一起使用,这将非常有用)。
memory = ConversationTokenBufferMemory(
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"})
在链中使用
让我们通过一个示例来说明,再次设置 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=ConversationTokenBufferMemory(llm=OpenAI(), max_token_limit=60),
verbose=True,
)
conversation_with_summary.predict(input="Hi, what's up?")
API 参考:
- ConversationChain 来自
langchain.chains
> 进入新的 ConversationChain 链...
格式化后的提示:
以下是人类和 AI 之间友好对话的示例。AI 健谈并提供了来自其上下文的许多具体细节。如果 AI 不知道问题的答案,它会真实地说出自己不知道。
当前对话:
Human: Hi, what's up?
AI:
> 链结束。
" Hi there! I'm doing great, just enjoying the day. How about you?"
conversation_with_summary.predict(input="Just working on writing some documentation!")
> 进入新的 ConversationChain 链...
格式化后的提示:
以下是人类和 AI 之间友好对话的示例。AI 健谈并提供了来自其上下文的许多具体细节。如果 AI 不知道问题的答案,它会真实地说出自己不知道。
当前对话:
Human: Hi, what's up?
AI: Hi there! I'm doing great, just enjoying the day. How about you?
Human: Just working on writing some documentation!
AI:
> 链结束。
' Sounds like a productive day! What kind of documentation are you writing?'
conversation_with_summary.predict(input="For LangChain! Have you heard of it?")
> 进入新的 ConversationChain 链...
格式化后的提示:
以下是人类和 AI 之间友好对话的示例。AI 健谈并提供了来自其上下文的许多具体细节。如果 AI 不知道问题的答案,它会真实地说出自己不知道。
当前对话:
Human: Hi, what's up?
AI: Hi there! I'm doing great, just enjoying the day. How about you?
Human: Just working on writing some documentation!
AI: Sounds like a productive day! What kind of documentation are you writing?
Human: For LangChain! Have you heard of it?
AI:
> 链结束。
" Yes, I have heard of LangChain! It is a decentralized language-learning platform that connects native speakers and learners in real time. Is that the documentation you're writing about?"
# We can see here that the buffer is updated
conversation_with_summary.predict(
input="Haha nope, although a lot of people confuse it for that"
)
> 进入新的 ConversationChain 链...
格式化后的提示:
以下是人类和 AI 之间友好对话的示例。AI 健谈并提供了来自其上下文的许多具体细节。如果 AI 不知道问题的答案,它会真实地说出自己不知道。
当前对话:
Human: For LangChain! Have you heard of it?
AI: Yes, I have heard of LangChain! It is a decentralized language-learning platform that connects native speakers and learners in real time. Is that the documentation you're writing about?
Human: Haha nope, although a lot of people confuse it for that
AI:
> 链结束。
" Oh, I see. Is there another language learning platform you're referring to?"