Command Palette

Search for a command to run...

Hectal
Phase 7: Tools and Function Calling
LLM Engineering · Exercise 3 of 6 PreviousNext
Intermediate

Write the tool loop

Write run_loop(script, registry, max_steps). script is a list of scripted assistant messages (the fake model's replies, one per step). registry maps a tool name to a function that takes the parsed arguments dict and returns a value.

Each step: take the next scripted reply and append it to a list messages. If it has no tool_calls, return (reply["content"], messages). Otherwise, for each call, run the function and append a tool message {"role": "tool", "tool_call_id": <id>, "content": json.dumps(result)}. If max_steps steps pass without a final answer, return (None, messages).

What Check tests

  1. Returns the final text
  2. Messages are assistant, tool, assistant
  3. The tool message has the call's id and the JSON result
  4. The step limit stops the loop with None
  5. Several calls in one reply each get a tool message

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.