Langchain 的 Conversation buffer window memory

Langchain 的 Conversation buffer window memory

ConversationBufferWindowMemory 保存一段时间内对话交互的列表。它仅使用最后 K 个交互。这对于保持最近交互的滑动窗口非常有用,因此缓冲区不会变得太大。

我们首先来探讨一下这种存储器的基本功能。

示例代码,

from langchain.memory import ConversationBufferWindowMemory

memory = ConversationBufferWindowMemory( k=1)
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( k=1, return_messages=True)
memory.save_context({"input": "hi"}, {"output": "whats up"})
memory.save_context({"input": "not much you"}, {"output": "not much"})

memory.load_memory_variables({})

输出结果,

    {'history': [HumanMessage(content='not much you', additional_kwargs={}),
      AIMessage(content='not much', additional_kwargs={})]}

Using in a chain

让我们看一下示例,再次设置 verbose=True 以便我们可以看到提示。

from langchain.llms import OpenAI
from langchain.chains import ConversationChain
conversation_with_summary = ConversationChain(
    llm=OpenAI(temperature=0), 
    # We set a low k=2, to only keep the last 2 interactions in memory
    memory=ConversationBufferWindowMemory(k=2), 
    verbose=True
)
conversation_with_summary.predict(input="Hi, what's 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, what's up?
    AI:
    
    > Finished chain.





    " Hi there! I'm doing great. I'm currently helping a customer with a technical issue. How about you?"

示例代码,

conversation_with_summary.predict(input="What's 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, what's up?
    AI:  Hi there! I'm doing great. I'm currently helping a customer with a technical issue. How about you?
    Human: What's their issues?
    AI:
    
    > Finished chain.





    " The customer is having trouble connecting to their Wi-Fi network. I'm helping them troubleshoot the issue and get them connected."

示例代码,

conversation_with_summary.predict(input="Is 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, what's up?
    AI:  Hi there! I'm doing great. I'm currently helping a customer with a technical issue. How about you?
    Human: What's their issues?
    AI:  The customer is having trouble connecting to their Wi-Fi network. I'm helping them troubleshoot the issue and get them connected.
    Human: Is it going well?
    AI:
    
    > Finished chain.





    " Yes, it's going well so far. We've already identified the problem and are now working on a solution."

示例代码,

# Notice here that the first interaction does not appear.
conversation_with_summary.predict(input="What's 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: What's their issues?
    AI:  The customer is having trouble connecting to their Wi-Fi network. I'm helping them troubleshoot the issue and get them connected.
    Human: Is it going well?
    AI:  Yes, it's going well so far. We've already identified the problem and are now working on a solution.
    Human: What's the solution?
    AI:
    
    > Finished chain.





    " The solution is to reset the router and reconfigure the settings. We're currently in the process of doing that."

完结!

相关推荐
ZHOU_WUYI12 小时前
3.langchain中的prompt模板 (few shot examples in chat models)
人工智能·langchain·prompt
AI_小站17 小时前
RAG 示例:使用 langchain、Redis、llama.cpp 构建一个 kubernetes 知识库问答
人工智能·程序人生·langchain·kubernetes·llama·知识库·rag
ZHOU_WUYI2 天前
5.tree of thought 源码 (prompts 类)
langchain
waiting不是违停3 天前
MetaGPT实现多动作Agent
langchain·llm
ZHOU_WUYI6 天前
2. langgraph中的react agent使用 (在react agent添加历史消息)
人工智能·langchain
ZHOU_WUYI6 天前
4. langgraph中的react agent使用 (在react agent添加人机交互)
人工智能·langchain
ZHOU_WUYI6 天前
5. langgraph中的react agent使用 (从零构建一个react agent)
人工智能·langchain
ZHOU_WUYI6 天前
3. langgraph中的react agent使用 (在react agent添加系统提示)
人工智能·langchain
三月七(爱看动漫的程序员)6 天前
LM2 : A Simple Society of Language Models Solves Complex Reasoning
人工智能·gpt·语言模型·自然语言处理·chatgpt·langchain·1024程序员节
DbWong_09187 天前
langchain_chatchat+ollama部署本地知识库,联网查询以及对数据库(Oracle)数据进行查询
python·oracle·langchain·ollama