create_memory_manager and first memories
create_memory_manager wraps a chat model. Give it a conversation and it returns the memories the model decided to keep, each with an id.
A conversation, as a list of messages in the role-and-content format chat APIs use:
conversation = [
{"role": "user", "content": "Hi, my name is Asha. Order A-1001 arrived broken."},
{"role": "assistant", "content": "Sorry to hear that. How should we contact you?"},
{"role": "user", "content": "Please email me, I work nights."},
]from langmem import create_memory_manager
from memory_model import MemoryModel
manager = create_memory_manager(MemoryModel())
memories = manager.invoke({"messages": conversation})
for memory in memories:
print(memory.content)MemoryModel is a stand-in chat model, read in lesson 4; any LangChain chat model goes in its place. create_memory_manager takes a model and returns a manager. invoke takes a dictionary with messages and returns a list of ExtractedMemory. Each content is a Memory, a Pydantic model with one field, also called content.
Three facts came out of three messages. "I work nights" did not: the stand-in has no rule for it. A real model decides for itself, and would probably keep it.
memory = memories[0]
print(type(memory).__name__, type(memory.content).__name__)
print(len(memory.id), memory.id.count("-"))Every memory gets a UUID, so later updates can say which memory they change. The manager keeps nothing: it is a function from a conversation, and optionally existing memories, to memories. Saving them is your job, or a store's, lesson 11.
- Add a message saying
"I live in Pune"and extract again. - Pass an empty conversation.
- Print
memory.content.model_dump().
You understood something today that you didn't yesterday.