person9601 commited on
Commit
562e659
·
verified ·
1 Parent(s): 102bbb4

1 add get weather tool

Browse files
Files changed (1) hide show
  1. app.py +43 -7
app.py CHANGED
@@ -17,16 +17,52 @@ import os
17
  load_dotenv()
18
 
19
  # Access environment variables
20
- api_key = os.getenv("WEATHER_APIKEY")
 
21
  @tool
22
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
23
- #Keep this format for the description / args / args description but feel free to modify the tool
24
- """A tool that does nothing yet
 
 
 
 
 
 
 
 
 
25
  Args:
26
- arg1: the first argument
27
- arg2: the second argument
28
  """
29
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  @tool
32
  def get_current_time_in_timezone(timezone: str) -> str:
 
17
  load_dotenv()
18
 
19
  # Access environment variables
20
+ weather_key = os.getenv("WEATHER_APIKEY")
21
+
22
  @tool
23
+ #Keep this format for the description / args / args description but feel free to modify
24
+ #it's import to specify the return type
25
+ #(arg1:str, arg2:int)-> str:
26
+ # the tool
27
+ # """A tool that does nothing yet
28
+ # Args:
29
+ # arg1: the first argument
30
+ # arg2: the second argument
31
+ # """
32
+ # return "What magic will you build ?"
33
+ def my_custom_get_weather_info(location: str, units: str = "metric") -> str:
34
+ """A tool that fetches current weather information for a specified location.
35
  Args:
36
+ location: City name or location (e.g., 'New York', 'London,UK')
37
+ units: Temperature unit system - 'metric' (Celsius) or 'imperial' (Fahrenheit)
38
  """
39
+ try:
40
+ # You'd need to sign up for a free API key at OpenWeatherMap or similar service
41
+ api_key = weather_key # Replace with your actual API key
42
+ base_url = "http://api.weatherapi.com/v1"
43
+
44
+ params = {
45
+ "q": location,
46
+ "appid": api_key,
47
+ "units": units
48
+ }
49
+
50
+ response = requests.get(base_url, params=params)
51
+ data = response.json()
52
+
53
+ if response.status_code == 200:
54
+ temp = data["main"]["temp"]
55
+ description = data["weather"][0]["description"]
56
+ humidity = data["main"]["humidity"]
57
+ wind_speed = data["wind"]["speed"]
58
+
59
+ unit_symbol = "°C" if units == "metric" else "°F"
60
+
61
+ return f"Weather in {location}: {description}, Temperature: {temp}{unit_symbol}, Humidity: {humidity}%, Wind speed: {wind_speed} {'m/s' if units == 'metric' else 'mph'}"
62
+ else:
63
+ return f"Error getting weather data: {data.get('message', 'Unknown error')}"
64
+ except Exception as e:
65
+ return f"Failed to retrieve weather information: {str(e)}"
66
 
67
  @tool
68
  def get_current_time_in_timezone(timezone: str) -> str: