from smolagents import CodeAgent, DuckDuckGoSearchTool, load_tool, tool import datetime import pytz import requests import yaml # Predefined answers PREDEFINED_RESPONSES = { "tokyo": "The best time to visit Tokyo is during spring (March to May) or autumn (September to November).", "eiffel tower": "The best time to visit the Eiffel Tower is early morning or late evening to avoid crowds.", "rome": "The best time to visit the Colosseum is in the early morning or late afternoon to skip long lines.", "machu picchu": "The dry season from May to October is the best time to visit Machu Picchu." } @tool def get_current_time_in_timezone(timezone: str) -> str: """Fetches the current local time in a specified timezone.""" 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: {str(e)}" @tool def get_best_time_to_visit(place: str) -> str: """Fetches the best time to visit a place based on predefined knowledge.""" place = place.lower() return PREDEFINED_RESPONSES.get(place, f"Sorry, I don't have predefined info for {place}. Try a web search.") web_search_tool = DuckDuckGoSearchTool() agent = CodeAgent( tools=[get_current_time_in_timezone, get_best_time_to_visit, web_search_tool], max_steps=6, verbosity_level=1, name="TravelInfoAgent", description="Provides travel and time zone information using predefined answers and web search." )