ConversationEntityMemory#
- class langchain.memory.entity.ConversationEntityMemory[source]#
Bases:
BaseChatMemory
Entity extractor & summarizer memory.
Extracts named entities from the recent chat history and generates summaries. With a swappable entity store, persisting entities across conversations. Defaults to an in-memory entity store, and can be swapped out for a Redis, SQLite, or other entity store.
- param ai_prefix: str = 'AI'#
- param chat_history_key: str = 'history'#
- param chat_memory: BaseChatMessageHistory [Optional]#
- param 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:')#
- param entity_store: BaseEntityStore [Optional]#
- param 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.\nThe 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:')#
- param human_prefix: str = 'Human'#
- param input_key: str | None = None#
- param k: int = 3#
- param llm: BaseLanguageModel [Required]#
- param output_key: str | None = None#
- param return_messages: bool = False#
- async aclear() None #
Clear memory contents.
- Return type:
None
- async aload_memory_variables(inputs: Dict[str, Any]) Dict[str, Any] #
Async return key-value pairs given the text input to the chain.
- Parameters:
inputs (Dict[str, Any]) – The inputs to the chain.
- Returns:
A dictionary of key-value pairs.
- Return type:
Dict[str, Any]
- async asave_context(inputs: Dict[str, Any], outputs: Dict[str, str]) None #
Save context from this conversation to buffer.
- Parameters:
inputs (Dict[str, Any]) –
outputs (Dict[str, str]) –
- Return type:
None
- load_memory_variables(inputs: Dict[str, Any]) Dict[str, Any] [source]#
Returns chat history and all generated entities with summaries if available, and updates or clears the recent entity cache.
New entity name can be found when calling this method, before the entity summaries are generated, so the entity cache values may be empty if no entity descriptions are generated yet.
- Parameters:
inputs (Dict[str, Any]) –
- Return type:
Dict[str, Any]
- save_context(inputs: Dict[str, Any], outputs: Dict[str, str]) None [source]#
Save context from this conversation history to the entity store.
Generates a summary for each entity in the entity cache by prompting the model, and saves these summaries to the entity store.
- Parameters:
inputs (Dict[str, Any]) –
outputs (Dict[str, str]) –
- Return type:
None
- property buffer: List[BaseMessage]#
Access chat memory messages.