File size: 1,033 Bytes
2571628
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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."