File size: 886 Bytes
c79ef2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
26
27
28
from typing import Any
from smolagents.tools import Tool
import datetime
import pytz

class GetCurrentTimeTool(Tool):
    name = "get_current_time_in_timezone"
    description = "Fetches the current local time in a specified timezone."
    
    inputs = {
        "timezone": {
            "type": "string",
            "description": "A valid timezone (e.g., 'Africa/Kigali', 'Europe/London')"
        }
    }
    
    output_type = "string"

    def forward(self, timezone: str) -> str:
        try:
            tz = pytz.timezone(timezone)
            local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
            return f"The current local time in {timezone} is: {local_time}"
        except Exception as e:
            return f"Error fetching time for timezone '{timezone}': {str(e)}"

    def __init__(self, *args, **kwargs):
        self.is_initialized = False