Files changed (1) hide show
  1. app.py +36 -0
app.py CHANGED
@@ -18,6 +18,42 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
18
  """
19
  return "What magic will you build ?"
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
23
  """A tool that fetches the current local time in a specified timezone.
 
18
  """
19
  return "What magic will you build ?"
20
 
21
+ @tool
22
+ def get_temperature(location: str, celsius: bool = True) -> str:
23
+ """A tool that fetches the current temperature for a given location.
24
+ Args:
25
+ location: The name of the city/location
26
+ celsius: If True, returns temperature in Celsius, otherwise in Fahrenheit
27
+ """
28
+ try:
29
+ API_KEY = os.environ.get('WEATHER_API_KEY')
30
+ if not API_KEY:
31
+ return "Error: Weather API key not configured"
32
+
33
+ # Add units parameter to API call
34
+ units = 'metric' if celsius else 'imperial'
35
+ url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={API_KEY}&units={units}"
36
+
37
+ response = requests.get(url, timeout=10)
38
+ data = response.json()
39
+
40
+ if response.status_code == 200:
41
+ temperature = data["main"]["temp"]
42
+ unit = '°C' if celsius else '°F'
43
+ return f"The current temperature in {location} is {temperature:.1f}{unit}"
44
+ else:
45
+ error_msg = data.get('message', 'Unknown error')
46
+ return f"Error: {error_msg} (Status code: {response.status_code})"
47
+
48
+ except requests.Timeout:
49
+ return f"Error: Request timed out while fetching temperature for {location}"
50
+ except requests.RequestException as e:
51
+ return f"Network error while fetching temperature: {str(e)}"
52
+ except KeyError as e:
53
+ return f"Error parsing weather data: Missing {str(e)} in API response"
54
+ except Exception as e:
55
+ return f"Unexpected error while fetching temperature: {str(e)}"
56
+
57
  @tool
58
  def get_current_time_in_timezone(timezone: str) -> str:
59
  """A tool that fetches the current local time in a specified timezone.