-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
148 lines (115 loc) · 3.46 KB
/
tools.py
File metadata and controls
148 lines (115 loc) · 3.46 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
144
145
146
147
148
import requests
import json
import os
tools = [
{
"type": "function",
"function": {
"name": "get_game_info",
"description": "获取游戏的类型、Steam定价、开发商、价格等信息",
"parameters": {
"type": "object",
"properties": {
"game_name": {
"type": "string",
"description": "游戏名称,例如 '黑神话:悟空'"
}
},
"required": ["game_name"]
}
}
},
{
"type": "function",
"function": {
"name": "calculator",
"description": "计算用户提供的数学表达式",
"parameters": {
"type": "object",
"properties": {
"expression": {
"type": "string",
"description": "数学表达式,如 '2+3*4' 或 '10/2'"
}
},
"required": ["expression"]
}
}
}
]
def get_game_info(game_name: str):
try:
print("开始查询 RAWG")
url = "https://api.rawg.io/api/games"
params = {
"key":os.getenv("rawg_key"),
"search": game_name,
"page_size": 1
}
resp = requests.get(
url,
params=params,
timeout=10
)
print("RAWG状态码:", resp.status_code)
print("RAWG返回:", resp.text)
resp.raise_for_status()
data = resp.json()
if not data.get("results"):
return "RAWG 没找到游戏"
game = data["results"][0]
name = game.get("name", "未知")
genres = [
g["name"]
for g in game.get("genres", [])
]
platforms = [
p["platform"]["name"]
for p in game.get("platforms", [])
]
# -------------------------
# CheapShark
# -------------------------
print("开始查询 CheapShark")
cheap_url = "https://www.cheapshark.com/api/1.0/games"
cheap_params = {
"title": name
}
cheap_resp = requests.get(
cheap_url,
params=cheap_params,
timeout=10
)
print("CheapShark状态码:", cheap_resp.status_code)
print("CheapShark返回:", cheap_resp.text)
price = "暂无价格"
if cheap_resp.status_code == 200:
cheap_data = cheap_resp.json()
if cheap_data:
price = cheap_data[0].get(
"cheapest",
"暂无价格"
)
return (
f"🎮 游戏:{name}\n"
f"📂 类型:{', '.join(genres)}\n"
f"💰 最低价格:{price}\n"
f"🖥️ 平台:{', '.join(platforms)}"
)
except Exception as e:
print("发生错误:", str(e))
return f"查询失败: {str(e)}"
def calculator(expression: str):
try:
allowed_chars = "0123456789+-*/(). "
for char in expression:
if char not in allowed_chars:
return "非法表达式"
result = eval(expression, {"__builtins__": None}, {})
return str(result)
except:
return "计算错误"
tool_map = {
"get_game_info": get_game_info,
"calculator": calculator
}