Spaces:
Sleeping
Sleeping
| from typing import Any, Optional | |
| from smolagents.tools import Tool | |
| class CityDatabaseTool(Tool): | |
| """Loads a static 'database' of city facts once, at construction time.""" | |
| name = "get_city_info" | |
| description = "Looks up the country and population for a given city." | |
| inputs = {'city': {'type': 'string', 'description': 'The name of the city to look up.'}} | |
| output_type = "string" | |
| def __init__(self, *args, **kwargs): | |
| super().__init__(*args, **kwargs) | |
| print("[CityDatabaseTool] loading city database once...") | |
| self._db = { | |
| "Cairo": {"country": "Egypt", "population_millions": 10.2}, | |
| "Tokyo": {"country": "Japan", "population_millions": 13.9}, | |
| "Reykjavik": {"country": "Iceland", "population_millions": 0.13}, | |
| } | |
| def forward(self, city: str) -> str: | |
| info = self._db.get(city) | |
| if not info: | |
| return f"No data for {city}." | |
| return f"{city} is in {info['country']}, population ~{info['population_millions']}M." | |