网站怎么做移动的窗口,天津网站建设开发维护,宁波外贸行业现状,黄冈建设工程信息网Langchain 的 Conversation buffer window memory ConversationBufferWindowMemory 保存一段时间内对话交互的列表。它仅使用最后 K 个交互。这对于保持最近交互的滑动窗口非常有用#xff0c;因此缓冲区不会变得太大。
我们首先来探讨一下这种存储器的基本功能。
示例代码因此缓冲区不会变得太大。
我们首先来探讨一下这种存储器的基本功能。
示例代码
from langchain.memory import ConversationBufferWindowMemorymemory ConversationBufferWindowMemory( k1)
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 ConversationBufferWindowMemory( k1, return_messagesTrue)
memory.save_context({input: hi}, {output: whats up})
memory.save_context({input: not much you}, {output: not much})memory.load_memory_variables({})输出结果 {history: [HumanMessage(contentnot much you, additional_kwargs{}),AIMessage(contentnot much, additional_kwargs{})]}Using in a chain
让我们看一下示例再次设置 verboseTrue 以便我们可以看到提示。
from langchain.llms import OpenAI
from langchain.chains import ConversationChain
conversation_with_summary ConversationChain(llmOpenAI(temperature0), # We set a low k2, to only keep the last 2 interactions in memorymemoryConversationBufferWindowMemory(k2), verboseTrue
)
conversation_with_summary.predict(inputHi, whats up?)输出结果 Entering new ConversationChain chain...Prompt after formatting:The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.Current conversation:Human: Hi, whats up?AI: Finished chain. Hi there! Im doing great. Im currently helping a customer with a technical issue. How about you?示例代码
conversation_with_summary.predict(inputWhats their issues?)输出结果 Entering new ConversationChain chain...Prompt after formatting:The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.Current conversation:Human: Hi, whats up?AI: Hi there! Im doing great. Im currently helping a customer with a technical issue. How about you?Human: Whats their issues?AI: Finished chain. The customer is having trouble connecting to their Wi-Fi network. Im helping them troubleshoot the issue and get them connected.示例代码
conversation_with_summary.predict(inputIs it going well?)输出结果 Entering new ConversationChain chain...Prompt after formatting:The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.Current conversation:Human: Hi, whats up?AI: Hi there! Im doing great. Im currently helping a customer with a technical issue. How about you?Human: Whats their issues?AI: The customer is having trouble connecting to their Wi-Fi network. Im helping them troubleshoot the issue and get them connected.Human: Is it going well?AI: Finished chain. Yes, its going well so far. Weve already identified the problem and are now working on a solution.示例代码
# Notice here that the first interaction does not appear.
conversation_with_summary.predict(inputWhats the solution?)输出结果 Entering new ConversationChain chain...Prompt after formatting:The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.Current conversation:Human: Whats their issues?AI: The customer is having trouble connecting to their Wi-Fi network. Im helping them troubleshoot the issue and get them connected.Human: Is it going well?AI: Yes, its going well so far. Weve already identified the problem and are now working on a solution.Human: Whats the solution?AI: Finished chain. The solution is to reset the router and reconfigure the settings. Were currently in the process of doing that.完结