Tool Calling Basics
AI agents extend their capabilities by calling external tools (APIs, functions, databases).
Task
Build a ToolRegistry that:
- Registers tools with a name, callable, and JSON schema.
- Invokes tools by name with keyword arguments.
- Lists available tools and their schemas.
- Raises
ToolNotFoundErrorfor unknown tools.
Constraints
- Tool names must be unique.
- Schema must be a dict with at least
'description'key. - Arguments must be validated against schema before calling.
Examples
Example 1:
Input:
reg = ToolRegistry()
reg.register('add', lambda a, b: a+b, {'description': 'Add two numbers'})
reg.call('add', a=2, b=3)Output:
5Explanation: Tool 'add' is found and executed with provided args.
Starter Code
from typing import Any, Dict, Callable
class ToolRegistry:
def __init__(self):
self.tools: Dict[str, Callable] = {}
self.schemas: Dict[str, Dict] = {}
def register(self, name: str, fn: Callable, schema: Dict) -> None:
# TODO: Register tool with name, function, and JSON schema
pass
def call(self, name: str, **kwargs) -> Any:
# TODO: Validate and call tool, raise ToolNotFoundError if missing
pass
def list_tools(self) -> Dict[str, Dict]:
# TODO: Return all registered tool schemas
pass
Python3
ReadyLines: 1Characters: 0
Ready