如何创建个人网站英语作文,柏乡县建设局网站,网站作用,建网通分类目录#xff1a;《自然语言处理从入门到应用》总目录 会话缓存记忆ConversationBufferMemory
本节将介绍如何使用对话缓存记忆ConversationBufferMemory。这种记忆方式允许存储消息#xff0c;并将消息提取到一个变量中#xff0c;我们首先将其提取为字符串#xff1a…分类目录《自然语言处理从入门到应用》总目录 会话缓存记忆ConversationBufferMemory
本节将介绍如何使用对话缓存记忆ConversationBufferMemory。这种记忆方式允许存储消息并将消息提取到一个变量中我们首先将其提取为字符串
from langchain.memory import ConversationBufferMemorymemory ConversationBufferMemory()
memory.save_context({input: hi}, {output: whats up})
memory.load_memory_variables({})输出
{history: Human: hi\nAI: whats up}我们还可以将历史记录作为消息列表获取。如果我们与聊天模型一起使用这非常有用
memory ConversationBufferMemory(return_messagesTrue)
memory.save_context({input: hi}, {output: whats up})
memory.load_memory_variables({})输出
{history: [HumanMessage(contenthi, additional_kwargs{}),AIMessage(contentwhats up, additional_kwargs{})]}在链式结构中使用
我们还可以在链式结构中使用它设置verboseTrue以便我们可以看到提示
from langchain.llms import OpenAI
from langchain.chains import ConversationChainllm OpenAI(temperature0)
conversation ConversationChain(llmllm, verboseTrue, memoryConversationBufferMemory()
)
conversation.predict(inputHi there!)日志输出 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 there!
AI: Finished chain.输出 Hi there! Its nice to meet you. How can I help you today?输入
conversation.predict(inputIm doing well! Just having a conversation with an AI.)日志输出 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 there!
AI: Hi there! Its nice to meet you. How can I help you today?
Human: Im doing well! Just having a conversation with an AI.
AI: Finished chain.输出 Thats great! Its always nice to have a conversation with someone new. What would you like to talk about?输入
conversation.predict(inputTell me about yourself.)日志输出 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 there!
AI: Hi there! Its nice to meet you. How can I help you today?
Human: Im doing well! Just having a conversation with an AI.
AI: Thats great! Its always nice to have a conversation with someone new. What would you like to talk about?
Human: Tell me about yourself.
AI: Finished chain.输出 Sure! Im an AI created to help people with their everyday tasks. Im programmed to understand natural language and provide helpful information. Im also constantly learning and updating my knowledge base so I can provide more accurate and helpful answers.会话缓存记忆ConversationBufferWindowMemory
会话缓存记忆ConversationBufferWindowMemory保留了对话中随时间变化的交互列表。它只使用最后的 K K K次交互。这对于保持最近交互的滑动窗口很有用以防止缓冲区过大。
from langchain.memory import ConversationBufferWindowMemorymemory ConversationBufferWindowMemory(k1)
memory.save_context({input: hi}, {output: whats up})
memory.save_context({input: not much you}, {output: not much})
memory.load_memory_variables({})输出
{history: Human: not much you\nAI: not much}我们还可以将历史记录作为消息列表获取如果我们将其与聊天模型一起使用这将非常有用
memory ConversationBufferWindowMemory(k1, return_messagesTrue)memory.save_context({input: hi}, {output: whats up})
memory.save_context({input: not much you}, {output: not much})
memory.load_memory_variables({})输出
{history: [HumanMessage(contentnot much you, additional_kwargs{}),
AIMessage(contentnot much, additional_kwargs{})]}Using in a chain
在下面的示例中再次设置verboseTrue以便查看提示
from langchain.llms import OpenAI
from langchain.chains import ConversationChainconversation_with_summary ConversationChain(llmOpenAI(temperature0), memoryConversationBufferWindowMemory(k2), verboseTrue
)conversation_with_summary.predict(inputHi, whats up?)日志输出 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, whats up?
AI: Finished chain.输出 Hi there! Im doing great. Im currently helping a customer with a technical issue. How about you?输入
conversation_with_summary.predict(inputWhats their issues?)日志输出 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, whats up?
AI: Hi there! Im doing great. Im currently helping a customer with a technical issue. How about you?
Human: Whats their issues?
AI: Finished chain.输出 The customer is having trouble connecting to their Wi-Fi network. Im helping them troubleshoot the issue and get them connected.输入
conversation_with_summary.predict(inputIs it going well?)输出 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, whats up?
AI: Hi there! Im doing great. Im currently helping a customer with a technical issue. How about you?
Human: Whats their issues?
AI: The customer is having trouble connecting to their Wi-Fi network. Im helping them troubleshoot the issue and get them connected.
Human: Is it going well?
AI: Finished chain.输出 Yes, its going well so far. Weve already identified the problem and are now working on a solution.当前若继续对话则 Hi there! Im doing great. Im currently helping a customer with a technical issue. How about you?的记忆将被遗忘
conversation_with_summary.predict(inputWhats the solution?)日志输出 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: Whats their issues?
AI: The customer is having trouble connecting to their Wi-Fi network. Im helping them troubleshoot the issue and get them connected.
Human: Is it going well?
AI: Yes, its going well so far. Weve already identified the problem and are now working on a solution.
Human: Whats the solution?
AI: Finished chain.输出 The solution is to reset the router and reconfigure the settings. Were currently in the process of doing that.实体记忆Entity Memory
本节演示了如何使用一个记忆模块来记录有关特定实体的信息。它使用语言模型LLMs提取实体相关的信息并随着时间的推移逐渐积累对该实体的知识。让我们首先通过一个例子来了解如何使用这个功能
from langchain.llms import OpenAI
from langchain.memory import ConversationEntityMemoryllm OpenAI(temperature0)
memory ConversationEntityMemory(llmllm)
_input {input: Deven Sam are working on a hackathon project}
memory.load_memory_variables(_input)
memory.save_context(_input,{output: That sounds like a great project! What kind of project are they working on?}
)
memory.load_memory_variables({input: who is Sam})输出
{history: Human: Deven Sam are working on a hackathon project\nAI: That sounds like a great project! What kind of project are they working on?,
entities: {Sam: Sam is working on a hackathon project with Deven.}}输入
memory ConversationEntityMemory(llmllm, return_messagesTrue)
_input {input: Deven Sam are working on a hackathon project}
memory.load_memory_variables(_input)
memory.save_context(_input,{output: That sounds like a great project! What kind of project are they working on?}
)
memory.load_memory_variables({input: who is Sam})输出
{history: [HumanMessage(contentDeven Sam are working on a hackathon project, additional_kwargs{}),
AIMessage(content That sounds like a great project! What kind of project are they working on?, additional_kwargs{})],
entities: {Sam: Sam is working on a hackathon project with Deven.}}在链中调用
from langchain.chains import ConversationChain
from langchain.memory import ConversationEntityMemory
from langchain.memory.prompt import ENTITY_MEMORY_CONVERSATION_TEMPLATE
from pydantic import BaseModel
from typing import List, Dict, Any
conversation ConversationChain(llmllm, verboseTrue,promptENTITY_MEMORY_CONVERSATION_TEMPLATE,memoryConversationEntityMemory(llmllm)
)conversation.predict(inputDeven Sam are working on a hackathon project)日志输出 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:
{Deven: Deven is working on a hackathon project with Sam., Sam: Sam is working on a hackathon project with Deven.}Current conversation:Last line:
Human: Deven Sam are working on a hackathon project
You: Finished chain.输出 That sounds like a great project! What kind of project are they working on?输入
conversation.memory.entity_store.store输出
{Deven: Deven is working on a hackathon project with Sam, which they are entering into a hackathon.,
Sam: Sam is working on a hackathon project with Deven.}输入
conversation.predict(inputThey are trying to add more complex memory structures to Langchain)日志输出 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:
{Deven: Deven is working on a hackathon project with Sam, which they are entering into a hackathon., Sam: Sam is working on a hackathon project with Deven., Langchain: }Current conversation:
Human: Deven Sam are working on a hackathon project
AI: That sounds like a great project! What kind of project are they working on?
Last line:
Human: They are trying to add more complex memory structures to Langchain
You: Finished chain.输出 That sounds like an interesting project! What kind of memory structures are they trying to add?输入
conversation.predict(inputThey are adding in a key-value store for entities mentioned so far in the conversation.)日志输出 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:
{Deven: Deven is working on a hackathon project with Sam, which they are entering into a hackathon. They are trying to add more complex memory structures to Langchain., Sam: Sam is working on a hackathon project with Deven, trying to add more complex memory structures to Langchain., Langchain: Langchain is a project that is trying to add more complex memory structures., Key-Value Store: }Current conversation:
Human: Deven Sam are working on a hackathon project
AI: That sounds like a great project! What kind of project are they working on?
Human: They are trying to add more complex memory structures to Langchain
AI: That sounds like an interesting project! What kind of memory structures are they trying to add?
Last line:
Human: They are adding in a key-value store for entities mentioned so far in the conversation.
You: Finished chain.输出 That sounds like a great idea! How will the key-value store help with the project?输入
conversation.predict(inputWhat do you know about Deven Sam?)日志输出 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:
{Deven: Deven is working on a hackathon project with Sam, which they are entering into a hackathon. They are trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation., Sam: Sam is working on a hackathon project with Deven, trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation.}Current conversation:
Human: Deven Sam are working on a hackathon project
AI: That sounds like a great project! What kind of project are they working on?
Human: They are trying to add more complex memory structures to Langchain
AI: That sounds like an interesting project! What kind of memory structures are they trying to add?
Human: They are adding in a key-value store for entities mentioned so far in the conversation.
AI: That sounds like a great idea! How will the key-value store help with the project?
Last line:
Human: What do you know about Deven Sam?
You: Finished chain.输出 Deven and Sam are working on a hackathon project together, trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation. They seem to be working hard on this project and have a great idea for how the key-value store can help.检查记忆存储
我们也可以直接检查记忆存储。在下面的示例中我们直接查看它然后通过一些添加信息的示例来观察它的变化。
from pprint import pprint
pprint(conversation.memory.entity_store.store)输出
{Daimon: Daimon is a company founded by Sam, a successful entrepreneur.,Deven: Deven is working on a hackathon project with Sam, which they are entering into a hackathon. They are trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation, and seem to be working hard on this project with a great idea for how the key-value store can help.,Key-Value Store: A key-value store is being added to the project to store entities mentioned in the conversation.,Langchain: Langchain is a project that is trying to add more complex memory structures, including a key-value store for entities mentioned so far in the conversation.,Sam: Sam is working on a hackathon project with Deven, trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation. They seem to have a great idea for how the key-value store can help, and Sam is also the founder of a company called Daimon.}输出
conversation.predict(inputSam is the founder of a company called Daimon.)日志输出 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:
{Daimon: Daimon is a company founded by Sam, a successful entrepreneur., Sam: Sam is working on a hackathon project with Deven, trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation. They seem to have a great idea for how the key-value store can help, and Sam is also the founder of a company called Daimon.}Current conversation:
Human: They are adding in a key-value store for entities mentioned so far in the conversation.
AI: That sounds like a great idea! How will the key-value store help with the project?
Human: What do you know about Deven Sam?
AI: Deven and Sam are working on a hackathon project together, trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation. They seem to be working hard on this project and have a great idea for how the key-value store can help.
Human: Sam is the founder of a company called Daimon.
AI:
Thats impressive! It sounds like Sam is a very successful entrepreneur. What kind of company is Daimon?
Last line:
Human: Sam is the founder of a company called Daimon.
You: Finished chain.输出 Thats impressive! It sounds like Sam is a very successful entrepreneur. What kind of company is Daimon?输入
from pprint import pprint
pprint(conversation.memory.entity_store.store)输出
{Daimon: Daimon is a company founded by Sam, a successful entrepreneur, who is working on a hackathon project with Deven to add more complex memory structures to Langchain.,Deven: Deven is working on a hackathon project with Sam, which they are entering into a hackathon. They are trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation, and seem to be working hard on this project with a great idea for how the key-value store can help.,Key-Value Store: A key-value store is being added to the project to store entities mentioned in the conversation.,Langchain: Langchain is a project that is trying to add more complex memory structures, including a key-value store for entities mentioned so far in the conversation.,Sam: Sam is working on a hackathon project with Deven, trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation. They seem to have a great idea for how the key-value store can help, and Sam is also the founder of a successful company called Daimon.}输入
conversation.predict(inputWhat do you know about Sam?)日志输出 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:
{Deven: Deven is working on a hackathon project with Sam, which they are entering into a hackathon. They are trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation, and seem to be working hard on this project with a great idea for how the key-value store can help., Sam: Sam is working on a hackathon project with Deven, trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation. They seem to have a great idea for how the key-value store can help, and Sam is also the founder of a successful company called Daimon., Langchain: Langchain is a project that is trying to add more complex memory structures, including a key-value store for entities mentioned so far in the conversation., Daimon: Daimon is a company founded by Sam, a successful entrepreneur, who is working on a hackathon project with Deven to add more complex memory structures to Langchain.}Current conversation:
Human: What do you know about Deven Sam?
AI: Deven and Sam are working on a hackathon project together, trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation. They seem to be working hard on this project and have a great idea for how the key-value store can help.
Human: Sam is the founder of a company called Daimon.
AI:
Thats impressive! It sounds like Sam is a very successful entrepreneur. What kind of company is Daimon?
Human: Sam is the founder of a company called Daimon.
AI: Thats impressive! It sounds like Sam is a very successful entrepreneur. What kind of company is Daimon?
Last line:
Human: What do you know about Sam?
You: Finished chain.输出 Sam is the founder of a successful company called Daimon. He is also working on a hackathon project with Deven to add more complex memory structures to Langchain. They seem to have a great idea for how the key-value store can help.参考文献 [1] LangChain官方网站https://www.langchain.com/ [2] LangChain ️ 中文网跟着LangChain一起学LLM/GPT开发https://www.langchain.com.cn/ [3] LangChain中文网 - LangChain 是一个用于开发由语言模型驱动的应用程序的框架http://www.cnlangchain.com/