Spaces:
Sleeping
Sleeping
from typing import Any, Dict, Optional | |
from smolagents.tools import Tool | |
from tools.car_sharing_db import CarSharingDB | |
class GetMonthlyStatsTool(Tool): | |
name = "get_monthly_stats" | |
description = "Retrieves monthly car sharing statistics for a specific user." | |
inputs = { | |
'user_name': {'type': 'string', 'description': 'Name of the user to get statistics for'}, | |
'month': {'type': 'integer', 'description': 'Month number (1-12). If not provided, current month is used.', 'nullable': True}, | |
'year': {'type': 'integer', 'description': 'Year (e.g., 2023). If not provided, current year is used.', 'nullable': True} | |
} | |
output_type = "any" | |
def __init__(self, db_path="car_sharing.db"): | |
self.db = CarSharingDB(db_path) | |
self.is_initialized = True | |
def forward(self, user_name: str, month: Optional[int] = None, year: Optional[int] = None) -> Dict[str, Any]: | |
""" | |
Get monthly car sharing statistics for a user. | |
Args: | |
user_name: Name of the user to get statistics for | |
month: Month number (1-12). If not provided, current month is used. | |
year: Year (e.g., 2023). If not provided, current year is used. | |
Returns: | |
A dictionary with monthly statistics | |
""" | |
try: | |
# Convert month and year to integers if provided | |
if month is not None: | |
month = int(month) | |
if month < 1 or month > 12: | |
return { | |
"success": False, | |
"error": "Month must be between 1 and 12", | |
"total_km": 0 | |
} | |
if year is not None: | |
year = int(year) | |
# Get monthly statistics | |
stats = self.db.get_monthly_stats(user_name, month, year) | |
# Add success flag | |
stats["success"] = True | |
return stats | |
except ValueError: | |
return { | |
"success": False, | |
"error": "Month and year must be integers", | |
"total_km": 0 | |
} | |
except Exception as e: | |
return { | |
"success": False, | |
"error": str(e), | |
"total_km": 0 | |
} |