yooke commited on
Commit
20a28ab
·
verified ·
1 Parent(s): ae7a494

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -1
app.py CHANGED
@@ -4,6 +4,9 @@ import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
 
 
 
7
 
8
  from Gradio_UI import GradioUI
9
 
@@ -18,6 +21,73 @@ 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 +125,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,
 
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
+ import requests
8
+ import json
9
+ from datetime import datetime
10
 
11
  from Gradio_UI import GradioUI
12
 
 
21
  """
22
  return "What magic will you build ?"
23
 
24
+
25
+
26
+
27
+ @tool
28
+ def query_weather(location: str, days: int = 1) -> str:
29
+ """查询指定地点的天气情况
30
+
31
+ 参数:
32
+ location:要查询天气的地点(城市名称)
33
+ days:要查询的天数(默认为1天,最大3天)
34
+ """
35
+ if days < 1 or days > 3:
36
+ return "查询天数必须在1-3之间"
37
+
38
+ # 使用wttr.in API,完全免费,无需API密钥
39
+ base_url = "https://wttr.in/"
40
+
41
+ # 构建API请求URL
42
+ params = {
43
+ "format": "j1", # 返回JSON格式
44
+ "lang": "zh-cn" # 中文结果
45
+ }
46
+
47
+ try:
48
+ # 发送请求
49
+ response = requests.get(f"{base_url}{location}", params=params)
50
+
51
+ # 检查响应状态
52
+ if response.status_code != 200:
53
+ return f"查询失败: HTTP状态码 {response.status_code}"
54
+
55
+ data = response.json()
56
+
57
+ # 处理天气数据
58
+ result = f"{location}天气预报:\n\n"
59
+
60
+ # 处理预报数据
61
+ for i, day in enumerate(data["weather"]):
62
+ if i >= days:
63
+ break
64
+
65
+ date = day["date"]
66
+
67
+ result += f"【{date}】\n"
68
+
69
+ # 添加当天的天气信息
70
+ for period in day["hourly"]:
71
+ # 每隔6小时显示一次预报
72
+ if int(period["time"]) % 600 == 0:
73
+ time_str = f"{int(period['time'])//100:02d}:00"
74
+ temp = period["tempC"]
75
+ weather_desc = period["lang_zh"][0]["value"]
76
+ humidity = period["humidity"]
77
+ wind_speed = period["windspeedKmph"]
78
+ wind_dir = period["winddir16Point"]
79
+
80
+ result += f"- {time_str}: {weather_desc}, 温度{temp}°C, 湿度{humidity}%, 风速{wind_speed}km/h, 风向{wind_dir}\n"
81
+
82
+ result += "\n"
83
+
84
+ return result
85
+
86
+ except Exception as e:
87
+ return f"查询天气时发生错误: {str(e)}"
88
+
89
+
90
+
91
  @tool
92
  def get_current_time_in_timezone(timezone: str) -> str:
93
  """A tool that fetches the current local time in a specified timezone.
 
125
 
126
  agent = CodeAgent(
127
  model=model,
128
+ tools=[final_answer,query_weather], ## add your tools here (don't remove final answer)
129
  max_steps=6,
130
  verbosity_level=1,
131
  grammar=None,