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

【LangChain】理论及应用实战(4):Memory

文章目录

  • 一、对话Memory的实现
    • 1.1 ConversationBufferMemory
  • 二、几种其他的memory
    • 2.1 ConversationBufferWindowMemory
    • 2.2 ConversationTokenBufferMemory
    • 2.3 ConversationSummaryBufferMemory
  • 三、自定义Memory
  • 四、为Chain增加Memory
    • 4.1 在LLMChain上使用Memory
    • 4.2 在ConversationChain上使用Memory
    • 4.3 同一个Chain合并使用多个Memory
    • 4.4 给一个多参数Chain增加Memory
  • 参考资料

本文主要内容参考资料:AI Agent智能体开发,一步步教你搭建agent开发环境(需求分析、技术选型、技术分解)

与大模型进行多轮对话时,并不是前面的对话真的被大模型记住了,其实还是通过prompt来实现的。也就是在进行多轮对话时,在没有令牌限制或其他限制的情况下,会将前面的对话内容放进目前对话的prompt中。

langchain当中通过memory模块当中的一系列方法来实现多轮对话的记忆存储。

一、对话Memory的实现

1.1 ConversationBufferMemory

langchain中国最常见的一种对话Memory的实现就是基于 ConversationBufferMemory 方法。该方法会保存历史对话的所有内容,并存储在内容中,属于短时记忆。启用的方式是构建一个对话的ConversationChain,将memory的方法作为参数传进去。还可以指定大模型、以及显示prompt的内容等。

示例代码:

import  os
import openai
from  langchain_community.chat_models import ChatOpenAI
from langchain.chains import ConversationChain
from langchain.memory import  ConversationBufferMemory
 
os.environ["OPENAI_API_KEY"] = ''
openai.api_key = os.environ.get("OPENAI_API_KEY")
 
llm = ChatOpenAI(model_name = 'gpt-3.5-turbo',temperature = 0.0)
memory = ConversationBufferMemory() # 全部存储
 
conversation = ConversationChain(
    llm = llm,
    memory = memory,
    verbose = True # 开启查看每次prompt内容
)
 
while 1:
    content = input('user:')
    print(conversation.predict(input=content))

输出如下:

user:Hi,my name is Rain.
 
> 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,my name is Rain.
AI:
 
> Finished chain.
Hello Rain! It's nice to meet you. How can I assist you today?
user:what is 1+1?
> 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,my name is Rain.
AI: Hello Rain! It's nice to meet you. How can I assist you today?
Human: what is 1+1?
AI:
 
> Finished chain.
1 + 1 equals 2. Is there anything else you would like to know?
 
user:what is my name?
 
> 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,my name is Rain.
AI: Hello Rain! It's nice to meet you. How can I assist you today?
Human: what is 1+1?
AI: 1 + 1 equals 2. Is there anything else you would like to know?
Human: what is my name?
AI:
> Finished chain.
Your name is Rain.

二、几种其他的memory

2.1 ConversationBufferWindowMemory

这个方法不同于上文提到的方法,可以通过参数设置需要记忆的对话轮数

from langchain.memory import  ConversationBufferWindowMemory
 
memory = ConversationBufferWindowMemory(k = 1) # K参数设置记忆的对话轮数

2.2 ConversationTokenBufferMemory

这个方法可以设置最大令牌上限,其实也是限制记忆的数量,毕竟大模型都是按Token数量收费的,当对话轮数很多时,每次对话记忆耗费的Token数量也会变得很大。指定llm参数是因为每种模型的token计算方式不同。

from langchain.memory import  ConversationTokenBufferMemory
 
memory = ConversationTokenBufferMemory(llm=llm,max_token_limit=30) # 设置令牌上限

2.3 ConversationSummaryBufferMemory

这个方法会将之前所有的对话轮数根据你设置的令牌上限进行总结,保证新对话的prompt不会超过这个令牌上限,同时又能最大程度保存一些历史对话信息。

from langchain.memory import  ConversationSummaryBufferMemory
 
memory = ConversationSummaryBufferMemory(llm=llm,max_token_limit=30) 

通过总结摘要的方式,可以实现长对话的记忆功能。

三、自定义Memory

[待更新…]

四、为Chain增加Memory

4.1 在LLMChain上使用Memory


from langchain.chains import LLMChain
from langchain_ollama import OllamaLLM
from langchain.memory import ConversationBufferMemory
from langchain.prompts import PromptTemplate

# 自定义模版
template = """你是一个可以和人类对话的机器人.
{chat_history}
人类:{human_input}
机器人:
"""

prompt = PromptTemplate(
    template=template,
    input_variables={"chat_history", "human_input"}
)

memory = ConversationBufferMemory(
    memory_key="chat_history"  # 和prompt中的input_variables中的占位符对应
)

llm_model = OllamaLLM(model="llama3.1:8b")

chain = LLMChain(
    llm=llm_model,
    prompt=prompt,
    memory=memory
)

result = chain.predict(human_input="你知道我叫什么名字吗")
print(result)

4.2 在ConversationChain上使用Memory

from langchain.chains import ConversationChain
from langchain.llms import OpenAI
from langchain.memory import ConversationBufferMemory

llm = OpenAI(
    temperature=0,
)
memory = ConversationBufferMemory(
    memory_key="history",
    return_messages=True,
)
chain = ConversationChain(
    llm=llm,
    memory=memory,
    verbose=True,
)

chain.predict(input="帮我做个一日游攻略")

4.3 同一个Chain合并使用多个Memory

[待更新…]

4.4 给一个多参数Chain增加Memory

[待更新…]

参考资料

AI Agent智能体开发,一步步教你搭建agent开发环境(需求分析、技术选型、技术分解)


http://www.kler.cn/a/584177.html

相关文章:

  • 视觉语言模型VLM发展脉络
  • windows第十二章 MFC控件常用消息
  • FANUC机器人几种常用的通讯网络及接口
  • Gone v2 中 Gone-Gin 性能测试报告
  • AUTOSAR_CP_EthernetSwitchDriver
  • 人工智能之数学基础:线性变换及其机器学习领域中的应用
  • Flutter_学习记录_connectivity_plus 检测网络
  • Yashan DB 应用开发
  • Python里matplotlib不显示中文的问题
  • MoonSharp 文档五
  • 前瞻技术:未来生活的新动力与改变
  • spark实验
  • 树与二叉树的遍历
  • Vue3中全局使用Sass变量方法
  • JetBrains(全家桶: IDEA、WebStorm、GoLand、PyCharm) 2024.3+ 2025 版免费体验方案
  • 【拓扑排序】火星词典
  • SpringS ecurity测试登录接口报错
  • Visual Studio关闭警告
  • 16.AVL树实现
  • 自动化测试 | Python+PyCharm+Google Chrome+Selenium 环境安装记录