The assistant that remembers, end to end
Everything in the course, in one file. This is the assistant promised in lesson 0: it remembers a customer across conversations, and it runs with no API key.
Nothing here is new. Every piece was built alone in an earlier lesson and the table says which.
| Piece | What it does | Lesson |
|---|---|---|
| pretend_mem0.py | The model, the embedder and the config | 5, 6 and 7 |
| answer() | The assistant, unchanged since lesson 1 | 1 |
| search before, add after | Memory around the reply | 21 |
| user_id | Keeps one customer apart from another | 12 |
| metadata | Where the memory came from | 13 |
| The three checks | Proof that it stored, found and separated | 22 |
Two conversations, a week apart
from shop import assistant, memory
shop = memory()
print(assistant(shop, "ravi", "I prefer email updates, not SMS."))
print(assistant(shop, "ravi", "Deliver to my office, not my home."))
print()
print(assistant(shop, "ravi", "Can you send me an update?"))The first two turns teach it something. The third is the conversation that went wrong in lesson 1, and this time the assistant knows how Ravi wants to be contacted before it answers.
What it kept
from shop import assistant, memory
shop = memory()
for said in ["I prefer email updates, not SMS.", "Deliver to my office, not my home.",
"Thanks, that is all."]:
assistant(shop, "ravi", said)
kept = shop.get_all(filters={"user_id": "ravi"})["results"]
for text in sorted(m["memory"] for m in kept):
print("-", text)Three messages in, and the pleasantry is not in the list. That is the model earning its place: the dictionary in lesson 2 would have kept it forever.
The failure worth showing
from shop import assistant, memory
shop = memory()
assistant(shop, "ravi", "Deliver to my office.")
print(assistant(shop, "priya", "Where is my order?"))
print(shop.search("deliver", filters={"user_id": "priya"})["results"])Priya gets no reference to an office, because the search was filtered to her. Drop that filter and the assistant tells one customer another customer's address, which is the single worst thing a memory layer can do and the reason lesson 22 tests for it.
Where to go next
Swap in a real model with the two lines from lesson 19 and keep this file as the test fixture. Point the store at a directory that persists, from lesson 18. Then add a run_id so that what is true in one conversation does not leak into the next.
- Add a fourth message that contradicts an earlier one and look at what is stored.
- Give the assistant a
run_idand check that a new session does not see the last one. - Write the three checks from lesson 22 against this file and run them.
Slow is fine. Stopping is the only problem.