ChaosBot/First_agent_template
0
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool2import datetime3import requests4import pytz5import yaml6import os7from tools.final_answer import FinalAnswerTool8from tools.web_search import DuckDuckGoSearchTool9from tools.visit_webpage import VisitWebpageTool10 11from Gradio_UI import GradioUI12 13web_search_tool = DuckDuckGoSearchTool(max_results=5)14visit_webpage_tool = VisitWebpageTool()15 16@tool17def get_weather(city: str) -> str:18 """19 A tool that fetches the current weather information for a specified city.20 21 Args:22 city: The name of the city to fetch the weather for (e.g., 'New York').23 24 Returns:25 str: A string containing the weather information or an error message.26 """27 api_key = os.getenv('WEATHER_API_KEY') 28 if not api_key:29 return "Error: API Key not found in environment variables."30 31 api_url = f"http://api.weatherapi.com/v1/current.json?key={api_key}&q={city}&aqi=no"32 response = requests.get(api_url)33 34 if response.status_code == 200:35 data = response.json()36 37 location = data.get("location", {})38 current_weather = data.get("current", {})39 40 city_name = location.get("name", "Unknown")41 country = location.get("country", "Unknown")42 temperature_c = current_weather.get("temp_c", "N/A")43 condition = current_weather.get("condition", {}).get("text", "N/A")44 wind_speed_kph = current_weather.get("wind_kph", "N/A")45 humidity = current_weather.get("humidity", "N/A")46 47 weather_info = (48 f"Weather in {city_name}, {country}:\n"49 f"Temperature: {temperature_c}°C\n"50 f"Condition: {condition}\n"51 f"Wind Speed: {wind_speed_kph} kph\n"52 f"Humidity: {humidity}%"53 )54 return weather_info55 else:56 return "Error: Unable to fetch weather data."57 58 59@tool60def get_current_time_in_timezone(timezone: str) -> str:61 """A tool that fetches the current local time in a specified timezone.62 Args:63 timezone: A string representing a valid timezone (e.g., 'America/New_York').64 """65 try:66 # Create timezone object67 tz = pytz.timezone(timezone)68 # Get current time in that timezone69 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")70 return f"The current local time in {timezone} is: {local_time}"71 except Exception as e:72 return f"Error fetching time for timezone '{timezone}': {str(e)}"73 74 75final_answer = FinalAnswerTool()76 77# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:78# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 79 80model = HfApiModel(81max_tokens=2096,82temperature=0.5,83model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded84custom_role_conversions=None,85)86 87 88# Import tool from Hub89image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)90 91with open("prompts.yaml", 'r') as stream:92 prompt_templates = yaml.safe_load(stream)93 94agent = CodeAgent(95 model=model,96 tools=[final_answer, image_generation_tool, get_current_time_in_timezone, get_weather, web_search_tool, visit_webpage_tool], ## add your tools here (don't remove final answer)97 max_steps=6,98 verbosity_level=1,99 grammar=None,100 planning_interval=None,101 name=None,102 description=None,103 prompt_templates=prompt_templates104)105 106 107GradioUI(agent).launch()