1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
24 small wins to finish your pathNext lesson →
metadata, and filtering on it
Identifiers say whose memory it is. Metadata says what kind of memory it is, and it is the difference between a search that finds the right thing and one that finds four things.
from pretend_mem0 import memory
shop = memory()
shop.add("Deliver to my office.", user_id="ravi",
metadata={"topic": "delivery"})
stored = shop.get_all(filters={"user_id": "ravi"})["results"][0]
print(stored["memory"], "|", stored["metadata"])Any dictionary you like, stored beside the memory and given back with it. Mem0 does not interpret it; it is yours.
Filtering on it
from pretend_mem0 import memory
shop = memory()
shop.add("Deliver to my office.", user_id="ravi", metadata={"topic": "delivery"})
shop.add("I prefer email updates.", user_id="ravi", metadata={"topic": "contact"})
found = shop.search("office", filters={"user_id": "ravi", "topic": "delivery"})
for hit in found["results"]:
print(hit["memory"])The filter and the question work together: the filter narrows the set, then the question ranks what is left. One without the other is weaker than both.
What to put in it
- A topic or category, so an assistant handling a delivery question does not have to read preferences about anything else.
- Where the fact came from: a chat, a form, an import. You will want this the first time a memory is wrong.
- How confident you are, if some of your memories are guesses and others are settings the user typed.
- Anything you would otherwise have to parse back out of the sentence.
Metadata is the cheapest thing in this course and the most useful in a year. A store with a hundred thousand memories and no tags can only be searched by meaning, and meaning is fuzzy. A store with tags can be narrowed to forty memories first, and then searched by meaning.
It does not change the text
Metadata is not embedded. Tagging a memory topic: delivery does not make it match the word delivery in a search; only the memory text is embedded. The tag filters, the text ranks, and confusing the two produces searches that quietly return nothing.
Try it yourself
- Tag two memories with the same topic and filter on it with an empty-ish query.
- Filter on a metadata value that no memory has, and see what comes back.
- Search for a word that appears only in the metadata and confirm it does not match.
Slow is fine. Stopping is the only problem.