la-min commited on
Commit
82d41d8
·
verified ·
1 Parent(s): 96e9892

add new tool, weather

Browse files
Files changed (1) hide show
  1. app.py +25 -1
app.py CHANGED
@@ -10,9 +10,33 @@ from huggingface_hub import login
10
  import os
11
 
12
  hf_api_key = os.getenv("HUGGINGFACEHUB_API_TOKEN")
 
13
 
14
  login(hf_api_key)
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
17
  @tool
18
  def get_exchange_rate(base_currency: str, target_currency: str) -> str:
@@ -73,7 +97,7 @@ with open("prompts.yaml", 'r') as stream:
73
 
74
  agent = CodeAgent(
75
  model=model,
76
- tools=[get_current_time_in_timezone, get_exchange_rate, DuckDuckGoSearchTool(), final_answer], ## add your tools here (don't remove final answer)
77
  max_steps=6,
78
  verbosity_level=1,
79
  grammar=None,
 
10
  import os
11
 
12
  hf_api_key = os.getenv("HUGGINGFACEHUB_API_TOKEN")
13
+ weather_api_key = os.getenv("WEATHER_API_KEY")
14
 
15
  login(hf_api_key)
16
 
17
+ @tool
18
+ def get_weather(city: str) -> str:
19
+ """Fetches current weather information for a given city.
20
+ Args:
21
+ city: Name of the city (e.g., 'Bangkok').
22
+ """
23
+ try:
24
+ API_KEY = "YOUR_OPENWEATHERMAP_API_KEY" # Replace with your API key
25
+ url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={weather_api_key}&units=metric"
26
+ response = requests.get(url)
27
+ data = response.json()
28
+
29
+ if data["cod"] != 200:
30
+ return f"Error fetching weather: {data['message']}"
31
+
32
+ weather_desc = data["weather"][0]["description"]
33
+ temp = data["main"]["temp"]
34
+ return f"The current weather in {city} is {weather_desc} with a temperature of {temp}°C."
35
+
36
+ except Exception as e:
37
+ return f"Error fetching weather: {str(e)}"
38
+
39
+
40
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
41
  @tool
42
  def get_exchange_rate(base_currency: str, target_currency: str) -> str:
 
97
 
98
  agent = CodeAgent(
99
  model=model,
100
+ tools=[get_weather, get_current_time_in_timezone, get_exchange_rate, DuckDuckGoSearchTool(), final_answer], ## add your tools here (don't remove final answer)
101
  max_steps=6,
102
  verbosity_level=1,
103
  grammar=None,