A model asks for a tool by name with arguments. Check the tool exists and the arguments are complete before running anything.
AI · intermediate · 7 min · 24 steps · free
def get_weather(city):
return {'city': city, 'temp_c': 31}
TOOLS = {'get_weather': {'run': get_weather, 'required': ['city']}}
def call_tool(request):
spec = TOOLS.get(request['tool'])
if spec is None:
return {'error': 'unknown tool ' + request['tool']}
missing = [p for p in spec['required'] if p not in request['args']]
if missing:
return {'error': 'missing ' + ', '.join(missing)}
return spec['run'](**request['args'])
print(call_tool({'tool': 'get_weather', 'args': {'city': 'Chennai'}}))
print(call_tool({'tool': 'get_weather', 'args': {}}))
print(call_tool({'tool': 'book_flight', 'args': {}}))get_weather(). Its body does not run yet: it runs each time the function is called.call_tool(). Its body does not run yet: it runs each time the function is called.call_tool(), with request = {tool: 'get_weather', args: a dict}. It gets its own names.spec is created and points to a new dict: {run: function get_weather(), required: a list}.spec is None is false, so the block under it is skipped.missing is false, so the block under it is skipped.get_weather(), with city = 'Chennai'. It gets its own names.{'city': city, 'temp_c': 31}.get_weather() has finished, so its names are gone.call_tool() has finished, so its names are gone. It prints {'city': 'Chennai', 'temp_c': 31}.call_tool(), with request = {tool: 'get_weather', args: a dict}. It gets its own names.spec is created and points to a new dict: {run: function get_weather(), required: a list}.spec is None is false, so the block under it is skipped.city.missing is true, so the block under it runs.{'error': 'missing ' + ', '.join(missing)}.call_tool() has finished, so its names are gone. It prints {'error': 'missing city'}.call_tool(), with request = {tool: 'book_flight', args: a dict}. It gets its own names.spec is created but does not point to any object yet (null).spec is None is true, so the block under it runs.call_tool() has finished, so its names are gone. It prints {'error': 'unknown tool book_flight'}.Answer: Your code. The model only asks.
Answer: A clear error
← Evaluating Retrieval · The Agent Loop: Think, Act, Observe →