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
- Make a dict
toolswith"calc"and"weather"pointing at your two functions (no brackets after their names). - Store
json.loads(pip.ask_with_tools("What is 6 times 7?", tools))inchoice. - Run the tool it picked:
result = tools[choice["tool"]](**choice["args"]) - Print
result. It should be42.
💡 Hint
tools = {"calc": calc, "weather": weather}. Writing calc() with brackets would run it. You want the function itself, so leave the brackets off.