|
|
|
|
|
|
|
|
|
|
|
|
|
from langchain.tools import Tool |
|
import random |
|
import requests |
|
import os |
|
from dotenv import load_dotenv |
|
load_dotenv() |
|
weather_API_key = os.getenv("OPENWEATHER_KEY") |
|
|
|
def get_weather_info(location: str) -> str: |
|
"""Fetches weather information for a given location.""" |
|
try: |
|
base_url = "https://api.openweathermap.org/data/2.5/weather" |
|
params = { |
|
"q": location, |
|
"appid": weather_API_key, |
|
"units": "metric" |
|
} |
|
response = requests.get(base_url, params=params) |
|
if response.status_code == 200: |
|
data = response.json() |
|
weather = data["weather"][0]["description"] |
|
temp = data["main"]["temp"] |
|
feels_like = data["main"]["feels_like"] |
|
humidity = data["main"]["humidity"] |
|
print(f"\nWeather in {location.title()}:") |
|
print(f"- Condition: {weather}") |
|
print(f"- Temperature: {temp}°C (feels like {feels_like}°C)") |
|
print(f"- Humidity: {humidity}%") |
|
data= {"condition": weather, "temp_c": temp,"humidity": humidity} |
|
except requests.exceptions.RequestException as e: |
|
print(f"Error: {e}") |
|
|
|
|
|
weather_conditions = [ |
|
{"condition": "Rainy", "temp_c": 15,"humidity": 30}, |
|
{"condition": "Clear", "temp_c": 25,"humidity": 50}, |
|
{"condition": "Windy", "temp_c": 20,"humidity": 80} |
|
] |
|
|
|
data = random.choice(weather_conditions) |
|
return f"Weather in {location}: {data['condition']}, {data['temp_c']}°C" |
|
|
|
|
|
weather_info_tool = Tool( |
|
name="get_weather_info", |
|
func=get_weather_info, |
|
description="Fetches dummy weather information for a given location." |
|
) |