0%
⭐ 0 XPMap
Chapter 3 · Quest 3 of 5 · 10 XP

Tool Picker

🧭 Here's the trick behind every AI agent: the model picks the tool. You show it your tools and a question, and it answers with JSON: which tool to use, and what to pass it.

import pip
tools = {"calc": calc, "weather": weather}
print(pip.ask_with_tools("What is 2 plus 3?", tools))
{"tool": "calc", "args": {"a": 2, "b": 3, "op": "+"}}

Your code then runs that tool. ** unpacks a dict into named arguments, so calc(**{"a": 2, "b": 3, "op": "+"}) is the same as calc(a=2, b=3, op="+"). Your two tools from the last quests are ready in the editor.

Instructions

  1. Make a dict tools with "calc" and "weather" pointing at your two functions (no brackets after their names).
  2. Store json.loads(pip.ask_with_tools("What is 6 times 7?", tools)) in choice.
  3. Run the tool it picked: result = tools[choice["tool"]](**choice["args"])
  4. Print result. It should be 42.
💡 Hint

tools = {"calc": calc, "weather": weather}. Writing calc() with brackets would run it. You want the function itself, so leave the brackets off.

Ask the community

bot.py
Terminal
← BackNext →

Chapter 3: Tools

The full quest map

Talk it out

  1. Read the instruction with the ✗ out loud.
  2. Say what your code does, line by line, as if to a friend.
  3. Find the line where what it does and what you want split apart.
  4. Press Run and read the terminal. Is that what you expected?
  5. Still stuck? Press Ask Pip why, or peek at the solution.
  6. Or ask on the community help board: say which quest, and paste your code.
New badge!

Your bot just got a new part. See it on the quest map.

Name your bot

It levels up as you play, and gets a new part with every chapter.