Monty Python.
import os
from langchain_openai import ChatOpenAI
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
import requests
# Set your API keys
os.environ["OPENAI_API_KEY"] = "sk-proj-51Hbb"
WEATHER_API_KEY = "70af9bd14888db"
# Define the Weather Tool function
def get_weather(location: str):
"""Fetch weather data for a city name or zipcode."""
# This URL handles both city names and zipcodes via OpenWeatherMap's search
url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={WEATHER_API_KEY}&units=metric"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
temp = data['main']['temp']
desc = data['weather'][0]['description']
return f"The current weather in {location} is {temp}°C with {desc}."
else:
return "Sorry, I couldn't find that location."