Tools · capstone
Capstone project: Project: Let the Assistant Look Up a Note
Lessons 2 to 7 built every piece of a tool round, and the demonstration has been gluing them together by hand. This project puts the round into one function that the study assistant can call with a question:
client = make_client()
print(answer_with_tools("What do the notes say about copying a list?", client=client))
The lists note says that two names can refer to the same list, so a change through one shows through the other. Use scores.copy() or list(scores) for an independent copy. (03-lists)
The pieces you have
| Step | Your code |
|---|---|
| Describe the tools | TOOL_DESCRIPTIONS (Lesson 2) |
| Read the calls from a reply | read_tool_calls(response) (Lesson 3) |
| Find and run a tool | TOOLS and run_tool(call, tools) (Lessons 4 and 6) |
| Record the model’s request | assistant_turn(calls) (Lesson 5) |
| Run every call and wrap the results | tool_messages(calls, tools) (Lesson 7) |
answer_with_tools adds the two requests around them, and decides what to return.
Build answer_with_tools
answer_with_tools(question, *, client) in tools.py:
Start the conversation:
messages = [{"role": "system", "content": TOOLS_SYSTEM}, {"role": "user", "content": question}].TOOLS_SYSTEMcomes fromprompts.pyand tells the model to look things up and name the note it used. The scripted model answers these questions the same way with or without it; a live model reads it.Send it:
client.chat.completions.create(model=MODEL, messages=messages, tools=TOOL_DESCRIPTIONS).Read the calls with
read_tool_calls. If there are none, the model answered straight away: return the reply’scontent, or""when it isNone.Otherwise, add
assistant_turn(calls)and then the messages fromtool_messages(calls, TOOLS)to the conversation. You can build a new list or append tomessages; either is fine. Send it withcreateagain, with the samemodelandtools.If that second reply asks for tools again, return
NEEDS_MORE_LOOKUPS, the fixed message at the top oftools.py. Otherwise return itscontent, or""when it isNone.
Use the client you are given; do not build one with make_client() inside the function. Use the module’s TOOLS and TOOL_DESCRIPTIONS by name, as above. The checks replace both with their own tools and a folder of notes you have not seen, and they script the model’s replies, including calls to tools that do not exist and arguments that are not JSON. Your run_tool already turns those into error results, so answer_with_tools needs no try of its own.
Why one round
Step 5 gives up after one round of tools. A model may need more: a live model might first call list_notes to find an id, and only then ask for read_note. answer_with_tools cannot know how many rounds a question needs, so for now it stops and says so instead of guessing. Chapter 7 replaces step 5 with a loop that keeps going, with limits of its own.
Run it
Run python tools.py. The demonstration asks the study assistant two questions, one that needs one note and one that needs two:
The study assistant with tools:
You: What do the notes say about copying a list?
Assistant: The lists note says that two names can refer to the same list, so a change through one shows through the other. Use scores.copy() or list(scores) for an independent copy. (03-lists)
You: Compare the notes on lists and dictionaries.
Assistant: Lists keep items in order and you reach them by position, such as scores[0]. Dictionaries map keys to values and you reach a value by its key, such as ages["Ana"]. Both are mutable, and both have a copy() method for an independent copy. (03-lists, 06-dictionaries)
The demonstration shows the happy path. Before you check your work, think through what it does not show: a model that answers without asking for any tool, a call to a tool that does not exist, arguments that are not JSON, and a second reply that asks for tools again. Each of them is covered by one of the steps above or by your run_tool. If you get stuck, ask Monty for a hint about one case without asking for the whole function.
Try it live
This step is optional. answer_with_tools reads the module’s MODEL each time it runs, so a short script can point it at the live model without changing tools.py. Create a file called try_live.py next to tools.py with these lines, then run python try_live.py in the
import tools
tools.MODEL = "pl-live"
print(tools.answer_with_tools("What do the notes say about copying a list?", client=tools.make_client()))
The live model writes its own answer from the note your program sends it, so the wording will differ from the scripted one, and it may decide it needs more than one round and get NEEDS_MORE_LOOKUPS. A 403 means your account has no live access, and a 429 means you reached the limit for now. A read timeout is a normal outcome too: a live model can think for longer than this course’s read bound. Delete try_live.py when you are done.
Where this goes next
The study assistant can now look things up in its notes and say which note it used. Chapter 7 turns the single round into an agent loop: the model asks, your program runs the tools, and the loop goes on until the model answers or a limit is reached.
In answer_with_tools, the second reply asks for another tool. What does the function do, and why?
A question needs no notes, and the first reply is a plain answer with no tool calls. How many requests does answer_with_tools make?
Task
In tools.py, replace the unfinished body of answer_with_tools(question, *, client).
messages = [{"role": "system", "content": TOOLS_SYSTEM}, {"role": "user", "content": question}].Call
client.chat.completions.create(model=MODEL, messages=messages, tools=TOOL_DESCRIPTIONS).If
read_tool_callsfinds no calls, return the reply’s content, or""when it isNone.Otherwise add
assistant_turn(calls)and the messages fromtool_messages(calls, TOOLS)to the conversation, and callcreateagain with the samemodelandtools.If that reply asks for tools again, return
NEEDS_MORE_LOOKUPS. Otherwise return its content, or""when it isNone.
Use the client you are given and the module’s TOOLS and TOOL_DESCRIPTIONS. Make at most two requests. Run python tools.py to ask the study assistant two questions, then check your work.