Running the right tool
Turn a tool name and arguments into a real function call.
When the model asks for a tool, you get a name (a string) and inputs (a dict). A clean way to run it is a dict that maps names to functions, like {'get_weather': get_weather}. Look up the function by name and call it with the inputs using **, which unpacks a dict into keyword arguments. Handle unknown names and bad inputs politely, because the model can make mistakes.
def add(a, b):
return a + b
TOOLS = {"add": add}
name = "add"
args = {"a": 2, "b": 3}
result = TOOLS[name](**args) # same as add(a=2, b=3)
print(result) # 5TOOLS[name] gives the function itself; (**args) calls it with a=2, b=3.
Only functions in your TOOLS dict can ever be run. That's a safety feature: the model can't call anything you didn't choose to offer.
Return errors as text (like 'Error: unknown tool') rather than crashing. You can send that text back to the model, and it can often fix its request.