当前位置: 首页 > news >正文

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

完结!

http://www.lryc.cn/news/97626.html

相关文章:

  • 电流源电路
  • iOS开发-CMMotionManager传感器陀螺仪
  • 影刀下载,插件安装
  • Linux的tcpdump命令详解
  • springboot运行报错Failed to load ApplicationContext for xxx
  • [SQL挖掘机] - 内连接: inner join
  • mysql(四)数据备份
  • Spring 拦截器
  • 【libevent】http客户端3:简单封装
  • JavaScript的函数中this的指向
  • Caddy 中实现自动 HTTPS
  • SK5代理(socks5代理)在网络安全与爬虫应用中的优势与编写指南
  • 【LeetCode-简单】剑指 Offer 06. 从尾到头打印链表(详解)
  • 【LeetCode】114.二叉树展开为链表
  • DAY3,Qt(完成闹钟的实现,定时器事件处理函数的使用)
  • TL-ER3220G设置vlan
  • PHPWord 实现合并多个word文件
  • rust持续学习Box::leak
  • 通过SSH实现将本地端口反向代理到公网服务器
  • Fragment的基本用法、Fragment和活动间的通信、Fragment的生命周期、动态加载布局的技巧
  • 机器学习 day30(正则化参数λ对模型的影响)
  • 图文教程:如何在 3DS Max 中创建3D迷你卡通房屋
  • 根据UIL下载图片/视频、根据URL自动下载图片/视频、GUI自动下载想要的图片
  • HTML <picture> 标签
  • 力扣天天练--week3-LeetCode75
  • 5.2 方法的定义和调用
  • Linux基础以及常用命令
  • echarts 折线图上只显示某一个点值
  • 1、传统锁回顾(Jvm本地锁,MySQL悲观锁、乐观锁)
  • 【Java||牛客】DFS应用迷宫问题