langchain.memory.entity.ConversationEntityMemory

class langchain.memory.entity.ConversationEntityMemory[源代码]

基类: BaseChatMemory

实体提取和摘要存储。

从最近的聊天历史中提取命名实体并生成摘要。具有可交换的实体存储,可以在对话之间持久化实体。默认为内存中的实体存储,可以替换为Redis、SQLite或其他实体存储。

参数 ai_prefix: str = 'AI'
参数 chat_history_key: str = 'history'
参数 chat_memory: BaseChatMessageHistory [可选]
参数 entity_cache: List[str] = []
param entity_extraction_prompt: BasePromptTemplate = PromptTemplate(input_variables=['history', 'input'], template='You are an AI assistant reading the transcript of a conversation between an AI and a human. Extract all of the proper nouns from the last line of conversation. As a guideline, a proper noun is generally capitalized. You should definitely extract all names and places.\n\nThe conversation history is provided just in case of a coreference (e.g. "What do you know about him" where "him" is defined in a previous line) -- ignore items mentioned there that are not in the last line.\n\nReturn the output as a single comma-separated list, or NONE if there is nothing of note to return (e.g. the user is just issuing a greeting or having a simple conversation).\n\nEXAMPLE\nConversation history:\nPerson #1: how\'s it going today?\nAI: "It\'s going great! How about you?"\nPerson #1: good! busy working on Langchain. lots to do.\nAI: "That sounds like a lot of work! What kind of things are you doing to make Langchain better?"\nLast line:\nPerson #1: i\'m trying to improve Langchain\'s interfaces, the UX, its integrations with various products the user might want ... a lot of stuff.\nOutput: Langchain\nEND OF EXAMPLE\n\nEXAMPLE\nConversation history:\nPerson #1: how\'s it going today?\nAI: "It\'s going great! How about you?"\nPerson #1: good! busy working on Langchain. lots to do.\nAI: "That sounds like a lot of work! What kind of things are you doing to make Langchain better?"\nLast line:\nPerson #1: i\'m trying to improve Langchain\'s interfaces, the UX, its integrations with various products the user might want ... a lot of stuff. I\'m working with Person #2.\nOutput: Langchain, Person #2\nEND OF EXAMPLE\n\nConversation history (for reference only):\n{history}\nLast line of conversation (for extraction):\nHuman: {input}\n\nOutput:')
参数 entity_store: BaseEntityStore [可选]
参数 : entity_summarization_prompt : BasePromptTemplate = PromptTemplate(input_variables=['entity', 'history', 'input', 'summary'], template='You are an AI assistant helping a human keep track of facts about relevant people, places, and concepts in their life. Update the summary of the provided entity in the "Entity" section based on the last line of your conversation with the human. If you are writing the summary for the first time, return a single sentence. The update should only include facts that are relayed in the last line of conversation about the provided entity, and should only contain facts about the provided entity. \n\nIf there is no new information about the provided entity or the information is not worth noting (not an important or relevant fact to remember long-term), return the existing summary unchanged. \n\nFull conversation history (for context):\n{history}\n\nEntity to summarize:\n{entity}\n\nExisting summary of {entity}:\n{summary}\n\nLast line of conversation:\nHuman: {input}\nUpdated summary:')
参数 : human_prefix : str = 'Human'
参数 : input_key : Optional[str] = None
参数 : k : int = 3
参数 : llm : BaseLanguageModel [Required]
参数 : output_key : Optional[str] = None
参数 : return_messages : bool = False
async aclear() None

清除内存内容。

返回类型

None

async aload_memory_variables(inputs: Dict[str, Any]) Dict[str, Any]

异步返回给定文本输入到链中的键值对。

参数

inputs (Dict[str, Any]) – 链的输入。

返回

键值对字典。

返回类型

Dict[str, Any

async asave_context(inputs: Dict[str, Any], outputs: Dict[str, str]) None

将本对话的上下文保存到缓冲区。

参数
  • inputs (Dict[str, Any]) –

  • outputs (Dict[str, str]) –

返回类型

None

clear() None[source]

清除内存内容。

返回类型

None

load_memory_variables(inputs: Dict[str, Any]) Dict[str, Any][source]

返回聊天历史和所有类别的实体摘要(若有),并更新或清除最近实体缓存。

在生成实体摘要之前调用此方法时,可以找到新的实体名称,因此如果尚未生成实体描述,实体缓存值可能为空。

参数

inputs (Dict[str, Any]) –

返回类型

Dict[str, Any

save_context(inputs: Dict[str, Any], outputs: Dict[str, str]) None[source]

将此对话历史的上下文保存到实体存储中。

通过提示模型为实体缓存中的每个实体生成摘要,并将这些摘要保存到实体存储中。

参数
  • inputs (Dict[str, Any]) –

  • outputs (Dict[str, str]) –

返回类型

None

属性 buffer: List[BaseMessage]

访问聊天记忆消息。