Spaces:
Build error
Build error
T-K-O-H commited on
Commit ·
8d7f3c1
1
Parent(s): 8130aa2
Update weather_tool to use OpenWeatherMap API
Browse files- app.py +36 -15
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -19,6 +19,7 @@ import logging
|
|
| 19 |
import sys
|
| 20 |
from langchain_community.utilities import DuckDuckGoSearchAPIWrapper
|
| 21 |
from openai import OpenAI
|
|
|
|
| 22 |
|
| 23 |
# LangChain
|
| 24 |
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage, ToolMessage
|
|
@@ -48,24 +49,44 @@ app = FastAPI()
|
|
| 48 |
|
| 49 |
# Define tools
|
| 50 |
@tool
|
| 51 |
-
def weather_tool(
|
| 52 |
-
"""Get weather information for a specific location
|
| 53 |
-
logger.info(f"Getting weather information for: {
|
| 54 |
try:
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
if not results:
|
| 61 |
-
# If no specific weather results, try a more general weather search
|
| 62 |
-
results = search.run(f"weather {query}")
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
except Exception as e:
|
| 70 |
logger.error(f"Error getting weather information: {str(e)}")
|
| 71 |
return f"Error getting weather information: {str(e)}"
|
|
|
|
| 19 |
import sys
|
| 20 |
from langchain_community.utilities import DuckDuckGoSearchAPIWrapper
|
| 21 |
from openai import OpenAI
|
| 22 |
+
import requests
|
| 23 |
|
| 24 |
# LangChain
|
| 25 |
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage, ToolMessage
|
|
|
|
| 49 |
|
| 50 |
# Define tools
|
| 51 |
@tool
|
| 52 |
+
def weather_tool(location: str) -> str:
|
| 53 |
+
"""Get current weather information for a specific location using OpenWeatherMap API."""
|
| 54 |
+
logger.info(f"Getting weather information for: {location}")
|
| 55 |
try:
|
| 56 |
+
# Get API key from environment
|
| 57 |
+
api_key = os.getenv("OPENWEATHER_API_KEY")
|
| 58 |
+
if not api_key:
|
| 59 |
+
return "Error: OPENWEATHER_API_KEY not found in environment variables"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
# Make API request
|
| 62 |
+
base_url = "http://api.openweathermap.org/data/2.5/weather"
|
| 63 |
+
params = {
|
| 64 |
+
"q": location,
|
| 65 |
+
"appid": api_key,
|
| 66 |
+
"units": "metric" # Use metric units
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
response = requests.get(base_url, params=params)
|
| 70 |
+
data = response.json()
|
| 71 |
+
|
| 72 |
+
if response.status_code != 200:
|
| 73 |
+
return f"Error getting weather: {data.get('message', 'Unknown error')}"
|
| 74 |
|
| 75 |
+
# Extract relevant weather information
|
| 76 |
+
weather = data["weather"][0]["description"]
|
| 77 |
+
temp = data["main"]["temp"]
|
| 78 |
+
humidity = data["main"]["humidity"]
|
| 79 |
+
wind_speed = data["wind"]["speed"]
|
| 80 |
+
|
| 81 |
+
result = f"""Current weather in {location}:
|
| 82 |
+
- Temperature: {temp}°C
|
| 83 |
+
- Conditions: {weather}
|
| 84 |
+
- Humidity: {humidity}%
|
| 85 |
+
- Wind Speed: {wind_speed} m/s"""
|
| 86 |
+
|
| 87 |
+
logger.info(f"Weather results: {result}")
|
| 88 |
+
return result
|
| 89 |
+
|
| 90 |
except Exception as e:
|
| 91 |
logger.error(f"Error getting weather information: {str(e)}")
|
| 92 |
return f"Error getting weather information: {str(e)}"
|
requirements.txt
CHANGED
|
@@ -7,3 +7,4 @@ langchain-core>=0.1.0
|
|
| 7 |
langchain-community>=0.0.10
|
| 8 |
langchain-openai>=0.0.5
|
| 9 |
websockets>=12.0
|
|
|
|
|
|
| 7 |
langchain-community>=0.0.10
|
| 8 |
langchain-openai>=0.0.5
|
| 9 |
websockets>=12.0
|
| 10 |
+
requests>=2.31.0
|