First_agent / app.py
cjb97's picture
reset
fe8bf03
raw
history blame
2.07 kB
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import datetime
import requests
import pytz
import yaml
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
# Weather tool to get current weather information
def get_weather(location: str) -> str:
"""A tool that fetches the current weather for a specified location.
Args:
location: A string representing a city or location (e.g., 'New York', 'Paris, France').
"""
try:
# Using OpenWeatherMap API with a free tier (limited requests)
api_key = "1ae035abc608d0e1095a5472dc989299" # OpenWeatherMap API key
url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}&units=metric"
response = requests.get(url)
data = response.json()
if response.status_code == 200:
temp = data["main"]["temp"]
weather = data["weather"][0]["description"]
humidity = data["main"]["humidity"]
wind_speed = data["wind"]["speed"]
return f"The current weather in {location} is {temp}°C with {weather}. Humidity: {humidity}%, Wind speed: {wind_speed} m/s."
else:
return f"Error fetching weather for {location}: {data.get('message', 'Unknown error')}"
except Exception as e:
return f"Error fetching weather for {location}: {str(e)}"
final_answer = FinalAnswerTool()
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
custom_role_conversions=None,
)
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
# We're creating our CodeAgent with multiple tools
agent = CodeAgent(
model=model,
tools=[
final_answer,
DuckDuckGoSearchTool(),
get_weather
],
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name=None,
description=None,
prompt_templates=prompt_templates
)
GradioUI(agent).launch()