Spaces:
Starting
Starting
from langchain_core.tools import tool | |
import requests | |
import logging | |
import os | |
from dotenv import load_dotenv | |
logger = logging.getLogger(__name__) | |
load_dotenv() | |
async def weather_info_tool(location: str) -> str: | |
"""Fetch real weather information for a given location.""" | |
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" | |
url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}&units=metric" | |
response = requests.get(url).json() | |
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)}" |