打电话推销好还是做网站推广好,网站制造公司,宁波网站推广大全,广西远昌建设公司今天要讲以下内容#xff1a;
1.ConversationBufferWindowMemory#xff1a;基于一个固定长度的滑动窗口的“记忆”功能
2.ConversationSummaryMemory#xff1a;总结对话“记忆”功能
3.ConversationSummaryBufferMemory#xff1a;上面两个的结合#xff0c;超过一定…今天要讲以下内容
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, osos.environ[OPENAI_API_KEY]
openai.api_key os.environ.get(OPENAI_API_KEY)template 你是一个中国厨师用中文回答做菜的问题。你的回答需要满足以下要求:
1. 你的回答必须是中文
2. 回答限制在100个字以内{chat_history}
Human: {human_input}
Chatbot:promptPromptTemplate(input_variables[chat_history,human_input],templatetemplate
)
# k3保留最近三次对话
memoryConversationBufferWindowMemory(memory_keychat_history,k3)
llm_chainLLMChain(llmOpenAI(),promptprompt,memorymemory,verboseTrue
)
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, osllmOpenAI(temperature0)
memoryConversationSummaryMemory(llmOpenAI())
prompt_template 你是一个中国厨师用中文回答做菜的问题。你的回答需要满足以下要求:
1. 你的回答必须是中文
2. 回答限制在100个字以内{history}
Human: {input}
AI:promptPromptTemplate(input_variables[history, input], templateprompt_template
)
# 使用ConversationChain可以不用定义prompt来维护历史聊天记录的为了使用中文我们才定义的
conversation_with_summaryConversationChain(llmllm,memorymemory,promptprompt,verboseTrue
)
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 humans 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 humans 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 tiktokenfrom langchain import PromptTemplate
from langchain.chains import ConversationChain
from langchain.memory import ConversationSummaryBufferMemory
from langchain.llms import OpenAISUMMARIZER_TEMPLATE 请将以下内容逐步概括所提供的对话内容并将新的概括添加到之前的概括中形成新的概括。EXAMPLE
Current summary:
Human询问AI对人工智能的看法。AI认为人工智能是一种积极的力量。New lines of conversation:
Human为什么你认为人工智能是一种积极的力量
AI因为人工智能将帮助人类发挥他们的潜能。New summary:
Human询问AI对人工智能的看法。AI认为人工智能是一种积极的力量因为它将帮助人类发挥他们的潜能。
END OF EXAMPLECurrent summary:
{summary}New lines of conversation:
{new_lines}New summary:SUMMARY_PROMPTPromptTemplate(input_variables[summary, new_lines], templateSUMMARIZER_TEMPLATE
)
# 当对话的达到max_token_limit长度到多长之后我们就应该调用 LLM 去把文本内容小结一下
memoryConversationSummaryBufferMemory(llmOpenAI(),promptSUMMARY_PROMPT, max_token_limit256)CHEF_TEMPLATE 你是一个中国厨师用中文回答做菜的问题。你的回答需要满足以下要求:
1. 你的回答必须是中文。
2. 对于做菜步骤的回答尽量详细一些。{history}
Human: {input}
AI:CHEF_PROMPTPromptTemplate(input_variables[history, input], templateCHEF_TEMPLATE
)
conversation_with_summarConversationChain(llmOpenAI(model_nametext-davinci-003,stop\n\n,max_tokens2048, temperature0.5),promptCHEF_PROMPT,memorymemory,verboseTrue
)
answerconversation_with_summar.predict(input你是谁)
print(answer)
结果 Entering new ConversationChain chain...
Prompt after formatting:
你是一个中国厨师用中文回答做菜的问题。你的回答需要满足以下要求:
1. 你的回答必须是中文。
2. 对于做菜步骤的回答尽量详细一些。Human: 你是谁
AI: Finished chain.我是一位中国厨师可以为您提供做菜的帮助。
继续问
answerconversation_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. 最后将葱花饼装盘即可。
继续问
answerconversation_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 OpenAISUMMARIZER_TEMPLATE 请将以下内容逐步概括所提供的对话内容并将新的概括添加到之前的概括中形成新的概括。EXAMPLE
Current summary:
Human询问AI对人工智能的看法。AI认为人工智能是一种积极的力量。New lines of conversation:
Human为什么你认为人工智能是一种积极的力量
AI因为人工智能将帮助人类发挥他们的潜能。New summary:
Human询问AI对人工智能的看法。AI认为人工智能是一种积极的力量因为它将帮助人类发挥他们的潜能。
END OF EXAMPLECurrent summary:
{summary}New lines of conversation:
{new_lines}New summary:SUMMARY_PROMPTPromptTemplate(input_variables[summary, new_lines], templateSUMMARIZER_TEMPLATE
)
memoryConversationSummaryBufferMemory(llmOpenAI(),promptSUMMARY_PROMPT,max_token_limit40)
# 灌入对应的客服历史记录
memory.save_context({input:你好},{output:你好我是客服李四有什么我可以帮助您的么}
)
memory.save_context({input:我叫张三在你们这里下了一张订单订单号是 2023ABCD我的邮箱地址是 customerabc.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_TEMPLATEentityMemoryConversationEntityMemory(llmllm)
conversationConversationChain(llmllm,verboseTrue,promptENTITY_MEMORY_CONVERSATION_TEMPLATE,memoryentityMemory
)answerconversation.predict(input我叫张老三在你们这里下了一张订单订单号是 2023ABCD我的邮箱地址是 customerabc.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: , customerabc.com: }Current conversation:Last line:
Human: 我叫张老三在你们这里下了一张订单订单号是 2023ABCD我的邮箱地址是 customerabc.com但是这个订单十几天了还没有收到货
You: Finished chain.
您好张老三感谢您下订单我们会尽快处理您的订单。我们会尽快为您发货如果您有任何疑问请随时联系我们。
我们把 memory 里面存储的东西打印出来。
print(conversation.memory.entity_store.store)
结果
{张老三: 张老三是一位客户他的订单号是2023ABCD邮箱地址是customerabc.com。, 2023ABCD: 2023ABCD is an order placed by Zhang Lao San with the email address customerabc.com., customerabc.com: customerabc.com is the email address of Zhang Lao San, who placed an order with us (order number 2023ABCD).}EntityMemory 里面不仅存储了这些命名实体的名字也对应的把命名实体所关联的上下文记录了下来。我们此时来问相关问题他就能回答出来
answerconversation.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 customerabc.com.}Current conversation:Human: 我叫张老三在你们这里下了一张订单订单号是 2023ABCD我的邮箱地址是 customerabc.com但是这个订单十几天了还没有收到货AI: 您好张老三感谢您下订单我们会尽快处理您的订单。我们会尽快为您发货如果您有任何疑问请随时联系我们。Last line:Human: 我刚才的订单号是多少You: Finished chain.您的订单号是2023ABCD。
再问一个
answerconversation.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 customerabc.com, and is the order number provided by the customer.}Current conversation:Human: 我叫张老三在你们这里下了一张订单订单号是 2023ABCD我的邮箱地址是 customerabc.com但是这个订单十几天了还没有收到货AI: 您好张老三感谢您下订单我们会尽快处理您的订单。我们会尽快为您发货如果您有任何疑问请随时联系我们。Human: 我刚才的订单号是多少AI: 您的订单号是2023ABCD。Last line:Human: 订单2023ABCD是谁的订单You: Finished chain.订单2023ABCD是张老三的订单他的邮箱地址是customerabc.com。