mirror of
https://github.com/open-webui/open-webui
synced 2024-11-17 22:12:51 +00:00
refac
This commit is contained in:
parent
6589464ddf
commit
c5683dd24c
@ -56,6 +56,44 @@ class Tools:
|
|||||||
print(e)
|
print(e)
|
||||||
return "Invalid equation"
|
return "Invalid equation"
|
||||||
|
|
||||||
|
def get_current_weather(self, city: str) -> str:
|
||||||
|
"""
|
||||||
|
Get the current weather for a given city.
|
||||||
|
:param city: The name of the city to get the weather for.
|
||||||
|
:return: The current weather information or an error message.
|
||||||
|
"""
|
||||||
|
api_key = os.getenv('OPENWEATHER_API_KEY')
|
||||||
|
if not api_key:
|
||||||
|
return "API key is not set in the environment variable 'OPENWEATHER_API_KEY'."
|
||||||
|
|
||||||
|
base_url = "http://api.openweathermap.org/data/2.5/weather"
|
||||||
|
params = {
|
||||||
|
'q': city,
|
||||||
|
'appid': api_key,
|
||||||
|
'units': 'metric' # Optional: Use 'imperial' for Fahrenheit
|
||||||
|
}
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = requests.get(base_url, params=params)
|
||||||
|
response.raise_for_status() # Raise HTTPError for bad responses (4xx and 5xx)
|
||||||
|
data = response.json()
|
||||||
|
|
||||||
|
if data.get('cod') != 200:
|
||||||
|
return f"Error fetching weather data: {data.get('message')}"
|
||||||
|
|
||||||
|
weather_description = data['weather'][0]['description']
|
||||||
|
temperature = data['main']['temp']
|
||||||
|
humidity = data['main']['humidity']
|
||||||
|
wind_speed = data['wind']['speed']
|
||||||
|
|
||||||
|
return (f"Weather in {city}:\n"
|
||||||
|
f"Description: {weather_description}\n"
|
||||||
|
f"Temperature: {temperature}°C\n"
|
||||||
|
f"Humidity: {humidity}%\n"
|
||||||
|
f"Wind Speed: {wind_speed} m/s")
|
||||||
|
except requests.RequestException as e:
|
||||||
|
return f"Error fetching weather data: {str(e)}"
|
||||||
|
|
||||||
`;
|
`;
|
||||||
|
|
||||||
export const formatHandler = async () => {
|
export const formatHandler = async () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user