Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ from langchain.pydantic_v1 import BaseModel, Field
|
|
| 7 |
import requests
|
| 8 |
from datetime import datetime
|
| 9 |
from typing import List
|
|
|
|
| 10 |
from langchain.prompts import ChatPromptTemplate
|
| 11 |
from langchain.output_parsers import PydanticOutputParser
|
| 12 |
from langchain.memory import ConversationBufferMemory
|
|
@@ -29,10 +30,6 @@ llm = ChatGoogleGenerativeAI(
|
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
-
class WeatherInput(BaseModel):
|
| 33 |
-
city: str = Field(default=None, description="The city to get the weather for.")
|
| 34 |
-
|
| 35 |
-
|
| 36 |
def get_location_from_ip():
|
| 37 |
ip = requests.get('https://api.ipify.org').text
|
| 38 |
response = requests.get(f"https://ipapi.co/{ip}/json/").json()
|
|
@@ -43,11 +40,14 @@ def get_location_from_ip():
|
|
| 43 |
}
|
| 44 |
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
@tool("get_weather_by_location", args_schema=WeatherInput, return_direct=True)
|
| 47 |
def get_weather_by_location(city: str = None):
|
| 48 |
-
|
| 49 |
"""Get the weather based on the user's location if no city is specified."""
|
| 50 |
-
|
| 51 |
if not city:
|
| 52 |
location = get_location_from_ip()
|
| 53 |
city = location['city']
|
|
@@ -73,6 +73,7 @@ def get_weather_by_location(city: str = None):
|
|
| 73 |
|
| 74 |
|
| 75 |
def format_weather_response(weather_data, city):
|
|
|
|
| 76 |
intervals = weather_data['data']['timelines'][0]['intervals']
|
| 77 |
response = f"Weather forecast for {city}:\n\n"
|
| 78 |
|
|
@@ -89,6 +90,14 @@ def format_weather_response(weather_data, city):
|
|
| 89 |
|
| 90 |
return response
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
class DailyWeather(BaseModel):
|
| 93 |
date: str
|
| 94 |
temperature: float
|
|
|
|
| 7 |
import requests
|
| 8 |
from datetime import datetime
|
| 9 |
from typing import List
|
| 10 |
+
from langchain.tools import Tool
|
| 11 |
from langchain.prompts import ChatPromptTemplate
|
| 12 |
from langchain.output_parsers import PydanticOutputParser
|
| 13 |
from langchain.memory import ConversationBufferMemory
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
def get_location_from_ip():
|
| 34 |
ip = requests.get('https://api.ipify.org').text
|
| 35 |
response = requests.get(f"https://ipapi.co/{ip}/json/").json()
|
|
|
|
| 40 |
}
|
| 41 |
|
| 42 |
|
| 43 |
+
class WeatherInput(BaseModel):
|
| 44 |
+
city: str = Field(default=None, description="The city to get the weather for.")
|
| 45 |
+
|
| 46 |
+
|
| 47 |
@tool("get_weather_by_location", args_schema=WeatherInput, return_direct=True)
|
| 48 |
def get_weather_by_location(city: str = None):
|
|
|
|
| 49 |
"""Get the weather based on the user's location if no city is specified."""
|
| 50 |
+
|
| 51 |
if not city:
|
| 52 |
location = get_location_from_ip()
|
| 53 |
city = location['city']
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
def format_weather_response(weather_data, city):
|
| 76 |
+
"""Format the weather data into a readable string."""
|
| 77 |
intervals = weather_data['data']['timelines'][0]['intervals']
|
| 78 |
response = f"Weather forecast for {city}:\n\n"
|
| 79 |
|
|
|
|
| 90 |
|
| 91 |
return response
|
| 92 |
|
| 93 |
+
|
| 94 |
+
get_weather_tool = Tool(
|
| 95 |
+
name="get_weather_by_location",
|
| 96 |
+
func=get_weather_by_location,
|
| 97 |
+
description="Get the current weather for a specific location. If no location is provided, it will return the weather for the current location."
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
class DailyWeather(BaseModel):
|
| 102 |
date: str
|
| 103 |
temperature: float
|