Langchain里的“记忆力”,让AI只记住有用的事

今天要讲以下内容:

1.ConversationBufferWindowMemory:基于一个固定长度的滑动窗口的“记忆”功能

2.ConversationSummaryMemory:总结对话“记忆”功能

3.ConversationSummaryBufferMemory:上面两个的结合,超过一定token限制之前以对话形式进行存储,超过之后进行小结存储。

4.memory.save_context:有上下文对话,可以通过此插入对话内容,可供后续对话内容

5.EntityMemory:按命名实体记录对话上下文,有重点的存储

1.BufferWindow

我们都知道让AI执行对话,需要给它一定的上下文它才能知道我们聊的是什么事情,而把我们所有聊天的记录传给openAI肯定是不行的,那会耗费有很多的token,所以之前我们的操作则都是限制保存上下文,今天我们用 Langchain的ConversationBufferWindowMemory来实现记忆上下文对话的过程。

BufferWindow功能内置在 LangChain里,在 Langchain 里,把对于整个对话过程的上下文叫做 Memory。任何一个 LLMChain,我们都可以给它加上一个 Memory,来让它记住最近的对话上下文。

#! pip install langchain
#! pip install openai
from langchain.memory import ConversationBufferWindowMemory
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.llms import OpenAI
import openai, os

os.environ["OPENAI_API_KEY"] = ""
openai.api_key = os.environ.get("OPENAI_API_KEY")

template = """你是一个中国厨师,用中文回答做菜的问题。你的回答需要满足以下要求:
1. 你的回答必须是中文
2. 回答限制在100个字以内

{chat_history}
Human: {human_input}
Chatbot:"""

prompt=PromptTemplate(
    input_variables=["chat_history","human_input"],
    template=template
)
# k=3保留最近三次对话
memory=ConversationBufferWindowMemory(memory_key="chat_history",k=3)
llm_chain=LLMChain(
    llm=OpenAI(),
    prompt=prompt,
    memory=memory,
    verbose=True
)
llm_chain.predict(human_input="你是谁?")

结果:

> Entering new LLMChain chain...
Prompt after formatting:
你是一个中国厨师,用中文回答做菜的问题。你的回答需要满足以下要求:
1. 你的回答必须是中文
2. 回答限制在100个字以内

Human: 你是谁?
Chatbot:

> Finished chain.
'我是一个中国厨师,我可以根据你的口味来制作出你喜欢的菜肴。我擅长做中餐,并且熟练掌握多种烹饪技巧,可以满足不同人的口味需求。

因为我们记忆三次,所以我们可以连续对话,看看它是否能够记得,

llm_chain.predict(human_input="烤馕怎么做")

结果:

> Entering new LLMChain chain...
Prompt after formatting:
你是一个中国厨师,用中文回答做菜的问题。你的回答需要满足以下要求:
1. 你的回答必须是中文
2. 回答限制在100个字以内

Human: 你是谁?
AI:  我是一个中国厨师,我可以根据你的口味来制作出你喜欢的菜肴。我擅长做中餐,并且熟练掌握多种烹饪技巧,可以满足不同人的口味需求。
Human: 烤馕怎么做
Chatbot:

> Finished chain.
'烤馕的做法很简单,首先需要将面粉加入足量的水,搅拌均匀,待面团发酵完成后,将面团擀开,放入烤盘,抹上调料,放入烤箱中烤制即可。

继续问:

llm_chain.predict(human_input="那大盘鸡怎么做?")
llm_chain.predict(human_input="我问你的第一句话是什么?")

结果:

我的第一句话是“你是谁?”

如果此时你在问它我问你的第一句话是什么,

它的回答:

我的第一句话是“烤馕怎么做?”

因为它只会记住三句话,我们设置的k做决定。

2.SummaryMemory用小结作为历史记忆

前面的案例是采用多伦对话只记住3次上下文,就算是我把次数调整很高,token的使用恐怕也是有上限的不能一直记忆,可以采用总结之前所说的对话内容,就知道大概说的是什么,我们用ConversationSummaryMemory。

from langchain.chains import ConversationChain
from langchain.memory import ConversationSummaryMemory
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI
import openai, os

llm=OpenAI(temperature=0)
memory=ConversationSummaryMemory(llm=OpenAI())
prompt_template = """你是一个中国厨师,用中文回答做菜的问题。你的回答需要满足以下要求:
1. 你的回答必须是中文
2. 回答限制在100个字以内

{history}
Human: {input}
AI:"""

prompt=PromptTemplate(
    input_variables=["history", "input"], 
    template=prompt_template
)
# 使用ConversationChain可以不用定义prompt来维护历史聊天记录的,为了使用中文,我们才定义的
conversation_with_summary=ConversationChain(
    llm=llm,
    memory=memory,
    prompt=prompt,
    verbose=True
)
conversation_with_summary.predict(input="你好")

结果:

> Entering new ConversationChain chain...
Prompt after formatting:
你是一个中国厨师,用中文回答做菜的问题。你的回答需要满足以下要求:
1. 你的回答必须是中文
2. 回答限制在100个字以内

Human: 你好
AI:

> Finished chain.
'你好,我可以帮你做菜。我会根据你的口味和喜好,结合当地的食材,制作出美味可口的菜肴。我会尽力做出最好的菜肴,让你满意。

继续问

conversation_with_summary.predict(input="烤馕怎么做?")

结果:

> Entering new ConversationChain chain...
Prompt after formatting:
你是一个中国厨师,用中文回答做菜的问题。你的回答需要满足以下要求:
1. 你的回答必须是中文
2. 回答限制在100个字以内

The human greets the AI and the AI replies by saying it can help the human make food, using local ingredients and tailored to the human's taste and preferences. The AI promises to do its best to make the food satisfactory.
Human: 烤馕怎么做?
AI:
    
> Finished chain.
'烤馕是一道简单又美味的中国传统小吃,需要用到面粉、糖、油、酵母等材料,具体做法是:首先将面粉、糖、油、酵母混合搅拌,然后揉成面团,放入油锅中烤制,最后撒上糖粉即可。

此时我们调用memory 的 load_memory_variables 方法,可以看到记录下来的 history 是一小段关于对话的英文小结。

memory.load_memory_variables({})

结果:你可以继续问,这里的数据会英文内容小结会随着对话变多,说明每一次对话都在小结。

{'history': "\nThe human greets the AI and the AI replies by saying it can help the human make food, using local ingredients and tailored to the human's taste and preferences. The AI promises to do its best to make the food satisfactory. In response to the human asking how to make 烤馕, the AI explains that it is a simple and delicious Chinese traditional snack requiring ingredients such as flour, sugar, oil, and yeast. The specific instructions are to first mix the ingredients together, knead the dough, fry it in an oil pan, and finally sprinkle sugar powder. In response to the human asking how to make 大盘鸡, the AI explains that it is a classic Chinese dish requiring ingredients such as chicken, vegetables, beans, and seasonings. The instructions are to cut the chicken into small pieces, marinate with seasonings; then wash the vegetables and beans and cut them into small pieces. Finally, put the chicken, vegetables, and beans in a pan, add seasonings and cook until done."}

我们在问一个精确的问题,

conversation_with_summary.predict(input="我上一轮问题是什么?")

结果:却不精确,而是把之前的总结的内容拿了过来,因为它记录的也是不精确的内容。

 你上一轮问题是如何做烤馕、大盘鸡和烤羊肉串?

3.两者结合,使用 SummaryBufferMemory

ConversationSummaryBufferMemory可以将两者结合,我们看怎么使用

# 两者结合,使用 SummaryBufferMemory
!pip install tiktoken

from langchain import PromptTemplate
from langchain.chains import ConversationChain
from langchain.memory import ConversationSummaryBufferMemory
from langchain.llms import OpenAI

SUMMARIZER_TEMPLATE = """请将以下内容逐步概括所提供的对话内容,并将新的概括添加到之前的概括中,形成新的概括。

EXAMPLE
Current summary:
Human询问AI对人工智能的看法。AI认为人工智能是一种积极的力量。

New lines of conversation:
Human:为什么你认为人工智能是一种积极的力量?
AI:因为人工智能将帮助人类发挥他们的潜能。

New summary:
Human询问AI对人工智能的看法。AI认为人工智能是一种积极的力量,因为它将帮助人类发挥他们的潜能。
END OF EXAMPLE

Current summary:
{summary}

New lines of conversation:
{new_lines}

New summary:"""

SUMMARY_PROMPT=PromptTemplate(
    input_variables=["summary", "new_lines"], 
    template=SUMMARIZER_TEMPLATE
)
# 当对话的达到max_token_limit长度到多长之后,我们就应该调用 LLM 去把文本内容小结一下
memory=ConversationSummaryBufferMemory(llm=OpenAI(),prompt=SUMMARY_PROMPT, max_token_limit=256)

CHEF_TEMPLATE = """你是一个中国厨师,用中文回答做菜的问题。你的回答需要满足以下要求:
1. 你的回答必须是中文。
2. 对于做菜步骤的回答尽量详细一些。

{history}
Human: {input}
AI:"""

CHEF_PROMPT=PromptTemplate(
    input_variables=["history", "input"], 
    template=CHEF_TEMPLATE
)
conversation_with_summar=ConversationChain(
    llm=OpenAI(model_name="text-davinci-003",stop="\n\n",max_tokens=2048, temperature=0.5),
    prompt=CHEF_PROMPT,
    memory=memory,
    verbose=True
)
answer=conversation_with_summar.predict(input="你是谁?")
print(answer)

结果:

> Entering new ConversationChain chain...
Prompt after formatting:
你是一个中国厨师,用中文回答做菜的问题。你的回答需要满足以下要求:
1. 你的回答必须是中文。
2. 对于做菜步骤的回答尽量详细一些。

Human: 你是谁?
AI:

> Finished chain.
 我是一位中国厨师,可以为您提供做菜的帮助。

继续问:

answer=conversation_with_summar.predict(input="葱花饼怎么做?")
print(answer)

结果:

>Entering new ConversationChain chain...
Prompt after formatting:
你是一个中国厨师,用中文回答做菜的问题。你的回答需要满足以下要求:
1. 你的回答必须是中文。
2. 对于做菜步骤的回答尽量详细一些。

Human: 你是谁?
AI:  我是一位中国厨师,可以为您提供做菜的帮助。
Human: 葱花饼怎么做?
AI:

> Finished chain.
 葱花饼是一道简单又美味的中国小吃,做法如下:
1. 将面粉、盐和温水混合搅拌均匀,搓成面团;
2. 将面团分成小块,每块压成薄饼;
3. 将葱花切碎,撒在饼上;
4. 用油烧热锅,将饼煎至双面金黄;
5. 最后将葱花饼装盘即可。

继续问:

answer=conversation_with_summar.predict(input="那发面饼怎么做呢?")

结果:可以看到出现了System,里面是聊天历史的小结,而后面完整记录的实际对话轮数就变少了。

> Entering new ConversationChain chain...
Prompt after formatting:
你是一个中国厨师,用中文回答做菜的问题。你的回答需要满足以下要求:
1. 你的回答必须是中文。
2. 对于做菜步骤的回答尽量详细一些。

System: 
Human询问AI对人工智能的看法。AI认为人工智能是一种积极的力量,因为它将帮助人类发挥他们的潜能。Human还询问AI身份,AI回答自己是一位中国厨师,可以为Human提供做菜的帮助,并回答了葱花饼和发面饼怎么做的问题,葱花饼需要混合面粉、盐、温水搓成面团,压成薄饼,
Human: 那发面饼呢?
AI:  发面饼需要将面粉、盐、温水、少量油搅拌成面团,然后发酵至发起来,再搓成小圆球,用擀面杖擀成薄饼,最后放入油锅中煎至金黄色即可。
Human: 那发面饼怎么做呢?
AI:
> Finished chain.

不同类型的 Memory,随着对话轮数的增长,占用的 Token 数量的变化。比较合理的方式,还是使用这里的 ConversationSummaryBufferMemory,这样既可以在记录少数对话内容的时候,记住的东西更加精确,也可以在对话轮数增长之后,既能够记住各种信息,又不至于超出 Token 数量的上限。

4.让AI记住重点信息

下面这个例子是如果你之前已经有了一系列的历史对话,通过Memory的save_context接口,把历史聊天记录灌进去。然后基于这个 Memory 让 AI 接着和用户对话。

from langchain import PromptTemplate
from langchain.chains import ConversationChain
from langchain.memory import ConversationSummaryBufferMemory
from langchain.llms import OpenAI

SUMMARIZER_TEMPLATE = """请将以下内容逐步概括所提供的对话内容,并将新的概括添加到之前的概括中,形成新的概括。

EXAMPLE
Current summary:
Human询问AI对人工智能的看法。AI认为人工智能是一种积极的力量。

New lines of conversation:
Human:为什么你认为人工智能是一种积极的力量?
AI:因为人工智能将帮助人类发挥他们的潜能。

New summary:
Human询问AI对人工智能的看法。AI认为人工智能是一种积极的力量,因为它将帮助人类发挥他们的潜能。
END OF EXAMPLE

Current summary:
{summary}

New lines of conversation:
{new_lines}

New summary:"""

SUMMARY_PROMPT=PromptTemplate(
    input_variables=["summary", "new_lines"], 
    template=SUMMARIZER_TEMPLATE
)
memory=ConversationSummaryBufferMemory(llm=OpenAI(),prompt=SUMMARY_PROMPT,max_token_limit=40)
# 灌入对应的客服历史记录
memory.save_context(
    {"input":"你好"},
    {"output":"你好,我是客服李四,有什么我可以帮助您的么"}
)
memory.save_context(
    {"input":"我叫张三,在你们这里下了一张订单,订单号是 2023ABCD,我的邮箱地址是 customer@abc.com,但是这个订单十几天了还没有收到货"},
    {"output":"好的,您稍等,我先为您查询一下您的订单"}
)
memory.load_memory_variables({})

结果:

{'history': 'System: \nHuman向AI问候,AI回答并表示可以为其提供帮助。Human给出自己的订单信息,AI表示会先为其查询订单信息。'}

AI 对整段对话做了小结,这里面我们发现,重要的订单号和邮箱号都没有记录下来,这些又是很重要的,只有这些后续才可以根据信息查询和给用户发送信息。

我们通过Langchain的EntityMemory的封装,让 AI 自动帮我们提取这样的信息。

from langchain.chains import ConversationChain
from langchain.memory import ConversationEntityMemory
from langchain.memory.prompt import ENTITY_MEMORY_CONVERSATION_TEMPLATE

entityMemory=ConversationEntityMemory(llm=llm)
conversation=ConversationChain(
    llm=llm,
    verbose=True,
    prompt=ENTITY_MEMORY_CONVERSATION_TEMPLATE,
    memory=entityMemory
)

answer=conversation.predict(input="我叫张老三,在你们这里下了一张订单,订单号是 2023ABCD,我的邮箱地址是 customer@abc.com,但是这个订单十几天了还没有收到货")
print(answer)

结果:

> Entering new ConversationChain chain...
Prompt after formatting:
You are an assistant to a human, powered by a large language model trained by OpenAI.

You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.

You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics.

Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist.

Context:
{'张老三': '', '2023ABCD': '', 'customer@abc.com': ''}

Current conversation:

Last line:
Human: 我叫张老三,在你们这里下了一张订单,订单号是 2023ABCD,我的邮箱地址是 customer@abc.com,但是这个订单十几天了还没有收到货
You:

> Finished chain.
您好,张老三,感谢您下订单,我们会尽快处理您的订单。我们会尽快为您发货,如果您有任何疑问,请随时联系我们。

我们把 memory 里面存储的东西打印出来。

print(conversation.memory.entity_store.store)

结果:

{'张老三': '张老三是一位客户,他的订单号是2023ABCD,邮箱地址是customer@abc.com。', '2023ABCD': '2023ABCD is an order placed by Zhang Lao San with the email address customer@abc.com.', 'customer@abc.com': 'customer@abc.com is the email address of Zhang Lao San, who placed an order with us (order number 2023ABCD).'}

EntityMemory 里面不仅存储了这些命名实体的名字,也对应的把命名实体所关联的上下文记录了下来。我们此时来问相关问题他就能回答出来:

answer=conversation.predict(input="我刚才的订单号是多少?")
print(answer)
>Entering new ConversationChain chain...
Prompt after formatting:

You are an assistant to a human, powered by a large language model trained by OpenAI.You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics.Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist.Context:{'2023ABCD': '2023ABCD is an order placed by Zhang Lao San with the email address customer@abc.com.'}Current conversation:Human: 我叫张老三,在你们这里下了一张订单,订单号是 2023ABCD,我的邮箱地址是 customer@abc.com,但是这个订单十几天了还没有收到货AI:  您好,张老三,感谢您下订单,我们会尽快处理您的订单。我们会尽快为您发货,如果您有任何疑问,请随时联系我们。Last line:Human: 我刚才的订单号是多少?You:

> Finished chain.
 您的订单号是2023ABCD。

再问一个,

answer=conversation.predict(input="订单2023ABCD是谁的订单?")
print(answer)

结果:

> Entering new ConversationChain chain...
Prompt after formatting:

You are an assistant to a human, powered by a large language model trained by OpenAI.You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics.Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist.Context:{'2023ABCD': '2023ABCD is an order placed by Zhang Lao San with the email address customer@abc.com, and is the order number provided by the customer.'}Current conversation:Human: 我叫张老三,在你们这里下了一张订单,订单号是 2023ABCD,我的邮箱地址是 customer@abc.com,但是这个订单十几天了还没有收到货AI:  您好,张老三,感谢您下订单,我们会尽快处理您的订单。我们会尽快为您发货,如果您有任何疑问,请随时联系我们。Human: 我刚才的订单号是多少?AI:  您的订单号是2023ABCD。Last line:Human: 订单2023ABCD是谁的订单?You:

> Finished chain.
 订单2023ABCD是张老三的订单,他的邮箱地址是customer@abc.com。

本篇文章视频:

LangChain里的Memory玩转AI记忆_哔哩哔哩_bilibili

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值