Putting memory in the reply loop
Every call so far has been made by hand. An assistant with memory does two things around every reply, and the order of them is the whole design.
Search first, add last. Add first and the assistant retrieves the sentence the customer typed one line ago and solemnly reads it back to them.
The assistant, unchanged
This is the lookup from lesson 1, with one more entry. It knows nothing about memory and it is not going to.
from pretend_mem0 import memory
HANDBOOK = {
"late": "Sorry about that. I can chase order A17 for you.",
"update": "I can send you updates about your order.",
"refund": "Refunds take five working days.",
}
def answer(said):
for word, reply in HANDBOOK.items():
if word in said.lower():
return reply
return "I can help with orders and refunds."The three lines that wrap it
def assistant(shop, user, said):
known = shop.search(said, filters={"user_id": user})["results"]
reply = answer(said)
if known:
reply += " I have on file: " + known[0]["memory"] + "."
shop.add(said, user_id=user, metadata={"source": "chat"})
return replySearch, then reply, then add. The reply is still the same dictionary lookup; what changed is what the assistant knows while making it. Save both halves as shop.py.
from shop import assistant, memory
shop = memory()
print(assistant(shop, "ravi", "I prefer email updates, not SMS."))
print(assistant(shop, "ravi", "Can you send me an update?"))This is the conversation from lesson 1, and the second answer is different now. The assistant looked up what it knew about updates, found what Ravi said a moment ago, and used it.
Why the memory goes in the reply and not around it
Here the memory is appended to the sentence, which is crude on purpose: it makes what happened visible. With a real model the memories go into the prompt as context and the model writes one natural reply. The retrieval is identical; only what you do with the result changes.
- Swap the two calls so
addruns beforesearch, and read the second reply. - Add a third turn and see which memory comes back.
- Store with a
run_idas well, and check that a new session starts clean.
Little by little, you're building something great.