Langchain 的 Conversation summary memory
Langchain 的 Conversation summary memory
现在让我们看一下使用稍微复杂的内存类型 - ConversationSummaryMemory 。这种类型的记忆会随着时间的推移创建对话的摘要。这对于随着时间的推移压缩对话中的信息非常有用。对话摘要内存对发生的对话进行总结,并将当前摘要存储在内存中。然后可以使用该内存将迄今为止的对话摘要注入提示/链中。此内存对于较长的对话最有用,因为在提示中逐字保留过去的消息历史记录会占用太多令牌。
我们首先来探讨一下这种存储器的基本功能。
示例代码,
from langchain.memory import ConversationSummaryMemory, ChatMessageHistory
from langchain.llms import OpenAI
memory = ConversationSummaryMemory(llm=OpenAI(temperature=0))
memory.save_context({"input": "hi"}, {"output": "whats up"})
memory.load_memory_variables({})
输出结果,
{'history': '\nThe human greets the AI, to which the AI responds.'}
我们还可以获取历史记录作为消息列表(如果您将其与聊天模型一起使用,这非常有用)。
memory = ConversationSummaryMemory(llm=OpenAI(temperature=0), return_messages=True)
memory.save_context({"input": "hi"}, {"output": "whats up"})
memory.load_memory_variables({})
输出结果,
{'history': [SystemMessage(content='\nThe human greets the AI, to which the AI responds.', additional_kwargs={})]}
我们也可以直接使用 predict_new_summary 方法。
messages = memory.chat_memory.messages
previous_summary = ""
memory.predict_new_summary(messages, previous_summary)
输出结果,
'\nThe human greets the AI, to which the AI responds.'
Initializing with messages
如果您有此类之外的消息,您可以使用 ChatMessageHistory 轻松初始化该类。加载期间,将计算摘要。
示例代码,
history = ChatMessageHistory()
history.add_user_message("hi")
history.add_ai_message("hi there!")
memory = ConversationSummaryMemory.from_messages(llm=OpenAI(temperature=0), chat_memory=history, return_messages=True)
memory.buffer
输出结果,
'\nThe human greets the AI, to which the AI responds with a friendly greeting.'
Using in a chain
让我们看一下在链中使用它的示例,再次设置 verbose=True 以便我们可以看到提示。
示例代码,
from langchain.llms import OpenAI
from langchain.chains import ConversationChain
llm = OpenAI(temperature=0)
conversation_with_summary = ConversationChain(llm=llm, memory=ConversationSummaryMemory(llm=OpenAI()),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="Tell me more about it!")
输出结果,
> 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:The human greeted the AI and asked how it was doing. The AI replied that it was doing great and was currently helping a customer with a technical issue.Human: Tell me more about it!AI:> Finished chain." Sure! The customer is having trouble with their computer not connecting to the internet. I'm helping them troubleshoot the issue and figure out what the problem is. So far, we've tried resetting the router and checking the network settings, but the issue still persists. We're currently looking into other possible solutions."
示例代码,
conversation_with_summary.predict(input="Very cool -- what is the scope of the project?")
输出结果,
> 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:The human greeted the AI and asked how it was doing. The AI replied that it was doing great and was currently helping a customer with a technical issue where their computer was not connecting to the internet. The AI was troubleshooting the issue and had already tried resetting the router and checking the network settings, but the issue still persisted and they were looking into other possible solutions.Human: Very cool -- what is the scope of the project?AI:> Finished chain." The scope of the project is to troubleshoot the customer's computer issue and find a solution that will allow them to connect to the internet. We are currently exploring different possibilities and have already tried resetting the router and checking the network settings, but the issue still persists."
完结!
相关文章:
Langchain 的 Conversation summary memory
Langchain 的 Conversation summary memory 现在让我们看一下使用稍微复杂的内存类型 - ConversationSummaryMemory 。这种类型的记忆会随着时间的推移创建对话的摘要。这对于随着时间的推移压缩对话中的信息非常有用。对话摘要内存对发生的对话进行总结,并将当前摘…...
Safari 查看 http 请求
文章目录 1、开启 Safari 开发菜单2、显示 JavaScript 控制台 1、开启 Safari 开发菜单 Safari 设置中,打开开发菜单选项 *** 选择完成后,Safari 的目录栏就会出现一个 开发 功能。 2、显示 JavaScript 控制台 开启页面后,在开发中选中 显…...
kafka权限控制功能
参考链接: https://www.clougence.com/cc-doc/dataMigrationAndSync/database/privs_for_kafka Kafka需要的权限 | CloudCanal of ClouGence Kafka Topic 权限控制可以通过使用 Apache Kafka 的内置安全特性来实现。这主要涉及到两个方面:认证&#…...
公司内部重要文件如何加密防止泄露?
现如今,是互联网时代,数据安全在互联网时代中的数据安全岌岌可危,企业中,都会拥有终端,终端中每天都要处理文档,文件,表格,产生一系列的数据问题等,这个时候就要先企业中…...
C语言或Java-x型图案
描述 KiKi学习了循环,BoBo老师给他出了一系列打印图案的练习,该任务是打印用“*”组成的X形图案。 输入描述 多组输入,一个整数(2~20),表示输出的行数,也表示组成“X”的反斜线和正斜线的长度。…...
FTP客户端登录报错:Login failed
FTP客户端登录报错:Login failed 是selinux的问题,一般用户无法登录用户主目录 [rootchenshuyi ~]# setsebool -P tftp_home_dir 1...
Linux相关指令(上)
常见指令: 1 pwd:查看用户当前所在目录 以下面的路径为例: 2 ls:对于目录,该命令列出该目录下的所有子目录与文件。 对于文件,将列出文件名以及其他信息 ls-l(or ll):列…...
电压放大器在管道缺陷检测中应用有哪些
管道是一种重要的输送工业介质的设施,广泛应用于石油、化工、水利等领域。然而,由于长期使用和外界环境因素等原因,管道内部常会出现各种缺陷和损伤,如腐蚀、裂纹、磨损等,这些问题如果得不到及时发现和修复࿰…...
NLP(六十二)HuggingFace中的Datasets使用
Datasets库是HuggingFace生态系统中一个重要的数据集库,可用于轻松地访问和共享数据集,这些数据集是关于音频、计算机视觉、以及自然语言处理等领域。Datasets 库可以通过一行来加载一个数据集,并且可以使用 Hugging Face 强大的数据处理方法…...
Windows下基于VSCode搭建C++开发环境(包含整合MinGW64、CMake的详细流程)
最近想写写C,装了VisualStudio 2022,折腾半天。对于一个用惯VSCode的人来说,总感觉IDE太笨重。于是自己网上各种查资料,自己琢磨,搭建了一套Windows下基于VSCode和CMake的C轻量级开发环境。 具体搭建步骤 1. 下载并安…...
springboot+mybatis-plus+vue+element+vant2实现短视频网站,模拟西瓜视频移动端
目录 一、前言 二、管理后台 1.登录 2.登录成功,进入欢迎页 编辑 3.视频分类管理 4. 视频标签管理 5.视频管理 6.评论管理 编辑 7.用户管理 8.字典管理 (类似于后端的枚举) 9.参数管理(富文本录入) 10.管…...
MySQL学习-第二部分
文章目录 MySQL数据库学习1 表1.1 表中的数据类型1.2 表的创建1.3 表的删除1.4 default设置字段默认值1.5 表结构的修改1.5.1 表名的修改1.5.2 字段名的修改1.5.3 修改字段类型1.5.4 添加字段1.5.5 删除字段1.5.6 表的复制 1.6 表的约束1.6.1 什么是约束?1.6.2 not …...
TortoiseGit 入门指南17:使用子模块
如果你想在自己的代码仓库中嵌入其它仓库,这称为引入子模块(Submodule)。使用右键菜单TortoiseGit - Submodules Add 选项,弹出添加子模块对话框,可以将一个外部仓库嵌入到源代码树的专用子目录中。 Repository&#x…...
谷粒商城篇章5 ---- P173-P192 ---- 检索服务【分布式高级篇二】
目录 1 检索服务 1.1 搭建页面环境 1.1.1 引入依赖 1.1.2 将检索页面放到gulimall-search的src/main/resources/templates/目录下 1.1.3 调整搜索页面 1.1.4 将静态资源放到linux的nginx相关映射目录下/root/docker/nginx/html/static/ search/ 1.1.5 SwitchHosts配置域…...
N位分频器的实现
N位分频器的实现 一、 目的 使用verilog实现n位的分频器,可以是偶数,也可以是奇数 二、 原理 FPGA中n位分频器的工作原理可以简要概括为: 分频器的作用是将输入时钟频率分频,输出低于输入时钟频率的时钟信号。n位分频器可以将输入时钟频率分频2^n倍…...
华为OD真题--分苹果-带答案
有A,B两个同学想要分苹果。A的想法是使用二进制进行,1 1相加不进一位,如(9 5 1001 101 12)。B同学的想法是使用十进制进行,并且进一位。会输入两组数据,一组是苹果总数,一组分别…...
【前端实习评审】对小说详情模块更新的后端接口压力流程进行了人群优化
大家好,本篇文章分享一下【校招VIP】免费商业项目“推推”第一期书籍详情模块 前端同学的开发文档周最佳作品。该同学来自安徽科技学院土木工程专业。本项目亮点难点: 1.热门书籍在更新点的访问压力; 2.书籍更新通知的及时性和有效性…...
Factorization Machines(论文笔记)
样例一: 一个简单的例子,train是一个字典,先将train进行“one-hot” coding,然后输入相关特征向量,可以预测相关性。 from pyfm import pylibfm from sklearn.feature_extraction import DictVectorizer import numpy as np tra…...
Qt开发(5)——使用QTimer定时触发槽函数
实现效果 软件启动之后,开始计时,到达预定时间后,调用其他类的某个函数。 类的分工 BaseType:软件初始化的调用类 FuncType: 功能函数所在类 具体函数 // FuncType.h class FuncType: public QObject {Q_OBJECT public: publ…...
2023年JAVA最新面试题
2023年JAVA最新面试题 1 JavaWeb基础1.1 HashMap的底层实现原理?1.2 HashMap 和 HashTable的异同?1.5 Collection 和 Collections的区别?1.6 Collection接口的两种区别1.7 ArrayList、LinkedList、Vector者的异同?1.8 String、Str…...
铭豹扩展坞 USB转网口 突然无法识别解决方法
当 USB 转网口扩展坞在一台笔记本上无法识别,但在其他电脑上正常工作时,问题通常出在笔记本自身或其与扩展坞的兼容性上。以下是系统化的定位思路和排查步骤,帮助你快速找到故障原因: 背景: 一个M-pard(铭豹)扩展坞的网卡突然无法识别了,扩展出来的三个USB接口正常。…...
JavaSec-RCE
简介 RCE(Remote Code Execution),可以分为:命令注入(Command Injection)、代码注入(Code Injection) 代码注入 1.漏洞场景:Groovy代码注入 Groovy是一种基于JVM的动态语言,语法简洁,支持闭包、动态类型和Java互操作性,…...
深入浅出Asp.Net Core MVC应用开发系列-AspNetCore中的日志记录
ASP.NET Core 是一个跨平台的开源框架,用于在 Windows、macOS 或 Linux 上生成基于云的新式 Web 应用。 ASP.NET Core 中的日志记录 .NET 通过 ILogger API 支持高性能结构化日志记录,以帮助监视应用程序行为和诊断问题。 可以通过配置不同的记录提供程…...
【Redis技术进阶之路】「原理分析系列开篇」分析客户端和服务端网络诵信交互实现(服务端执行命令请求的过程 - 初始化服务器)
服务端执行命令请求的过程 【专栏简介】【技术大纲】【专栏目标】【目标人群】1. Redis爱好者与社区成员2. 后端开发和系统架构师3. 计算机专业的本科生及研究生 初始化服务器1. 初始化服务器状态结构初始化RedisServer变量 2. 加载相关系统配置和用户配置参数定制化配置参数案…...
【ROS】Nav2源码之nav2_behavior_tree-行为树节点列表
1、行为树节点分类 在 Nav2(Navigation2)的行为树框架中,行为树节点插件按照功能分为 Action(动作节点)、Condition(条件节点)、Control(控制节点) 和 Decorator(装饰节点) 四类。 1.1 动作节点 Action 执行具体的机器人操作或任务,直接与硬件、传感器或外部系统…...
Nuxt.js 中的路由配置详解
Nuxt.js 通过其内置的路由系统简化了应用的路由配置,使得开发者可以轻松地管理页面导航和 URL 结构。路由配置主要涉及页面组件的组织、动态路由的设置以及路由元信息的配置。 自动路由生成 Nuxt.js 会根据 pages 目录下的文件结构自动生成路由配置。每个文件都会对…...
WordPress插件:AI多语言写作与智能配图、免费AI模型、SEO文章生成
厌倦手动写WordPress文章?AI自动生成,效率提升10倍! 支持多语言、自动配图、定时发布,让内容创作更轻松! AI内容生成 → 不想每天写文章?AI一键生成高质量内容!多语言支持 → 跨境电商必备&am…...
【JavaSE】绘图与事件入门学习笔记
-Java绘图坐标体系 坐标体系-介绍 坐标原点位于左上角,以像素为单位。 在Java坐标系中,第一个是x坐标,表示当前位置为水平方向,距离坐标原点x个像素;第二个是y坐标,表示当前位置为垂直方向,距离坐标原点y个像素。 坐标体系-像素 …...
06 Deep learning神经网络编程基础 激活函数 --吴恩达
深度学习激活函数详解 一、核心作用 引入非线性:使神经网络可学习复杂模式控制输出范围:如Sigmoid将输出限制在(0,1)梯度传递:影响反向传播的稳定性二、常见类型及数学表达 Sigmoid σ ( x ) = 1 1 +...
今日学习:Spring线程池|并发修改异常|链路丢失|登录续期|VIP过期策略|数值类缓存
文章目录 优雅版线程池ThreadPoolTaskExecutor和ThreadPoolTaskExecutor的装饰器并发修改异常并发修改异常简介实现机制设计原因及意义 使用线程池造成的链路丢失问题线程池导致的链路丢失问题发生原因 常见解决方法更好的解决方法设计精妙之处 登录续期登录续期常见实现方式特…...
