【Langchain大语言模型开发教程】记忆

🔗 LangChain for LLM Application Development - DeepLearning.AI

学习目标

1、Langchain的历史记忆 ConversationBufferMemory

2、基于窗口限制的临时记忆 ConversationBufferWindowMemory

3、基于Token数量的临时记忆 ConversationTokenBufferMemory

4、基于历史内容摘要的临时记忆 ConversationSummaryMemory

Langchain的历史记忆(ConversationBufferMemory)

python 复制代码
import os
import warnings
from dotenv import load_dotenv, find_dotenv
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory

_ = load_dotenv(find_dotenv())
warnings.filterwarnings('ignore')

我们依然使用智谱的LLM,实例化一下Langchain的记忆模块,并构建一个带有记忆的对话模型

python 复制代码
llm = ChatOpenAI(api_key=os.environ.get('ZHIPUAI_API_KEY'),
                         base_url=os.environ.get('ZHIPUAI_API_URL'),
                         model="glm-4",
                         temperature=0.98)

memory = ConversationBufferMemory()
conversation = ConversationChain(
    llm=llm,
    memory = memory,
    verbose=True
)

进行对话

python 复制代码
conversation.predict(input="Hi, my name is Andrew")
conversation.predict(input="What is 1+1?")
conversation.predict(input="What is my name?")

模型确实可以记住我们的名字,打印一下记忆内容

python 复制代码
#两种方式
print(memory.buffer)

memory.load_memory_variables({})

此外,Langchain还提供了一个函数来添加对话内容

python 复制代码
memory.save_context({"input": "Hi"},
                    {"output": "What's up"})

基于窗口限制的临时记忆(ConversationBufferWindowMemory)

python 复制代码
from langchain.memory import ConversationBufferWindowMemory
python 复制代码
memory = ConversationBufferWindowMemory(k=1) #k表示我们保留最近几轮对话的数量

我们先来添加两轮对话

python 复制代码
memory.save_context({"input": "Hi"},
                    {"output": "What's up"})
memory.save_context({"input": "Not much, just hanging"},
                    {"output": "Cool"})

通过对话历史可以发现,记忆中只保存了一轮的信息

python 复制代码
memory.load_memory_variables({})

{'history': 'Human: Not much, just hanging\nAI: Cool'}

我们使用这种记忆方式来构建一个对话模型,发现他确实遗忘了第一轮的信息

python 复制代码
llm = ChatOpenAI(api_key=os.environ.get('ZHIPUAI_API_KEY'),
                         base_url=os.environ.get('ZHIPUAI_API_URL'),
                         model="glm-4",
                         temperature=0.98)
memory = ConversationBufferWindowMemory(k=1)
conversation = ConversationChain(
    llm=llm,
    memory = memory,
    verbose=False
)

基于Token数量的临时记忆 ConversationTokenBufferMemory

由于langchain中计算token数量的函数并不支持GLM4,所有使用这个函数会报错,根据源代码目前是支持gpt-3.5-turbo-0301、gpt-3.5-turbo、gpt-4,不知道以后会不会加入国产的这些模型。

python 复制代码
memory = ConversationTokenBufferMemory(llm=llm, max_token_limit=500)

memory.save_context({"input": "AI is what?!"},
                    {"output": "Amazing!"})
memory.save_context({"input": "Backpropagation is what?"},
                    {"output": "Beautiful!"})
memory.save_context({"input": "Chatbots are what?"}, 
                    {"output": "Charming!"})

基于历史内容摘要的临时记忆 ConversationSummaryMemory

同理哈,这个函数的作用就是,我们会将历史的对话信息进行总结然后存在我们的记忆单元中,由于这里同样涉及到token的计算,所以这里也是无法正常运行的了。

python 复制代码
from langchain.memory import ConversationSummaryBufferMemory
# create a long string
schedule = "There is a meeting at 8am with your product team. \
You will need your powerpoint presentation prepared. \
9am-12pm have time to work on your LangChain \
project which will go quickly because Langchain is such a powerful tool. \
At Noon, lunch at the italian resturant with a customer who is driving \
from over an hour away to meet you to understand the latest in AI. \
Be sure to bring your laptop to show the latest LLM demo."

memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=100)
memory.save_context({"input": "Hello"}, {"output": "What's up"})
memory.save_context({"input": "Not much, just hanging"},
                    {"output": "Cool"})
memory.save_context({"input": "What is on the schedule today?"},
                    {"output": f"{schedule}"})

构建一个对话模型 (verbose设置为true可以查看到我们历史的一些信息)

python 复制代码
conversation = ConversationChain(
    llm=llm,
    memory = memory,
    verbose=True
)

尝试进行提问

python 复制代码
conversation.predict(input="What would be a good demo to show?")

总结(吴恩达老师视频中的内容)

相关推荐
九亿AI算法优化工作室&18 分钟前
乡村地区无人机医药配送路径规划与优化仿真
人工智能·算法·matlab·回归
jndingxin21 分钟前
OpenCV CUDA模块中矩阵操作-----矩阵最大最小值查找函数
人工智能·opencv
AI Echoes27 分钟前
LLM(大语言模型)部署加速方法——PagedAttention
人工智能·语言模型·自然语言处理
yangshuo128136 分钟前
风车OVF镜像:解放AI开发限制的Ubuntu精简系统
linux·人工智能·ubuntu
Jamence40 分钟前
多模态大语言模型arxiv论文略读(七十七)
人工智能·语言模型·自然语言处理
AI量化投资实验室1 小时前
金融量化智能体,如何开发一个有效的策略?
人工智能·金融
九章云极AladdinEdu1 小时前
GPU SIMT架构的极限压榨:PTX汇编指令级并行优化实践
汇编·人工智能·pytorch·python·深度学习·架构·gpu算力
数智大号1 小时前
浪潮云边协同:赋能云计算变革的强力引擎
人工智能
胡玉洋2 小时前
从新手到高手:全面解析 AI 时代的「魔法咒语」——Prompt
人工智能·ai·prompt·transformer·协议
是店小二呀2 小时前
Trae 插件 Builder 模式:从 0 到 1 开发天气查询小程序,解锁 AI 编程新体验
人工智能·ai编程·trae