add(): what gets kept and what gets thrown away
add is the call that does the most work and shows the least. One line of conversation goes in; what comes back is a decision the model made.
from pretend_mem0 import memory
shop = memory()
result = shop.add("My order A17 is late. Thanks for the help!", user_id="ravi")
for stored in result["results"]:
print(stored["event"], "|", stored["memory"])Two sentences went in and one memory came out. Thanks for the help! was dropped, because the model decided it was not worth keeping, which is the judgement the dictionary in lesson 2 could not make.
What comes back
add returns a dictionary with a results list, one entry per decision. Each entry carries the memory text, an event saying what happened to it, and an id you can use later.
from pretend_mem0 import memory
shop = memory()
stored = shop.add("Deliver to my office reception.", user_id="ravi")["results"][0]
print(sorted(stored))
print(stored["event"])event is ADD here. It can also be UPDATE or DELETE, because Mem0 compares what it just heard against what it already knows, which is lesson 15.
Messages, not just strings
A string is a convenience. The real input is a list of messages, which is what you have in a chat application, and it lets the model use the assistant's half of the conversation too.
from pretend_mem0 import memory
shop = memory()
result = shop.add([
{"role": "user", "content": "Where is my order?"},
{"role": "assistant", "content": "Order A17 is arriving on Friday."},
], user_id="ravi")
for stored in result["results"]:
print(stored["memory"])shop.add("text", "ravi") raises a TypeError rather than guessing that the second string was a user id. You always write user_id="ravi".And at least one identifier is required. Call add with no user_id, agent_id or run_id and Mem0 refuses, because a memory belonging to nobody can never be found again. Lesson 12 is about which to use.
- Add a sentence of pure politeness on its own and see whether anything is stored.
- Call
addwithoutuser_idand read the error. - Put a fact in the assistant's message rather than the user's and see whether it survives.
Slow is fine. Stopping is the only problem.