Start learning free

LESSON 103 OF 129 · AI ENGINEERING: CONTEXT ENGINEERING

Remember the Customer Between Chats

Practice

A new chat starts with an empty messages list. Everything from the last chat is gone, so the model can't know who the customer is. A summary doesn't help: it lives inside one chat. What helps is memory: a few facts saved outside the chat and put back into the context when the customer returns.

A file as memory: a JSON file can hold one dictionary. json.load(file) reads the file into a dictionary, and json.dump(data, file) writes a dictionary back. Here is the idea on another file, a game's scores:

with open("scores.json") as file:
    scores = json.load(file)
scores["ana"] = 12
with open("scores.json", "w") as file:
    json.dump(scores, file)

Opening a file with "w" empties it first. So you always load the whole dictionary, change it, and write all of it back. Before you read a key that may be missing, check it with in: a missing key stops the program with a KeyError.

In this lesson the memory is memory.json. Its keys are customer emails, its values are the facts about each customer, and Tom is already in it. When Maya's chat ends, her facts are saved under her email. When she opens a new chat, system_for(email) reads the file and adds only her entry to the system prompt. A new customer gets the plain prompt. The finished program prints:

Facts: {'name': 'Maya', 'open_order': 1042, 'contact': 'text message'}
--- Three days later, a new chat ---
Shop: Sure, Maya: send me the new address for order 1042, and I'll confirm the change by text message.
In memory.json: tom@example.com {'name': 'Tom', 'open_order': 1038, 'contact': 'email'}
In memory.json: maya@example.com {'name': 'Maya', 'open_order': 1042, 'contact': 'text message'}
System prompt of the new chat: You are the Corner Shop's assistant. Answer in one short sentence. What we know about this customer: {"name": "Maya", "open_order": 1042, "contact": "text message"}
System prompt for a new customer: You are the Corner Shop's assistant. Answer in one short sentence.

Your task: the chat with Maya and the model call that extracts her facts are already written. Load memory.json, store facts under email, and write the dictionary back to the file. Then, inside system_for, load the file again and, if the email has an entry, add that customer's facts to the system prompt.


Assignment
Follow the Coding Tutorial to save Maya's facts to memory.json and load them into the system prompt of her next chat.


Hint
The key is the variable email, not the text "email". Write the file with "w" only after you loaded the whole dictionary. In system_for, check if email in saved: before you read saved[email], and turn the facts into text with json.dumps(...).


STEP 1 OF 9
    Ctrl + Enter to check