Updating and deleting memories
Pass existing memories and the model can change them instead of adding duplicates: PatchDoc edits a memory by id, RemoveDoc deletes one when deletes are enabled.
update = manager.invoke({"messages": [{"role": "user", "content": "Actually, text me instead."}], "existing": existing})
for memory in update:
changed = "same id" if memory.id in dict(existing) else "new id"
print(changed, "|", memory.content.content)existing is a list of (id, content) pairs. The contact memory kept its id and changed its text: the model called PatchDoc with that id and a JSON Patch replace on /content. The other two memories come back unchanged, so the result is the whole new state, not only the changes.
Deletes
forgetful = create_memory_manager(MemoryModel(), enable_deletes=True)
result = forgetful.invoke({"messages": [{"role": "user", "content": "Please forget my name."}], "existing": existing})
for memory in result:
print(type(memory.content).__name__, "|", getattr(memory.content, "content", ""))Deleting is off by default. With enable_deletes=True, the model gets a RemoveDoc tool, and a deleted memory comes back as a RemoveDoc with its id instead of a Memory. Your code then deletes it from wherever you keep memories. A store manager, lesson 11, does that for you.
enable_updates=False and add new memories instead.- Send "My name is Priya" with
existingand see which memory changes. - Pass
enable_updates=Falseand send "Text me instead" again. - Delete a memory from a Python dict of memories using the
RemoveDocids.
You understood something today that you didn't yesterday.