【AI】怎样总结与AI的对话内容,让AI拥有长期记忆

怎样总结与AI的对话内容,让AI拥有长期记忆

为什么要总结对话内容

我在这篇文章中曾经说过:现有大模型在 3-6 轮对话后就开始幻想(国内3 轮,ChatGPT4 能做到 6 轮)。那么怎么能在控制对话次数的同时让AI知道你和它之前聊过的内容呢?接下来我们来让AI拥有“长期记忆”

OpenAI的做法

也许你已经了解过OpenAI的summary实现方法:

from langchain.memory import ConversationSummaryBufferMemory
from langchain.llms import OpenAI
llm = ChatOpenAI(temperature=0.0)

# 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}"})
                   
memory.load_memory_variables({})
conversation.predict(input="What would be a good demo to show?")

OpenAI很棒,但是也有一些缺点:

  • 国内会被墙。
  • 价格比较贵,如果你处于调试或者学习阶段,你可能不想花那么多钱。
  • 不能在本地运行。

但是我想在其他大模型上使用ConversationSummaryBufferMemory,对不起,目前我没找到可以支持它大模型,尤其是最近大火Llama-3不能用实在是不开心。

有没有别的办法呢?有!

使用单独的模型来做summary

在Hugging Face的models里搜索samsum,我找到了philschmid/bart-large-cnn-samsum。实现方法如下:

from langchain.memory import ConversationBufferMemory
from transformers import pipeline
memory = ConversationBufferMemory()
summarizer = pipeline("summarization", model="philschmid/bart-large-cnn-samsum")
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.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}"})

summarizer(memory.buffer)
# =>
[{'summary_text': 'AI has a product team meeting at 8 am today. He will need his powerpoint presentation prepared between 9 am and 12 pm. At noon he will have lunch at the Italian resturant with a customer who is driving from over an hour away to understand the latest in AI.'}]
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值