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."

完结!

相关推荐
都叫我大帅哥19 小时前
LangChain与PDF的奇妙之旅:从文档加载到智能问答的全面指南
python·langchain
没有昵称28521 小时前
五、(基础)别让大模型 “断网”!教你用 LangChain 开发联网 AI Agent,实操教程来了
langchain·ai编程
_一条咸鱼_2 天前
LangChain正则表达式(19)
人工智能·面试·langchain
前端双越老师2 天前
使用 langChain.js 实现 RAG 知识库语义搜索
人工智能·langchain·node.js
都叫我大帅哥2 天前
LangChain的TXT文档加载:从入门到实战的终极指南
python·langchain
聚客AI4 天前
🔥 大模型开发进阶:基于LangChain的异步流式响应与性能优化
人工智能·langchain·agent
软件测试君4 天前
向量数据库 Chroma 和 Milvus的使用
langchain·aigc·openai
成都犀牛4 天前
LangChain 内存(Memory)
人工智能·机器学习·langchain
FLYINGPIG5 天前
【Langchain】超详细构建Langchain架构下的MCP与Agent智能结合体
langchain·llm
_一条咸鱼_5 天前
LangChain多模态提示词设计探索的源码级深度剖析(16)
人工智能·面试·langchain