LangChain初体验之Memory

本文介绍了LangChain内存模块的核心设计,包括如何存储聊天消息历史记录和构建查询机制,如从简单到复杂的不同消息视图获取方式。主要讨论了ConversationBufferMemory类,展示了内存系统在处理用户输入和生成响应中的作用。
摘要由CSDN通过智能技术生成

介绍

内存系统需要支持两个基本动作:读取和写入。回想一下,每个链都定义了一些需要某些输入的核心执行逻辑。其中一些输入直接来自用户,但其中一些输入可能来自内存。在给定的运行中,一条链将与其内存系统交互两次。

在接收到初始用户输入后但在执行核心逻辑之前,链将从其内存系统中读取并增强用户输入。
在执行核心逻辑之后但在返回答案之前,链将把当前运行的输入和输出写入内存,以便在未来运行中引用它们。
在这里插入图片描述

任何内存系统中的两个核心设计决策是:

  • 状态是如何存储的
  • 如何查询状态
存储:聊天消息列表

任何内存的基础都是所有聊天交互的历史记录。即使这些不是全部直接使用,它们也需要以某种形式存储。LangChain内存模块的关键部分之一是用于存储这些聊天消息的一系列集成,从内存列表到持久数据库

查询:聊天消息之上的数据结构和算法

保留聊天消息列表相当简单。不太直接的是建立在聊天消息之上的数据结构和算法,这些数据结构和算法服务于最有用的消息视图。

一个非常简单的内存系统可能只返回每次运行的最新消息。稍微复杂一点的内存系统可能会返回过去K条消息的简洁摘要。一个更复杂的系统可能会从存储的消息中提取实体,并且只返回当前运行中引用的实体的信息。

每个应用程序对内存的查询方式可能有不同的要求。内存模块应该可以轻松地开始使用简单的内存系统,并在需要时编写自己的自定义系统

实现

LangChain内存模块的核心是ConversationBufferMemory类。它是一个简单的内存系统,它将聊天消息存储在列表中,并允许您查询这些消息。

from langchain.prompts import PromptTemplate
from langchain_core.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.memory import ConversationBufferMemory

from langchain_community.llms import Ollama
llm = Ollama(model="qwen:7b")

# Notice that "chat_history" is present in the prompt template
template = """You are a nice chatbot having a conversation with a human.

Previous conversation:
{chat_history}

New human question: {question}
Response:"""
prompt = PromptTemplate.from_template(template)
# Notice that we need to align the `memory_key`
memory = ConversationBufferMemory(memory_key="chat_history")
conversation = LLMChain(
    llm=llm,
    prompt=prompt,
    verbose=True,
    memory=memory
)
conversation({"question": "hi"})

输出

{'question': 'hi',
 'chat_history': '',
 'text': 'Hello there! How may I assist you today?\n'}

再次提问,查看聊天历史

conversation({"question": "who aer you"})

输出

{'question': 'who aer you',
 'chat_history': 'Human: hi\nAI: Hello there! How may I assist you today?\n',
 'text': "I am an AI chatbot designed to have conversations with humans. I don't have a physical existence, but I'm here to provide information and assistance when needed. Is there anything specific you'd like to know or discuss?\n"}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值