当前位置: 首页 > 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."

完结!

相关文章:

Langchain 的 Conversation buffer window memory

Langchain 的 Conversation buffer window memory ConversationBufferWindowMemory 保存一段时间内对话交互的列表。它仅使用最后 K 个交互。这对于保持最近交互的滑动窗口非常有用,因此缓冲区不会变得太大。 我们首先来探讨一下这种存储器的基本功能。 示例代码&…...

电流源电路

3.3.3电流源电路 镜像电流源 电路 分析 仿真 比例电流源 电路 分析 仿真 加射极输出器的电流源1 电路 分析 仿真 加射极输出器的电流源2 电路 分析 仿真 威尔逊电流源 电路 分析 仿真...

iOS开发-CMMotionManager传感器陀螺仪

iOS开发-CMMotionManager传感器陀螺仪 之前开发中遇到需要使用陀螺仪判断是否拍照时候水平判断,如果没有水平拍照,则给出提示。方便用户拍照合适的题目图片。 一、CMMotionManager CMMotionManager是什么 CMMotionManager 是 Core Motion 库的核心类&…...

影刀下载,插件安装

1、下载 在影刀官网下载:www.yingdao.com 2、谷歌插件安装 参考: 影刀插件安装各种方式 浏览器安装插件说明 - 影刀帮助中心 安装说明:驱动外置 Chrome 需要安装插件,并且保证此插件处于开启状态 方式一:用户头…...

Linux的tcpdump命令详解

tcpdump 一款sniffer工具,是Linux上的抓包工具,嗅探器 补充说明 tcpdump命令 是一款抓包,嗅探器工具,它可以打印所有经过网络接口的数据包的头信息,也可以使用-w选项将数据包保存到文件中,方便以后分析。…...

springboot运行报错Failed to load ApplicationContext for xxx

Failed to load ApplicationContext for报错解决方法 报错Failed to load ApplicationContext for 报错Failed to load ApplicationContext for 网上找了一堆方法都尝试了还是没用 包括添加mapperScan,添加配置类 配置pom文件 [外链图片转存失败,源站可能有防盗链机…...

[SQL挖掘机] - 内连接: inner join

介绍: 内连接是一种多表连接方式,用于将两个或多个表中的数据通过共同的列值进行匹配,并返回满足连接条件的匹配行。简单来说,内连接能够将相关联的数据组合在一起,以便进行更复杂和全面的数据分析。 内连接的工作原理如下&…...

mysql(四)数据备份

目录 前言 一、概述 二、备份的类型 (一)物理与逻辑角度 (二)数据库备份策略角度 三、常见的备份方法 四、完整备份 (一)打包数据库文件备份 (二)备份工具备份 五、增量备份 六、操…...

Spring 拦截器

上篇博客链接:SpringAOP详解 上篇博客我们提到使用AOP的环绕通知来完成统一的用户登陆验证虽然方便了许多,但随之而来也带来了新的问题: HttpSession不知道如何去获取,获取困难登录和注册的方法并不需要拦截,使用切点没办法定义哪…...

【libevent】http客户端3:简单封装

LibEventHttp: 适用于简单的http请求 LibEventHttp/* Copyright (c) MediaArea.net SARL. All Rights Reserved.** Use of this source code is governed by a BSD-style license that can* be found in the License.html file in the root of the source tree.*///--------…...

JavaScript的函数中this的指向

JavaScript的函数中this的指向 JavaScript 语言之所以有 this 的设计,跟内存里面的数据结构有关系。 以下例子来简单描述this在不同情况下所指向的对象。 var obj {aa: function(){console.log(this.num)},num: 5 };var aa obj.aa; var num 10;obj.aa(); // …...

Caddy 中实现自动 HTTPS

要在 Caddy 中实现自动 HTTPS,您可以按照以下步骤进行操作: 步骤 1:安装 Caddy 首先,您需要安装 Caddy 服务器。您可以从 Caddy 的官方网站(https://caddyserver.com/)下载适用于您的操作系统的最新版本。…...

SK5代理(socks5代理)在网络安全与爬虫应用中的优势与编写指南

一、SK5代理(socks5代理)的基本概念 SK5代理是一种网络代理协议,它允许客户端通过代理服务器与目标服务器进行通信。相较于HTTP代理,SK5代理在传输数据时更加高效且安全,它支持TCP和UDP协议,并且能够实现数…...

【LeetCode-简单】剑指 Offer 06. 从尾到头打印链表(详解)

题目 输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。 题目地址:剑指 Offer 06. 从尾到头打印链表 - 力扣(LeetCode) 方法1:栈 思路 题目要求我们将链表的从尾到投打印一…...

【LeetCode】114.二叉树展开为链表

题目 给你二叉树的根结点 root ,请你将它展开为一个单链表: 展开后的单链表应该同样使用 TreeNode ,其中 right 子指针指向链表中下一个结点,而左子指针始终为 null 。展开后的单链表应该与二叉树 先序遍历 顺序相同。 示例 1&…...

DAY3,Qt(完成闹钟的实现,定时器事件处理函数的使用)

1.完成闹钟的实现&#xff0c;到点播报文本框的内容&#xff1b; ---alarm.h---头文件 #ifndef ALARM_H #define ALARM_H#include <QWidget> #include <QTimerEvent> //定时器处理函数类 #include <QTime> //时间类 #include <QPushButton> //按钮…...

TL-ER3220G设置vlan

TL-ER3220G是企业宽带路由器。 自带5个RJ45接口。 其中接口1到接口4都可以接入宽带线路。最多可以并接4路。 本例由接口1接入宽带&#xff0c;默认接口2到接口4组成1个vlan&#xff0c;名称vlan。其中接口5特殊&#xff0c;带宽最大100M。 计划将接口2和接口4组成第一个vlan&…...

PHPWord 实现合并多个word文件

PHPWord 本来想着当调包侠呢&#xff0c;结果翻了一遍文档&#xff0c;没有这种操作支持&#xff0c;阿这&#x1f602; GPT 不出意外的一顿胡扯&#xff0c;给&#x1f468;‍&#x1f9b3;气的要中风啦 思路 word 也就是docx结尾的文件本质上就是xml字符串&#xff0c; …...

rust持续学习Box::leak

Box就是unique_ptr 这个函数的功能是消费box返回一个全局变量&#xff01; 写一个函数&#xff0c;想要真的返回全局变量&#xff0c;感觉用这个是个好的做法 fn Foo()->Option<&static mut A> {let a Box::new(A());Some(Box::leak(a)) }这样就能当真拿到这个全…...

通过SSH实现将本地端口反向代理到公网服务器

使用场景 有一台公网服务器&#xff0c;能够对外开放服务进行访问&#xff0c;但是这个公网服务器资源较低&#xff0c;无法运行太多服务 有一台闲置电脑可以全天候开机使用&#xff0c;且配置较好&#xff0c;可以部署多个服务&#xff0c;但是没有公网IP 需求&#xff1a;将…...

Fragment的基本用法、Fragment和活动间的通信、Fragment的生命周期、动态加载布局的技巧

一、Fragment的简单用法 1、制作Fragment 1.1 新建一个布局文件left_fragment.xml <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"android:orientation"ve…...

机器学习 day30(正则化参数λ对模型的影响)

λ对Jcv和Jtrain的影响 假设该模型为四阶多项式当λ很大时&#xff0c;在最小化J的过程中&#xff0c;w会很小且接近0&#xff0c;此时模型f(x)近似于一个常数&#xff0c;所以此时模型欠拟合&#xff0c;Jtrain和Jcv都很大当λ很小时&#xff0c;表示模型几乎没有正则化&…...

图文教程:如何在 3DS Max 中创建3D迷你卡通房屋

推荐&#xff1a; NSDT场景编辑器助你快速搭建可二次开发的3D应用场景 在本教程中&#xff0c;我们将学习如何创建一个有趣的、低多边形的迷你动画房子&#xff0c;你可以在自己的插图或视频游戏项目中使用它。您将学习的一些技能将包括创建基本的3D形状和基本的建模技术。让我…...

根据UIL下载图片/视频、根据URL自动下载图片/视频、GUI自动下载想要的图片

目录 1&#xff0c;根据UIL下载图片/视频 2&#xff0c;根据URL自动下载图片/视频 3、GUI自动下载想要的图片 ①点击下载按钮&#xff0c;进行挨个下载 ②右击保存进行下载图片 4、图片或视频URL批量放入浏览器页面上 1&#xff0c;根据UIL下载图片/视频 def downForInter…...

HTML <picture> 标签

实例 如何使用 <picture> 标签: <picture><source media="(min-width:650px)" srcset="/i/photo/flower-4.jpg"><source media="(min-width:465px)" srcset="/i/photo/tulip.jpg"><img src="/i/ph…...

力扣天天练--week3-LeetCode75

topic75-9-t443:压缩字符串 题目描述&#xff1a; 给你一个字符数组 chars &#xff0c;请使用下述算法压缩&#xff1a; 从一个空字符串 s 开始。对于 chars 中的每组 连续重复字符 &#xff1a; 如果这一组长度为 1 &#xff0c;则将字符追加到 s 中。 否则&#xff0c;需…...

5.2 方法的定义和调用

5.2 方法的定义和调用 Java的方法类似于其他语言的函数&#xff0c;是一段用来完成特定功能的代码片段&#xff0c;一般情况下&#xff0c;定义一个方法包含以下语法&#xff1a; 一、方法的定义 方法包含一个方法头和一个方法体 修饰符 返回值类型 方法名 &#xff08;参数类…...

Linux基础以及常用命令

目录 1 Linux简介1.1 不同应用领域的主流操作系统1.2 Linux系统版本1.3 Linux安装1.3.1 安装VMWare1.3.2 安装CentOS镜像1.3.3 网卡设置1.3.4 安装SSH连接工具1.3.5 Linux和Windows目录结构对比 2 Linux常用命令2.0 常用命令&#xff08;ls&#xff0c;pwd&#xff0c;cd&#…...

echarts 折线图上只显示某一个点值

<template> <div> <!-- 数据来源 --> <div class"echarts" ref"echartsRef"></div> </div> </template> <script setup langts name"reconciled"> import { ref } from "vue"; im…...

1、传统锁回顾(Jvm本地锁,MySQL悲观锁、乐观锁)

目录 1.1 从减库存聊起1.2 环境准备1.3 简单实现减库存1.4 演示超卖现象1.5 jvm锁1.6 三种情况导致Jvm本地锁失效1、多例模式下&#xff0c;Jvm本地锁失效2、Spring的事务导致Jvm本地锁失效3、集群部署导致Jvm本地锁失效 1.7 mysql锁演示1.7.1、一个sql1.7.2、悲观锁1.7.3、乐观…...