Final_Assignment_Template / functionalities.py
alamanna's picture
Update functionalities.py
c792f80 verified
raw
history blame
2.93 kB
from smolagents import tool
import requests
@tool
def suggest_outfit(occasion: str) -> str:
"""
Suggests an outfit based on the occasion type.
Args:
occasion (str): One of "casual", "formal", "active", or "custom".
Returns:
str: A recommended outfit description suitable for the occasion.
"""
if occasion == "casual":
return "T‑shirt, Jeans and sneakers."
elif occasion == "formal":
return "White shirt, tie, blue suit and Oxford shoes."
elif occasion == "active":
return "Jogging trousers, T‑shirt and trainers."
else:
return "Custom outfit for the fashion advisor."
@tool
def define_coordinates(city: str) -> dict:
"""
Looks up geographic coordinates and timezone for a city.
Args:
city (str): Name of the city to geocode.
Returns:
dict: Contains "latitude", "longitude", and "timezone",
or {"error": "..."} if lookup fails.
"""
city = city.strip()
if not city:
return {"error": "City name cannot be empty."}
url = f"https://geocoding‑api.open‑meteo.com/v1/search?name={city}&count=1"
resp = requests.get(url); data = resp.json()
if not data.get("results"):
return {"error": f"No location found for city: {city}"}
r = data["results"][0]
return {"latitude": r["latitude"], "longitude": r["longitude"], "timezone": r["timezone"]}
@tool
def get_weather(city: str) -> dict:
"""
Retrieves current weather (temp, wind, code, description) for a city.
Args:
city (str): Name of the city to fetch weather for.
Returns:
dict: Keys include "temperature", "wind_speed", "weather_code",
"description", and "summary", or {"error": "..."} if failure.
"""
coords = define_coordinates(city)
if "error" in coords:
return {"error": coords["error"]}
url = (
f"https://api.open‑meteo.com/v1/forecast?"
f"latitude={coords['latitude']}&longitude={coords['longitude']}"
f"&current_weather=true&timezone={coords['timezone']}"
)
resp = requests.get(url); data = resp.json()
cw = data["current_weather"]
code = cw["weathercode"]
# classify code
(desc := (
"clear sky" if code==0 else
"mainly clear to partly cloudy" if 1<=code<=3 else
"fog" if code in (45,48) else
"drizzle" if 51<=code<=57 else
"rain" if 61<=code<=67 else
"snow" if 71<=code<=77 else
"rain showers" if 80<=code<=82 else
"snow showers" if 85<=code<=86 else
"thunderstorm" if code==95 else
"thunderstorm with hail" if 96<=code<=99 else
"unknown"
))
return {
"temperature": cw["temperature"],
"wind_speed": cw["windspeed"],
"weather_code": code,
"description": desc,
"summary": f"Temperature in {city}: {cw['temperature']}°C, wind {cw['windspeed']} km/h, {desc}."
}