实体
会话中的实体记忆功能可以记住关于特定实体的给定信息。它提取实体的信息(使用LLM)并随着时间建立其对该实体的了解(还使用LLM)。
先让我们了解如何使用此功能。
from langchain_openai import OpenAI
from langchain.memory import ConversationEntityMemory
llm = OpenAI(temperature=0)memory = ConversationEntityMemory(llm=llm)
_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(llm=llm, return_messages=True)
_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(content='Deven & 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, Anyconversation = ConversationChain(
llm=llm,
verbose=True,
prompt=ENTITY_MEMORY_CONVERSATION_TEMPLATE,
memory=ConversationEntityMemory(llm=llm)
)conversation.predict(input="Deven & Sam are working on a hackathon project") > 进入新的ConversationChain链...
格式化后的提示:
作为一位助手,由OpenAI培训的大型语言模型所支持。
您的设计能够协助各种任务,从回答简单问题到提供关于各种主题的深入解释和讨论。作为语言模型,您可以根据接收到的输入生成类似人类的文本,通过这些文本与人类进行自然对话并提供连贯且与话题相关的回应。
您不断学习和改进,您的能力也在不断发展。您可以处理并理解大量文本,并可利用这些知识对各种问题提供准确和信息丰富的回应。在下面的上下文部分,您可以访问由用户提供的一些个性化信息。此外,您可以根据接收的输入生成自己的文本,让您能够进行讨论并提供关于各种主题的解释和描述。
总的来说,您是一个强大的工具,可以帮助各种任务,并在各种主题上提供有价值的见解和信息。无论人类需要帮助回答具体问题还是只想谈论特定主题,您都在这里提供帮助。
上下文:
{'Deven': 'Deven正在与Sam合作进行一项黑客马拉松项目',
'Sam': 'Sam正在与Deven一起进行黑客马拉松项目'}
当前对话:
用户:Deven & Sam are working on a hackathon project
您:
> 完成对话链。
' That sounds like a great project! What kind of project are they working on?'conversation.memory.entity_store.store {'Deven': 'Deven正在与Sam合作进行一项黑客马拉松项目,他们正在参加一项黑客马拉松赛事。',
'Sam': 'Sam正在与Deven一起进行黑客马拉松项目'}conversation.predict(input="They are trying to add more complex memory structures to Langchain") > 进入新的ConversationChain链...
格式化后的提示:
作为一位助手,由OpenAI培训的大型语言模型所支持。
您的设计能够协助各种任务,从回答简单问题到提供关于各种主题的深入解释和讨论。作为语言模型,您可以根据接收到的输入生成类似人类的文本,通过这些文本与人类进行自然对话并提供连贯且与话题相关的回应。
您不断学习和改进,您的能力也在不断发展。您可以处理并理解大量文本,并可利用这些知识对各种问题提供准确和信息丰富的回应。在下面的上下文部分,您可以访问由用户提供的一些个性化信息。此外,您可以根据接收的输入生成自己的文本,让您能够进行讨论并提供关于各种主题的解释和描述。
总的来说,您是一个强大的工具,可以帮助各种任务,并在各种主题上提供有价值的见解和信息。无论人类需要帮助回答具体问题还是只想谈论特定主题,您都在这里提供帮助。
上下文:
{'Deven': 'Deven正在与Sam合作进行一项黑客马拉松项目,他们正在参加一项黑客马拉松赛事。',
'Langchain': ''}
当前对话:
用户:Deven & Sam are working on a hackathon project
AI: That sounds like a great project! What kind of project are they working on?
用户:They are trying to add more complex memory structures to Langchain
您:
> 完成对话链。
' That sounds like an interesting project! What kind of memory structures are they trying to add?'conversation.predict(input="They are adding in a key-value store for entities mentioned so far in the conversation.") > 进入新的ConversationChain链...
格式化后的提示:
作为一位助手,由OpenAI培训的大型语言模型所支持。
您的设计能够协助各种任务,从回答简单问题到提供关于各种主题的深入解释和讨论。作为语言模型,您可以根据接收到的输入生成类似人类的文本,通过这些文本与人类进行自然对话并提供连贯且与话题相关的回应。
您不断学习和改进,您的能力也在不断发展。您可以处理并理解大量文本,并可利用这些知识对各种问题提供准确和信息丰富的回应。在下面的上下文部分,您可以访问由用户提供的一些个性化信息。此外,您可以根据接收的输入生成自己的文本,让您能够进行讨论并提供关于各种主题的解释和描述。
总的来说,您是一个强大的工具,可以帮助各种任务,并在各种主题上提供有价值的见解和信息。无论人类需要帮助回答具体问题还是只想谈论特定主题,您都在这里提供帮助。
上下文:
{'Deven': 'Deven正在与Sam合作进行一项黑客马拉松项目,他们正在参加一项黑客马拉松赛事。',
'Langchain': 'Langchain是一个尝试添加更复杂的内存结构,包括一个key-value存储,用于保存迄今为止在对话中提到的实体。'}
当前对话:
用户:Deven & Sam are working on a hackathon project
AI: That sounds like a great project! What kind of project are they working on?
用户: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?
用户:They are adding in a key-value store for entities mentioned so far in the conversation.
您:
> 完成对话链。
' That sounds like a great idea! How will the key-value store help with the project?'conversation.predict(input="What do you know about Deven & Sam?") > 进入新的ConversationChain链...
格式化后的提示:
作为一位助手,由OpenAI培训的大型语言模型所支持。
您的设计能够协助各种任务,从回答简单问题到提供关于各种主题的深入解释和讨论。作为语言模型,您可以根据接收到的输入生成类似人类的文本,通过这些文本与人类进行自然对话并提供连贯且与话题相关的回应。
您不断学习和改进,您的能力也在不断发展。您可以处理并理解大量文本,并可利用这些知识对各种问题提供准确和信息丰富的回应。在下面的上下文部分,您可以访问由用户提供的一些个性化信息。此外,您可以根据接收的输入生成自己的文本,让您能够进行讨论并提供关于各种主题的解释和描述。
总的来说,您是一个强大的工具,可以帮助各种任务,并在各种主题上提供有价值的见解和信息。无论人类需要帮助回答具体问题还是只想谈论特定主题,您都在这里提供帮助。
上下文:
{'Deven': 'Deven正在与Sam合作进行一项黑客马拉松项目,他们正在参加一项黑客马拉松赛事。他们试图将更复杂的内存结构添加到Langchain中,包括用于保存迄今为止在对话中提到的实体的key-value存储。',
'Sam': 'Sam正在与Deven一起进行黑客马拉松项目。他们试图将更复杂的内存结构添加到Langchain中,包括用于保存迄今为止在对话中提到的实体的key-value存储。'}
当前对话:
用户:Deven & Sam are working on a hackathon project
AI: That sounds like a great project! What kind of project are they working on?
用户: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?
用户: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?
用户:What do you know about Deven & Sam?
您:
> 完成对话链。
' 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(input="Sam is the founder of a company called Daimon.")
> 进入新的ConversationChain链...
格式化后的提示:
作为一位助手,由OpenAI培训的大型语言模型所支持。
您的设计能够协助各种任务,从回答简单问题到提供关于各种主题的深入解释和讨论。作为语言模型,您可以根据接收到的输入生成类似人类的文本,通过这些文本与人类进行自然对话并提供连贯且与话题相关的回应。
您不断学习和改进,您的能力也在不断发展。您可以处理并理解大量文本,并可利用这些知识对各种问题提供准确和信息丰富的回应。您可以根据接收的输入生成自己的文本,允许您进行讨论并提供对各种主题的解释和描述。
总的来说,您是一个强大的工具,可以帮助各种任务,并提供对各种主题的有价值见解和信息。无论是回答特定问题还是关于特定主题的对话,您都在这里提供帮助。
Context:
{'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.',
'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(input="What 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:
That's 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: That's 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.'