nizar125 commited on
Commit
773857a
·
verified ·
1 Parent(s): 8c5c24b

adding new tool

Browse files
Files changed (1) hide show
  1. app.py +37 -7
app.py CHANGED
@@ -9,14 +9,44 @@ from Gradio_UI import GradioUI
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
 
 
15
  Args:
16
- arg1: the first argument
17
- arg2: the second argument
 
 
18
  """
19
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -55,7 +85,7 @@ 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,
 
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
+ @tool
13
+ def get_weather(city: str) -> str:
14
+ """
15
+ Gets current weather for a given city using OpenWeatherMap API.
16
+
17
  Args:
18
+ city: City name (e.g., "Tunis", "London")
19
+
20
+ Returns:
21
+ Weather description and temperature.
22
  """
23
+ try:
24
+ import requests
25
+
26
+ api_key = "5499415a1e8327b9200f4df7eb114c11"
27
+
28
+ # Step 1: Get coordinates from city name
29
+ geo_url = f"http://api.openweathermap.org/geo/1.0/direct?q={city}&limit=1&appid={api_key}"
30
+ geo_response = requests.get(geo_url).json()
31
+
32
+ if not geo_response:
33
+ return f"City '{city}' not found."
34
+
35
+ lat = geo_response[0]["lat"]
36
+ lon = geo_response[0]["lon"]
37
+
38
+ # Step 2: Get current weather using coordinates
39
+ weather_url = f"https://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&exclude=minutely,hourly,daily,alerts&appid={api_key}&units=metric"
40
+ weather_data = requests.get(weather_url).json()
41
+
42
+ description = weather_data["current"]["weather"][0]["description"]
43
+ temp = weather_data["current"]["temp"]
44
+
45
+ return f"The current weather in {city} is '{description}' with a temperature of {temp}°C."
46
+
47
+ except Exception as e:
48
+ return f"Error fetching weather data: {str(e)}"
49
+
50
 
51
  @tool
52
  def get_current_time_in_timezone(timezone: str) -> str:
 
85
 
86
  agent = CodeAgent(
87
  model=model,
88
+ tools=[final_answer, get_weather, get_current_time_in_timezone], ## add your tools here (don't remove final answer)
89
  max_steps=6,
90
  verbosity_level=1,
91
  grammar=None,