DangMinh21 commited on
Commit
fa0d6cd
·
verified ·
1 Parent(s): 8c5c24b

Add get_weather tool

Browse files
Files changed (1) hide show
  1. app.py +43 -1
app.py CHANGED
@@ -18,6 +18,46 @@ 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.
@@ -55,7 +95,9 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
 
 
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
18
  """
19
  return "What magic will you build ?"
20
 
21
+ @tool
22
+ def get_weather(city: str) -> str:
23
+ """
24
+ Fetches the current weather for a specified city using OpenWeatherMap API.
25
+ Args:
26
+ city: The name of the city (e.g., 'Ho Chi Minh', 'Paris', 'London').
27
+ """
28
+ API_KEY = "eeac62ed496032584f632ff3777fc69c"
29
+ BASE_URL = "http://api.openweathermap.org/data/2.5/weather"
30
+ params = = {
31
+ "q": city,
32
+ "appid": API_KEY,
33
+ "units": "metric"
34
+ }
35
+ try:
36
+ respone = requests.get((BASE_URL, params=params))
37
+ respone.raise_for_status()
38
+ data = respone.json()
39
+
40
+ weather_description = data['weather'][0]['description']
41
+ temp = data['main']['temp']
42
+ feels_like = data['main']['feels_like']
43
+ humidity = data['main']['humidity']
44
+ wind_spead = data['wind']['speed']
45
+
46
+ return (f"The weather in {city} is currently {weather_description}. "
47
+ f"Tenperature: {temp} C, feels like {feels_like} C. "
48
+ f"Humidity is at {humidity}% and wind speed is {wind_spead} m/s.")
49
+
50
+ except requests.exceptions.HTTPError as http_err:
51
+ if response.status_code == 401:
52
+ return "Error: Invalid API key. Please check your OpenWeatherMap API key."
53
+ elif response.status_code == 404:
54
+ return f"Error: Could not find weather data for the city '{city}'. Please check the spelling."
55
+ else:
56
+ return f"An HTTP error occurred: {http_err}"
57
+ except Exception as e:
58
+ return f"An error occurred: {e}"
59
+
60
+
61
  @tool
62
  def get_current_time_in_timezone(timezone: str) -> str:
63
  """A tool that fetches the current local time in a specified timezone.
 
95
 
96
  agent = CodeAgent(
97
  model=model,
98
+ tools=[final_answer,
99
+ get_current_time_in_timezone,
100
+ get_weather], ## add your tools here (don't remove final answer)
101
  max_steps=6,
102
  verbosity_level=1,
103
  grammar=None,