使用LangChain memory帮我维护历史信息

大多数基于大型语言模型(LLM)的应用都有会话界面。会话中的一个重要组成部分是能够引用之前对话中引入的信息。在最基本的层面上,会话系统应该能够直接访问一些过去的消息。

我们将这种存储过去交互信息的能力称为“记忆(memory)”。LangChain提供了许多工具来为系统添加记忆功能。这些工具可以单独使用,也可以无缝地整合到 Chain中。

简介

记忆系统需要支持两个基本动作:读取和写入。请记住,每个Chain都定义了一些核心执行逻辑,期望某些输入。其中一些输入直接来自用户,但有些输入可以来自记忆。在给定的运行中,Chain将与其记忆系统交互两次。

1. 在接收到初始用户输入之后但在执行核心逻辑之前,Chain将从其记忆系统中读取并增强用户输入。

2. 在执行核心逻辑之后但在返回答案之前, Chain将把当前运行的输入和输出写入记忆,以便将来可以引用。图表说明了会话界面中记忆系统的读取和写入操作。

之前让写一首爱设计的古诗,后边描绘黄山自动出古诗,所以这大模型对话是有上下文记忆的。

可以看到 文心一言,展示出我们输入历史记录,接下来实现一下吧。

逻辑架构

了解

让我们看看LangChain中的记忆实际上是什么样的。在这里,我们将介绍与任意记忆类交互的基础知识。

让我们看看如何在使用 ConversationBufferMemory。conversationBufferMemory 是一种极其简单的记忆形式,它只是在缓冲区中保持一系列聊天消息,并将这些消息传递到提示模板中。

from langchain.memory import ConversationBufferMemory

memory = ConversationBufferMemory()

memory.chat_memory.add_user_message("我是用户消息")
memory.chat_memory.add_ai_message("我是大模型消息")

print(memory)

 

如何定义Memory中保存历史记录的Key

在进入Chain之前,会从记忆中读取各种变量。这些变量有特定的名称,需要与Chain期望的变量对齐。您可以通过调用 memory.load memory_variables({})来查看这些变量是什么。请注意,我们传入的空字典只是实际变量的占位符。如果您使用的记忆类型依赖于输入变量,则可能需要传入一些。

from langchain.memory import ConversationBufferMemory
from langchain.prompts import PromptTemplate
memory = ConversationBufferMemory()

memory.chat_memory.add_user_message("我是用户消息")
memory.chat_memory.add_ai_message("我是大模型消息")
# print(memory)

res_variables = memory.load_memory_variables({})

print(res_variables)
print(res_variables["history"])

 聊天历史

from langchain.memory import ConversationBufferMemory
from langchain.prompts import PromptTemplate
memory = ConversationBufferMemory()

memory.chat_memory.add_user_message("我是用户消息")
memory.chat_memory.add_ai_message("我是大模型消息")

# print(memory)

res_variables = memory.load_memory_variables({})

# print(res_variables)
# print(res_variables["history"])


prompt = PromptTemplate.from_template("聊天历史:\n{history}\n")
res = prompt.format(**res_variables)   # res_variables  是个字典,可以使用** 打散

print(res)

 

 

自定义取值变量名key

在这种情况下,您可以看到 load_memory_variables 返回一个键,即 history 。这意味着您的 Chain(很可能是您的提示(prompt)应该期望一个名为 history 的输入。您通常可以通过记忆类上的参数来控制这个变量

例如,如果您希望记忆变量以chat history键返回,您可以这样做:

from langchain.memory import ConversationBufferMemory
from langchain.prompts import PromptTemplate

# 自定义一个历史key。 可以memory_key控制 return_messages=True变成列表
memory = ConversationBufferMemory(memory_key="aaa_history", return_messages=True)  

memory.chat_memory.add_user_message("我是用户消息")
memory.chat_memory.add_ai_message("我是大模型消息")
# print(memory)

res_variables = memory.load_memory_variables({})

print(res_variables)

prompt = PromptTemplate.from_template("聊天历史:\n{aaa_history}\n")
res = prompt.format(**res_variables)   # res_variables  是个字典,可以使用** 打散

print(res)

 

 

Memory返回字符串还是消息列表

最常见的一种记忆类型涉及返回聊天消息列表。这些可以作为单个字符串返回,全部连接在一起(当它们将传递给LLM时很有用)或者是 ChatMessages列表(当传递给ChatModels时很有用)。默认情况下,它们作为单个字符串返回。为了以消息列表的形式返回,

可以设置 return_messages=True

加 return_messages=True 结果

在使用 chay model时候就需要使用返回是一个消息列表了。

end...

posted @   王竹笙  阅读(38)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示