-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_tools.py
More file actions
143 lines (121 loc) · 4.97 KB
/
example_tools.py
File metadata and controls
143 lines (121 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import asyncio
import inspect
from urllib.parse import quote
import httpx
from arclet.entari import Session, command, plugin
from arclet.letoderea import BLOCK
from arclet.letoderea.core import add_task
from entari_plugin_llm import LLMToolEvent
tools = plugin.dispatch(LLMToolEvent)
client = httpx.AsyncClient(timeout=30)
@plugin.collect_disposes
def dispose_client():
add_task(client.aclose())
@tools
async def ask_user_for_argument(session: Session, prompt: str, timeout: int = 120):
"""
向用户询问参数并等待
Args:
session (Session): 当前会话对象
prompt (str): 询问提示语
timeout (int): 超时时间,单位秒
Returns:
str: 用户输入的参数
null: 等待超时或用户未输入
"""
resp = await session.prompt(prompt, timeout=timeout)
if not resp:
return "未提供必要的信息,无法继续操作"
return resp.extract_plain_text()
@tools
async def split_answer_send(session: Session, answers: list[str], delays: list[float]):
"""
将拆分的多段回答发送给用户
注意:该工具应当在最后一步调用。调用完成后,LLM 不应再返回任何内容。
Args:
session (Session): 当前会话对象
answers (list[str | float]): 回答列表, 每段回答可以是字符串或数字
delays (float): 每段回答发送后的间隔时间,单位秒。尽量保证每段的发送间隔不相同,并且大于 1 秒
"""
for answer, delay in zip(answers, delays):
await session.send(answer)
await asyncio.sleep(delay)
return BLOCK.finish("所有回答已发送, 无需继续后续步骤")
API = "https://wttr.in/{city}?format=j1&lang=zh"
async def _get_weather(city: str, timeout: int = 30) -> dict:
"""
获取指定城市的天气信息
如果用户未给出指定城市名称,需要询问用户
Args:
city (str): 城市名称
timeout (int): 请求超时时间,单位秒
"""
url = API.format(city=quote(city))
# return {
# 'format': '* {type}: {content}',
# 'temperature': '-1°C',
# 'condition': '零星小雪',
# 'feels_like': '-2°C',
# 'humidity': '86%',
# 'wind_speed': '5 km/h',
# 'uv_index': '1',
# 'visibility': '4 km',
# 'precipitation': '0.0 mm',
# 'observation_time': '2026-03-05 10:06 AM'
# }
response = await client.get(url, timeout=timeout)
if response.status_code != 200:
return {"error": {"code": response.status_code, "message": response.text}}
data = response.json()
current_condition = data.get("current_condition", [])
if current_condition:
condition = current_condition[0]
temperature = condition["temp_C"] + "°C"
weather_desc = condition["lang_zh"][0]["value"]
feels_like = condition["FeelsLikeC"] + "°C"
humidity = condition["humidity"] + "%"
wind_speed = condition["windspeedKmph"] + " km/h"
uv_index = condition["uvIndex"]
visibility = condition["visibility"] + " km"
current_time = condition["localObsDateTime"]
precip = condition["precipMM"] + " mm"
return {
"format": "* {type}: {content}",
"temperature": temperature,
"condition": weather_desc,
"feels_like": feels_like,
"humidity": humidity,
"wind_speed": wind_speed,
"uv_index": uv_index,
"visibility": visibility,
"precipitation": precip,
"observation_time": current_time,
}
return {"error": {"code": 404, "message": "未找到该城市的天气信息"}}
tools.register(_get_weather)
@command.on("weather {city?}")
async def get_weather(session: Session, city: str = ""):
"""获取指定城市的天气信息"""
# await session.account.send_message(channel="private:3165388245", message="拦截")
await session.account.internal(
"send_private_msg", user_id=3165388245, message=[{"type": "text", "data": {"text": "AAA"}}]
)
if not city:
resp = await session.prompt("你想查询哪个城市的天气?")
if not resp:
return "未提供城市名称,无法查询天气"
city = resp.extract_plain_text()
weather_info = await _get_weather(city)
print(weather_info)
if "error" in weather_info:
return f"获取 {city} 天气信息失败,状态码:{weather_info['code']},错误信息:{weather_info['message']}。"
return inspect.cleandoc(f"""
{city} 当前天气信息({weather_info["observation_time"]}):
天气状况:{weather_info["condition"]}
温度:{weather_info["temperature"]},体感温度:{weather_info["feels_like"]}
湿度:{weather_info["humidity"]}
风速:{weather_info["wind_speed"]}
紫外线指数:{weather_info["uv_index"]}
能见度:{weather_info["visibility"]}
降雨量: {weather_info["precipitation"]}
""")