Your assistant has all its parts: three tools, a question before a return starts, the allow-list, the step limit and the audit log. One thing is missing. The loop at the bottom of the program served one question, and a support assistant answers many customers.
The fix is a function: one question in, one answer out. Everything that belongs to one conversation is created inside the function, so every call starts fresh. Here is the same idea on a small function that has nothing to do with the shop:
def greet(name):
words = ["Hello"]
words.append(name)
return " ".join(words)
print(greet("Ana")) # Hello Ana
print(greet("Sam")) # Hello Sam
The list words is made again on every call, so Ana never shows up in Sam's greeting. Your function answer(question) does the same with messages and rounds: each customer gets their own conversation and their own round counter. Inside a function, return can also end a loop early: it leaves the loop and hands back the answer in one go.
Three customers are waiting in the questions list. When your program is finished, it prints each conversation and then the audit log (the lines in between are left out here):
Customer: I'm chloe@example.com. Please return my last order.
…
Assistant: Done: the return of order 7 has started.
Customer: Order 4 arrived broken. I want my money back.
…
Assistant: I'm sorry: I can't send refunds myself, and order 4 is 53 days old, so it can't be returned either. Returns are accepted for 30 days.
Customer: Has order 5 shipped yet? Keep checking until it has.
…
Assistant: Let me pass this to a person.
Audit log:
{"round": 1, "tool": "find_orders", "input": {"email": "chloe@example.com"}, "status": "ok"}
…
The first customer gets a return, after saying yes. The second asks for a refund: the model's made-up tool is refused, and the shop's 30-day rule stops the return. The third sends the model in circles, and the step limit hands the conversation to a person.
Your task: write the function answer(question) with the tool loop and its limit inside, then loop over questions and print each customer's question and the assistant's answer.
Assignment
Follow the Coding Tutorial to put the whole conversation in answer(question) and run it for three customers.
Hint
Create messages and rounds inside the function, not above it. run_tools now takes three arguments: reply, messages and rounds. The last return sits after the loop, at the level of the while line.
Free plan: 30 tutor messages. PRO is unlimited.
