Mugiwarx commited on
Commit
d5e548c
·
verified ·
1 Parent(s): 9384487

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -23
app.py CHANGED
@@ -22,46 +22,45 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
22
  @tool
23
  def get_weather(city: str, days: Optional[int] = 1) -> str:
24
  """
25
- A tool that fetches weather information for a given city.
26
-
27
  Args:
28
- city (str): The city to get the weather for.
29
  days (Optional[int]): The number of days for the forecast (default is 1).
30
-
31
  Returns:
32
- str: A formatted weather report.
33
  """
34
  try:
35
- # 1️ Open-Meteo API URL (Basis-URL für Wetterdaten)
36
- api_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1&language=en&format=json"
37
-
38
- # 2️⃣ Stadt in Koordinaten umwandeln (da API GPS-Koordinaten benötigt)
39
- response = requests.get(api_url)
40
- response.raise_for_status()
41
- data = response.json()
42
-
43
- if "results" not in data or len(data["results"]) == 0:
44
- return f"❌ Keine Wetterdaten für {city} gefunden."
45
-
46
- lat = data["results"][0]["latitude"]
47
- lon = data["results"][0]["longitude"]
48
-
49
- # 3️⃣ Wetterdaten abrufen
50
  weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}&current_weather=true"
51
  weather_response = requests.get(weather_url)
52
  weather_response.raise_for_status()
53
  weather_data = weather_response.json()
54
 
55
- # 4 Wetterdaten formatieren
56
  weather = weather_data["current_weather"]
57
  temp = weather["temperature"]
58
  windspeed = weather["windspeed"]
59
  condition = weather["weathercode"]
60
 
61
- return f"🌍 Wetter in {city}: {temp}°C, Windgeschwindigkeit: {windspeed} km/h, Wettercode: {condition}"
62
 
63
  except requests.exceptions.RequestException as e:
64
- return f" Fehler beim Abrufen der Wetterdaten: {str(e)}"
 
65
 
66
  @tool
67
  def get_current_time_in_timezone(timezone: str) -> str:
 
22
  @tool
23
  def get_weather(city: str, days: Optional[int] = 1) -> str:
24
  """
25
+ Fetches weather information for a given city using Open-Meteo API.
26
+
27
  Args:
28
+ city (str): The name of the city for which to retrieve weather data.
29
  days (Optional[int]): The number of days for the forecast (default is 1).
30
+
31
  Returns:
32
+ str: A formatted weather report including temperature and wind speed.
33
  """
34
  try:
35
+ # 1️ Stadt in Koordinaten umwandeln
36
+ geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1&language=en&format=json"
37
+ geo_response = requests.get(geo_url)
38
+ geo_response.raise_for_status()
39
+ geo_data = geo_response.json()
40
+
41
+ if "results" not in geo_data or len(geo_data["results"]) == 0:
42
+ return f"Keine Wetterdaten für {city} gefunden."
43
+
44
+ lat = geo_data["results"][0]["latitude"]
45
+ lon = geo_data["results"][0]["longitude"]
46
+
47
+ # 2️ Wetterdaten abrufen
 
 
48
  weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}&current_weather=true"
49
  weather_response = requests.get(weather_url)
50
  weather_response.raise_for_status()
51
  weather_data = weather_response.json()
52
 
53
+ # 3️ Wetterdaten formatieren
54
  weather = weather_data["current_weather"]
55
  temp = weather["temperature"]
56
  windspeed = weather["windspeed"]
57
  condition = weather["weathercode"]
58
 
59
+ return f" Wetter in {city}: {temp}°C, Windgeschwindigkeit: {windspeed} km/h, Wettercode: {condition}"
60
 
61
  except requests.exceptions.RequestException as e:
62
+ return f" Fehler beim Abrufen der Wetterdaten: {str(e)}"
63
+
64
 
65
  @tool
66
  def get_current_time_in_timezone(timezone: str) -> str: