Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
2 |
import datetime
|
|
|
3 |
import requests
|
4 |
import pytz
|
5 |
import yaml
|
@@ -9,14 +10,46 @@ 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
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
"""
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
@tool
|
22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
@@ -51,7 +84,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
51 |
|
52 |
agent = CodeAgent(
|
53 |
model=model,
|
54 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
55 |
max_steps=6,
|
56 |
verbosity_level=1,
|
57 |
grammar=None,
|
|
|
1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
2 |
import datetime
|
3 |
+
import time
|
4 |
import requests
|
5 |
import pytz
|
6 |
import yaml
|
|
|
10 |
|
11 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
12 |
@tool
|
13 |
+
def get_node_cpu_idle_data(start: str, end: str, step: str = "15s") -> str:
|
14 |
+
"""
|
15 |
+
这个工具从 https://play.grafana.org/ 演示站点的 Prometheus 端点中,
|
16 |
+
获取指定时间范围内的 Prometheus 指标数据:
|
17 |
+
node_cpu_seconds_total{instance="pool-e1ro5g0nq-g0b64", mode="idle", cpu="0"}。
|
18 |
+
|
19 |
+
参数:
|
20 |
+
start (str): 起始时间,格式为 "YYYY-MM-DD HH:MM:SS"。
|
21 |
+
end (str): 截止时间,格式为 "YYYY-MM-DD HH:MM:SS"。
|
22 |
+
step (str): 查询步长(分辨率),默认为 "15s"。
|
23 |
+
|
24 |
+
返回:
|
25 |
+
str: 成功时返回查询结果数据(JSON 格式字符串),失败时返回错误提示。
|
26 |
"""
|
27 |
+
# 将时间字符串转换为 UNIX 时间戳
|
28 |
+
try:
|
29 |
+
start_ts = int(time.mktime(datetime.datetime.strptime(start, "%Y-%m-%d %H:%M:%S").timetuple()))
|
30 |
+
end_ts = int(time.mktime(datetime.datetime.strptime(end, "%Y-%m-%d %H:%M:%S").timetuple()))
|
31 |
+
except Exception as e:
|
32 |
+
return f"解析时间时出错: {str(e)}"
|
33 |
+
|
34 |
+
# Grafana Play 的 Prometheus 查询 API 端点 (通常 datasource id 为 1)
|
35 |
+
url = "https://play.grafana.org/api/datasources/proxy/1/api/v1/query_range"
|
36 |
+
|
37 |
+
# 查询参数
|
38 |
+
params = {
|
39 |
+
"query": 'node_cpu_seconds_total{instance="pool-e1ro5g0nq-g0b64", mode="idle", cpu="0"}',
|
40 |
+
"start": start_ts,
|
41 |
+
"end": end_ts,
|
42 |
+
"step": step
|
43 |
+
}
|
44 |
+
|
45 |
+
try:
|
46 |
+
response = requests.get(url, params=params)
|
47 |
+
response.raise_for_status()
|
48 |
+
# 返回 JSON 格式的查询结果数据
|
49 |
+
data = response.json()
|
50 |
+
return f"查询结果: {data}"
|
51 |
+
except Exception as e:
|
52 |
+
return f"获取数据时出错: {str(e)}"
|
53 |
|
54 |
@tool
|
55 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
84 |
|
85 |
agent = CodeAgent(
|
86 |
model=model,
|
87 |
+
tools=[final_answer,get_node_cpu_idle_data,get_current_time_in_timezone], ## add your tools here (don't remove final answer)
|
88 |
max_steps=6,
|
89 |
verbosity_level=1,
|
90 |
grammar=None,
|