update and delete
People change their minds, and a memory layer that cannot keep up is worse than none: it answers confidently with last year's answer.
from pretend_mem0 import memory
shop = memory()
shop.add("Deliver to my office.", user_id="ravi")
stored = shop.get_all(filters={"user_id": "ravi"})["results"][0]
print(shop.update(stored["id"], text="Deliver to the office reception."))
print(shop.get(stored["id"])["memory"])update takes the id and the new text, and says it worked. The memory is rewritten in place, keeping its id, and re-embedded so searches find it by its new words rather than its old ones.
Deleting one
from pretend_mem0 import memory
shop = memory()
shop.add("Deliver to my office.", user_id="ravi")
stored = shop.get_all(filters={"user_id": "ravi"})["results"][0]
shop.delete(stored["id"])
print(shop.get(stored["id"]))
print(len(shop.get_all(filters={"user_id": "ravi"})["results"]), "left")Gone, and get returns None rather than raising. delete_all takes identifiers instead of an id and clears everything matching, which is what a forget me button calls.
The update you did not ask for
Mem0 also updates memories on its own. Every add is compared against what is already stored, and when the model decides the new sentence replaces an old one, the result carries UPDATE rather than ADD. That is why add returns an event at all.
The stand-in model in this course never does that: it has no opinion about whether two sentences conflict, so it always adds. A real model does, and it is worth knowing before it surprises you, because a memory you did not delete can still change under you.
event field in the result rather than ignoring it.Deciding who wins
When a fact is critical, do not leave it to the model. Search for the existing memory, delete it, and add the new one, so the outcome is code you wrote rather than a judgement you did not see.
- Update a memory and then search for a word that was only in the old text.
- Delete a memory and call
geton its id. - Add a sentence that flatly contradicts a stored memory and look at the
eventyou get back.
Every expert started right here.