jarvis_gaia_agent / tools /weather_info.py
onisj's picture
feat(tools): add more tool to extend the functionaily of jarvis
751d628
import aiohttp
import ssl
import logging
import os
from langchain_core.tools import tool
from tenacity import retry, stop_after_attempt, wait_exponential
from dotenv import load_dotenv
logger = logging.getLogger(__name__)
load_dotenv()
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=1, max=10))
async def fetch_weather(location: str, api_key: str) -> dict:
url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}&units=metric"
ssl_context = ssl.create_default_context()
try:
async with aiohttp.ClientSession() as session:
async with session.get(url, ssl=ssl_context) as response:
response.raise_for_status()
return await response.json()
except aiohttp.ClientError as e:
logger.error(f"Failed to fetch weather for {location}: {e}")
raise
@tool
async def weather_info_tool(location: str, query_type: str = "current") -> str:
"""
Fetch weather information for a given location.
Args:
location (str): City or location name.
query_type (str): Type of weather query ('current', 'forecast'; default: 'current').
Returns:
str: Weather information or error message.
"""
try:
api_key = os.getenv("OPENWEATHERMAP_API_KEY")
if not api_key:
logger.error("OPENWEATHERMAP_API_KEY not set")
return "Weather unavailable: API key missing"
if query_type != "current":
logger.warning(f"Query type '{query_type}' not supported; using current weather")
query_type = "current"
response = await fetch_weather(location, api_key)
if response.get("cod") == 200:
condition = response["weather"][0]["description"]
temp = response["main"]["temp"]
return f"Weather in {location}: {condition}, {temp}°C"
return f"Unable to fetch weather for {location}."
except Exception as e:
logger.error(f"Error fetching weather for {location}: {e}")
return f"Error: {str(e)}"