AI ordering agent project · Chapter 8 of 12
Memory of preferences
By the end of this chapter your agent remembers what a member eats across every conversation: "I am Jain, no onion and garlic. Please remember that" is saved in one row of Postgres with an upsert that merges the foods in SQL, the member's diet goes into the system message of every turn, and your code, not the model, makes sure find_dish only shows a Jain member Jain dishes. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Remembering
- 3 tasks
- Python
- In the all labs plan
The first 2 chapters of AI ordering agent project are free. This one comes with the all labs plan.
Why you need this
The first of the pilot week's 19 wrong orders is the one Farah still talks about: a Jain member got onion. She had said she was Jain, weeks earlier, on WhatsApp, to someone who was not on shift that day.
Chapter 6 gave the agent a memory, but only of one conversation. Tara says "I am Jain" on Monday; on Wednesday she starts a new conversation and the agent knows nothing about her. Members will not repeat their diet every time, and they should not have to. What the agent needs is a different kind of memory: a few facts about the member, kept across conversations, used on every turn. And because a Jain member's lunch depends on it, the most important use must not depend on the model choosing to follow a sentence.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
preferences.py: remember what the member eats
In agent/preferences.py, write recall(db, user_id): {"diet": row.diet, "avoid": list(row.avoid)} for the member's row of agent_preferences, or {"diet": None, "avoid": []} when there is none. Then remember(db, user_id, diet=None, avoid=None): one statement, insert(Preference).values(user_id, diet, avoid=sorted(set(words(avoid)))) with on_conflict_do_update(index_elements=["user_id"], set_={"diet": func.coalesce(stmt.excluded.diet, Preference.diet), "avoid": MERGED, "updated_at": func.now()}); commit, and return recall(...).
The Check recalls Advait with nothing saved, remembers Tara as jain avoiding "Onion, garlic " (garlic, onion), then "potato, onion" (the diet kept, three foods), then veg (the foods kept), and wants one row for her. Then a transaction of its own adds egg to Advait's foods and commits while your remember adds paneer: all three must be kept.
- Task 2
preferences.py: a line for the system message
In agent/preferences.py, write preference_note(prefs): None when there is no diet and nothing to avoid; otherwise one sentence: "The member eats jain food and avoids garlic and onion. Choose dishes that fit." With only a diet, "The member eats veg food. Choose dishes that fit."; with only foods, "The member avoids egg. Choose dishes that fit."; three or more foods are joined like "garlic, onion and potato".
The Check compares your sentences for five sets of preferences, then saves Bhavna's (jain, onion) and wants the system message of her next chat to hold the note.
- Task 3 · interview question
Interview: preferences.py, use the memory in the tools
In agent/preferences.py, write apply_preferences(name, args, prefs): for find_dish whose args have no diet (None or missing), when prefs has a diet, return a new dict with that diet; otherwise return args unchanged (another tool, a diet already given, no diet saved, no prefs). Never change the dict you were given.
The Check calls it for six cases, then, through the app, Advait says "I am Jain, no onion and garlic. Please remember that" (the agent calls save_preference) and then "What is on the menu in Thane?": the model calls find_dish without a diet, and its result must hold only jain dishes.
Kunal will ask: the system message already tells the model that Tara eats Jain food. Why fill the diet in in code?
What you will learn
- Facts, not messages: one row per member, read on every turn.
- The upsert: INSERT ... ON CONFLICT DO UPDATE, and excluded, in SQL and in SQLAlchemy.
- coalesce and arrays: keep the old value when no new one is given; merge two lists of foods inside the statement.
- The lost update: why a merge made from a read you did earlier loses data, shown with two transactions.
- The memory in the prompt: one plain sentence in the system message.
- The memory in code: filling in the diet for find_dish, because a sentence is not a filter.
More in Remembering
Build "Memory of preferences" yourself
DabbaDrop members want to order by chat. You build the agent that books their tiffins through DabbaDrop's own API, and asks before it pays. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of AI ordering agent project are free. This one comes with the all labs plan.