Entity Memory
Moving from Chat Logs to Database Records. Structured facts.
1. The "Needle in a Haystack" Problem
Chat history is unstructured text. To find "What is Bob's favorite color?", the model must read 10,000 words of logs. This is inefficient and prone to error.
Entity Memory solves this by extracting facts into a structured Key-Value store.
2. Extraction as State Stabilization
Instead of storing:
"I was born in 1990 and I love dogs."
Entity Memory extracts:
{
"entity": "User",
"facts": {
"birth_year": 1990,
"likes": ["dogs"]
}
}
This is Stable State. It doesn't scroll away. It doesn't get summarized into oblivion.
3. How It Works
- Extract: An "Extractor Chain" runs on every user input. It identifies entities (People, Places, Preferences).
- Update: It updates the in-memory store for that specific entity.
- Inject: When the user speaks again, we inject specific facts about mentioned entities into the prompt.
from langchain.memory import ConversationEntityMemory
memory = ConversationEntityMemory(llm=llm)
_input = {"input": "My friend Alice likes pizza."}
memory.load_memory_variables(_input)
# Result: Updated 'Alice' entity with fact 'likes pizza'4. The Trade-offs
Pros:
- Permanent recall of specific facts.
- Very token efficient (only injects relevant entities).
Cons:
- Rigid. Ideally needs a schema.
- Extraction errors (Model might extract "Alice" as a place).
5. Summary
For complex assistants (Travel Agents, RPG NPCs), unstructured logs are not enough. You need a "Character Sheet" for your user.
Key Intuition: "Don't remember the conversation. Remember the facts."