hjxdl 0.2.45__py3-none-any.whl → 0.2.47__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hdl/_version.py +2 -2
- hdl/utils/desc/func_desc.py +44 -27
- hdl/utils/desc/template.py +7 -7
- hdl/utils/llm/chat.py +102 -45
- {hjxdl-0.2.45.dist-info → hjxdl-0.2.47.dist-info}/METADATA +1 -1
- {hjxdl-0.2.45.dist-info → hjxdl-0.2.47.dist-info}/RECORD +8 -8
- {hjxdl-0.2.45.dist-info → hjxdl-0.2.47.dist-info}/WHEEL +0 -0
- {hjxdl-0.2.45.dist-info → hjxdl-0.2.47.dist-info}/top_level.txt +0 -0
hdl/_version.py
CHANGED
hdl/utils/desc/func_desc.py
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
|
1
|
+
TOOL_DESC = {
|
2
2
|
|
3
|
-
"get_weather":
|
3
|
+
"get_weather":{
|
4
|
+
"desc": """
|
4
5
|
## 函数名:get_weather
|
5
|
-
|
6
|
-
触发条件:用户的问题中询问到了一个城市的天气
|
6
|
+
描述:用户询问一个城市的天气时,调用此工具获得此城市的天气信息
|
7
7
|
参数:
|
8
8
|
# city (str): 城市名
|
9
9
|
返回值 (str):天气信息
|
10
|
+
""",
|
11
|
+
"json": """
|
10
12
|
需要返回的json
|
11
13
|
{
|
12
14
|
"function_name": "get_weather",
|
@@ -16,16 +18,19 @@ FN_DESC = {
|
|
16
18
|
}
|
17
19
|
}
|
18
20
|
|
19
|
-
"""
|
21
|
+
"""
|
22
|
+
},
|
20
23
|
|
21
|
-
"get_datetime_by_cityname":
|
24
|
+
"get_datetime_by_cityname": {
|
25
|
+
"desc": """
|
22
26
|
## 函数名:get_datetime_by_cityname
|
23
|
-
|
24
|
-
触发条件:用户询问一个城市当前的日期或时间
|
27
|
+
描述:用户询问一个城市当前的日期或时间时,调用此工具可以获得此城市当前的日期和时间
|
25
28
|
参数:
|
26
29
|
# city (str): 城市名
|
27
30
|
返回值 (str):这个城市当前所在时区的日期和时间
|
28
|
-
|
31
|
+
""",
|
32
|
+
"json": """
|
33
|
+
需要给出的json
|
29
34
|
{
|
30
35
|
"function_name": "get_datetime_by_cityname",
|
31
36
|
"params":
|
@@ -33,16 +38,18 @@ FN_DESC = {
|
|
33
38
|
"city": <city_name>
|
34
39
|
}
|
35
40
|
}
|
41
|
+
"""
|
42
|
+
},
|
36
43
|
|
37
|
-
""
|
38
|
-
|
39
|
-
"execute_code": """
|
44
|
+
"execute_code": {
|
45
|
+
"desc": """
|
40
46
|
## 函数名:execute_code,
|
41
47
|
描述:当用户明确要求执行一段代码时,调用此工具,执行这段代码,返回执行结果。
|
42
|
-
触发条件:用户要求执行一段代码
|
43
48
|
参数:
|
44
49
|
# code (str): 用户要求执行的代码
|
45
50
|
返回值 (str): 执行结果
|
51
|
+
""",
|
52
|
+
"json": """
|
46
53
|
需要返回的json
|
47
54
|
{
|
48
55
|
"function_name": "execute_code",
|
@@ -51,16 +58,18 @@ FN_DESC = {
|
|
51
58
|
"code": <code>
|
52
59
|
}
|
53
60
|
}
|
61
|
+
"""
|
62
|
+
},
|
54
63
|
|
55
|
-
""
|
56
|
-
|
57
|
-
"calculate": """
|
64
|
+
"calculate": {
|
65
|
+
"desc": """
|
58
66
|
## 函数名:calculate,
|
59
|
-
|
60
|
-
触发条件:用户要求计算一个数学表达式
|
67
|
+
描述:当需要计算一个数学表达式时,调用此工具,计算这个表达式,返回计算结果。
|
61
68
|
参数:
|
62
69
|
# expression (str): 用户要求的数学表达式
|
63
70
|
返回值 (str): 计算结果
|
71
|
+
""",
|
72
|
+
"json": """
|
64
73
|
需要返回的json
|
65
74
|
{
|
66
75
|
"function_name": "calculate",
|
@@ -69,17 +78,19 @@ FN_DESC = {
|
|
69
78
|
"expression": <expression>
|
70
79
|
}
|
71
80
|
}
|
81
|
+
"""
|
82
|
+
},
|
72
83
|
|
73
|
-
""
|
74
|
-
|
75
|
-
"fetch_baidu_results": """
|
84
|
+
"fetch_baidu_results": {
|
85
|
+
"desc": """
|
76
86
|
## 函数名:fetch_baidu_results,
|
77
|
-
|
78
|
-
触发条件:用户明确要求查询网络上相关信息
|
87
|
+
描述:在用户提的问题需要联网搜索才能得到结果或者问题中有联网搜索的需求时,调用此工具,返回查询结果。
|
79
88
|
参数:
|
80
89
|
# query (str): 用户要求的查询内容
|
81
90
|
# max_n_links (int, optional): 搜索条目的最大数目,若用户指定了数目,则使用用户指定的数目,若用户提问中没有指定,你需要在下面的json中"max_n_links"这一项指定为数值3。
|
82
91
|
返回值 (str): 联网搜索到的信息
|
92
|
+
""",
|
93
|
+
"json": """
|
83
94
|
需要返回的json
|
84
95
|
{
|
85
96
|
"function_name": "fetch_baidu_results",
|
@@ -89,15 +100,18 @@ FN_DESC = {
|
|
89
100
|
"max_n_links": <num of max results, 如果用户没有要求,这一项则指定为3>
|
90
101
|
}
|
91
102
|
}
|
103
|
+
"""
|
104
|
+
},
|
92
105
|
|
93
|
-
""
|
94
|
-
|
106
|
+
"wolfram_alpha_calculate": {
|
107
|
+
"desc": """
|
95
108
|
## 函数名:wolfram_alpha_calculate
|
96
109
|
描述:当用户要求计算一个数学表达式时,调用此工具,计算这个表达式,返回计算结果。
|
97
|
-
触发条件:用户要求计算一个数学表达式
|
98
110
|
参数:
|
99
111
|
# query (str): 用户要求的数学表达式,需要转换为英文,比如"integrate sin(x)"
|
100
112
|
返回值 (str): 计算结果
|
113
|
+
""",
|
114
|
+
"json": """
|
101
115
|
需要返回的json
|
102
116
|
{
|
103
117
|
"function_name": "wolfram_alpha_calculate",
|
@@ -106,7 +120,10 @@ FN_DESC = {
|
|
106
120
|
"query": <expression>
|
107
121
|
}
|
108
122
|
}
|
109
|
-
"""
|
123
|
+
"""
|
124
|
+
},
|
125
|
+
|
110
126
|
|
111
127
|
"default": None
|
128
|
+
|
112
129
|
}
|
hdl/utils/desc/template.py
CHANGED
@@ -18,11 +18,11 @@ FN_TEMPLATE = """
|
|
18
18
|
"""
|
19
19
|
|
20
20
|
COT_TEMPLATE = """
|
21
|
-
你是一个专家级AI
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
21
|
+
你是一个专家级AI助手,有能针对一个问题和当前解决到的步骤给出下一步该进行操作的能力。
|
22
|
+
以JSON格式回复,该步骤对应的 json 包括'title'、'tool', 'content'和'stop_thinking',
|
23
|
+
若已有的信息可以回答用户问题,则'stop_thinking'键的值为 true 并把答案写在'conten'键的值中,若还需要下一步操作,'stop_thinking'键的则为 false 并在 'title' 键对应的值中给出该进行的操作;
|
24
|
+
若本步骤的操作不需要调用下文中提到的工具或信息足够回答用户问题,而不必包含'tool'键,若需要调用下文中的工具,则必须包含'tool'键,并且其值是工具函数名称。
|
25
|
+
你的回答中应只能是 json,且不包含其他多余文字不能有格式错误。
|
26
|
+
|
27
|
+
以下是可用的工具:
|
28
28
|
"""
|
hdl/utils/llm/chat.py
CHANGED
@@ -7,7 +7,7 @@ import subprocess
|
|
7
7
|
|
8
8
|
from openai import OpenAI
|
9
9
|
from ..desc.template import FN_TEMPLATE, COT_TEMPLATE
|
10
|
-
from ..desc.func_desc import
|
10
|
+
from ..desc.func_desc import TOOL_DESC
|
11
11
|
import json
|
12
12
|
# import traceback
|
13
13
|
|
@@ -114,6 +114,7 @@ class OpenAI_M():
|
|
114
114
|
groq_api_key: str = None,
|
115
115
|
tools: list = None,
|
116
116
|
tool_desc: dict = None,
|
117
|
+
cot_desc: str = None,
|
117
118
|
*args,
|
118
119
|
**kwargs
|
119
120
|
):
|
@@ -136,35 +137,93 @@ class OpenAI_M():
|
|
136
137
|
**kwargs
|
137
138
|
)
|
138
139
|
self.tools: list = tools
|
139
|
-
self.tool_desc: dict =
|
140
|
+
self.tool_desc: dict = TOOL_DESC
|
140
141
|
if tool_desc is not None:
|
141
142
|
self.tool_desc = self.tool_desc | tool_desc
|
142
143
|
|
143
|
-
self.
|
144
|
-
|
145
|
-
|
144
|
+
self.tool_descs = [
|
145
|
+
self.tool_desc[tool.__name__]['desc']
|
146
|
+
for tool in self.tools
|
147
|
+
]
|
148
|
+
self.tool_descs_verbose = [
|
149
|
+
self.tool_desc[tool.__name__]['desc']
|
150
|
+
+ self.tool_desc[tool.__name__]['json']
|
151
|
+
for tool in self.tools
|
152
|
+
]
|
146
153
|
|
147
|
-
|
154
|
+
self.tool_info = "\n".join(self.tool_descs)
|
155
|
+
self.tool_desc_str = "\n".join(self.tool_descs_verbose)
|
156
|
+
|
157
|
+
self.cot_desc = cot_desc
|
158
|
+
if not self.cot_desc:
|
159
|
+
self.cot_desc = COT_TEMPLATE
|
160
|
+
|
161
|
+
def cot(
|
148
162
|
self,
|
149
163
|
prompt,
|
164
|
+
max_step: int = 30,
|
150
165
|
**kwargs
|
151
166
|
):
|
167
|
+
"""_summary_
|
168
|
+
|
169
|
+
Args:
|
170
|
+
prompt (_type_): _description_
|
171
|
+
max_step (int, optional): _description_. Defaults to 30.
|
172
|
+
|
173
|
+
Returns:
|
174
|
+
_type_: _description_
|
175
|
+
"""
|
176
|
+
# 初始化当前信息为空字符串,用于累积后续的思考步骤和用户问题
|
177
|
+
current_info = ""
|
178
|
+
# 初始化步数为0,用于控制最大思考次数
|
179
|
+
n_steps = 0
|
180
|
+
|
181
|
+
# 初始化步骤列表,用于记录每一步的思考过程
|
152
182
|
steps = []
|
153
|
-
|
154
|
-
|
183
|
+
|
184
|
+
# 进入思考循环,直到找到答案或达到最大步数
|
155
185
|
while True:
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
186
|
+
# 增加步数计数
|
187
|
+
n_steps += 1
|
188
|
+
# 检查是否达到最大步数,如果是,则退出循环并返回默认答案
|
189
|
+
if n_steps >= max_step:
|
190
|
+
print("Max step reached!")
|
191
|
+
return "Sorry, I can't think of a better answer."
|
192
|
+
|
193
|
+
# 调用思考函数,传入当前信息和用户问题,获取下一步思考的结果
|
194
|
+
resp = self.invoke(
|
195
|
+
current_info + "用户问题:" + prompt,
|
196
|
+
sys_info=COT_TEMPLATE + self.tool_info,
|
197
|
+
assis_info = "好的,我将根据用户的问题和信息给出当前需要进行的操作或最终答案"
|
198
|
+
)
|
199
|
+
|
200
|
+
try:
|
201
|
+
# 将思考结果解析为JSON格式,以便后续处理
|
202
|
+
step_json = json.loads(resp)
|
203
|
+
# 将当前思考步骤添加到步骤列表中
|
204
|
+
steps.append(step_json)
|
205
|
+
# 如果思考步骤中标记为停止思考,则打印所有步骤并返回最终答案
|
206
|
+
if step_json["stop_thinking"]:
|
207
|
+
print(steps)
|
208
|
+
return step_json["content"]
|
209
|
+
else:
|
210
|
+
# 如果思考步骤中包含使用工具的指示,则构造工具提示并调用agent_response方法
|
211
|
+
if 'tool' in step_json:
|
212
|
+
tool_prompt = step_json["tool"] + step_json["title"] + step_json["content"]
|
213
|
+
tool_resp = self.agent_response(
|
214
|
+
tool_prompt,
|
215
|
+
stream=False,
|
216
|
+
**kwargs
|
217
|
+
)
|
218
|
+
# 将工具返回的信息累积到当前信息中
|
219
|
+
current_info += tool_resp
|
220
|
+
else:
|
221
|
+
# 如果不使用工具,将当前思考步骤的标题累积到当前信息中
|
222
|
+
current_info += step_json["title"]
|
223
|
+
except Exception as e:
|
224
|
+
# 捕获异常并打印,然后继续下一轮思考
|
225
|
+
print(e)
|
226
|
+
continue
|
168
227
|
|
169
228
|
def get_resp(
|
170
229
|
self,
|
@@ -178,30 +237,28 @@ class OpenAI_M():
|
|
178
237
|
stream: bool = True,
|
179
238
|
**kwargs: t.Any,
|
180
239
|
):
|
181
|
-
"""
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
返回:
|
196
|
-
- response: 模型的响应。
|
240
|
+
"""Prepare and send a request to the chat model, and return the model's response.
|
241
|
+
|
242
|
+
Args:
|
243
|
+
prompt (str): The user's input text.
|
244
|
+
sys_info (str, optional): System information, if any. Defaults to None.
|
245
|
+
assis_info (str, optional): Assistant information, if any. Defaults to None.
|
246
|
+
images (list, optional): List of images to send with the request, if any. Defaults to None.
|
247
|
+
image_keys (tuple, optional): Tuple containing the keys for image information. Defaults to ("image_url", "url").
|
248
|
+
stop (_type_, optional): List of stop sequences for the model. Defaults to ["USER:", "ASSISTANT:"].
|
249
|
+
model (str, optional): The name of the model to use. Defaults to "default_model".
|
250
|
+
stream (bool, optional): Whether to use streaming mode for the response. Defaults to True.
|
251
|
+
|
252
|
+
Returns:
|
253
|
+
_type_: The response object from the model.
|
197
254
|
"""
|
198
255
|
|
199
|
-
#
|
256
|
+
# Initialize the content list with at least the user's text input
|
200
257
|
content = [
|
201
258
|
{"type": "text", "text": prompt},
|
202
259
|
]
|
203
260
|
|
204
|
-
#
|
261
|
+
# Adjust the image_keys to be a tuple of length 3 based on its current length
|
205
262
|
if isinstance(image_keys, str):
|
206
263
|
image_keys = (image_keys,) * 3
|
207
264
|
elif len(image_keys) == 2:
|
@@ -209,7 +266,7 @@ class OpenAI_M():
|
|
209
266
|
elif len(image_keys) == 1:
|
210
267
|
image_keys = (image_keys[0],) * 3
|
211
268
|
|
212
|
-
#
|
269
|
+
# If images are provided, append them to the content list
|
213
270
|
if images:
|
214
271
|
if isinstance(images, str):
|
215
272
|
images = [images]
|
@@ -221,10 +278,10 @@ class OpenAI_M():
|
|
221
278
|
}
|
222
279
|
})
|
223
280
|
else:
|
224
|
-
#
|
281
|
+
# If no images are provided, content is simply the prompt text
|
225
282
|
content = prompt
|
226
283
|
|
227
|
-
#
|
284
|
+
# Initialize the messages list and add system information if provided
|
228
285
|
messages = []
|
229
286
|
if sys_info:
|
230
287
|
messages.append({
|
@@ -232,20 +289,20 @@ class OpenAI_M():
|
|
232
289
|
"content": sys_info
|
233
290
|
})
|
234
291
|
|
235
|
-
#
|
292
|
+
# Add the user's input as a message
|
236
293
|
messages.append({
|
237
294
|
"role": "user",
|
238
295
|
"content": content
|
239
296
|
})
|
240
297
|
|
241
|
-
#
|
298
|
+
# Add assistant information to the messages list if provided
|
242
299
|
if assis_info:
|
243
300
|
messages.append({
|
244
301
|
"role": "assistant",
|
245
302
|
"content": assis_info
|
246
303
|
})
|
247
304
|
|
248
|
-
#
|
305
|
+
# Call the model to generate a response
|
249
306
|
response = self.client.chat.completions.create(
|
250
307
|
messages=messages,
|
251
308
|
stream=stream,
|
@@ -253,7 +310,7 @@ class OpenAI_M():
|
|
253
310
|
**kwargs
|
254
311
|
)
|
255
312
|
|
256
|
-
#
|
313
|
+
# Return the model's response
|
257
314
|
return response
|
258
315
|
|
259
316
|
def invoke(
|
@@ -1,5 +1,5 @@
|
|
1
1
|
hdl/__init__.py,sha256=GffnD0jLJdhkd-vo989v40N90sQbofkayRBwxc6TVhQ,72
|
2
|
-
hdl/_version.py,sha256=
|
2
|
+
hdl/_version.py,sha256=tBkG62-I0GpJvDzYax3ziOOH-j0V7T_D21A6ka_5MNA,413
|
3
3
|
hdl/args/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
4
4
|
hdl/args/loss_args.py,sha256=s7YzSdd7IjD24rZvvOrxLLFqMZQb9YylxKeyelSdrTk,70
|
5
5
|
hdl/controllers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
@@ -122,13 +122,13 @@ hdl/utils/database_tools/connect.py,sha256=xCacGucKxlQUXs6AsNddpeECvdqT1180V1ZWq
|
|
122
122
|
hdl/utils/database_tools/datetime.py,sha256=xqE2xNiOpADzX-R8_bM0bioJRF3Ay9Jp1CAG6dy6uVI,1202
|
123
123
|
hdl/utils/database_tools/web.py,sha256=PJ7R1chUD-vz-Up0orfFqdrZq3CFOlwiO9gIjFSb-R8,5224
|
124
124
|
hdl/utils/desc/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
125
|
-
hdl/utils/desc/func_desc.py,sha256=
|
126
|
-
hdl/utils/desc/template.py,sha256=
|
125
|
+
hdl/utils/desc/func_desc.py,sha256=89GQEuJjkNaksBkgky2u6Yu2wDXAk1i7UpWozpIqwnA,3258
|
126
|
+
hdl/utils/desc/template.py,sha256=LHH6PxJvNBNr_2wHqz2jbxrH-OznbLegjWuj8RsRgdQ,2110
|
127
127
|
hdl/utils/general/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
128
128
|
hdl/utils/general/glob.py,sha256=8-RCnt6L297wMIfn34ZAMCsGCZUjHG3MGglGZI1cX0g,491
|
129
129
|
hdl/utils/general/runners.py,sha256=aWvEXYeuIF_F0AwucHIrrOOeeTM89goEFwW1rM35x1U,2231
|
130
130
|
hdl/utils/llm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
131
|
-
hdl/utils/llm/chat.py,sha256=
|
131
|
+
hdl/utils/llm/chat.py,sha256=jUFXMnYF2Q0CYWsMZDiCNx2pHRPASdL3nXuEUpDJGjw,19294
|
132
132
|
hdl/utils/llm/chatgr.py,sha256=A9SCWAvQulgO2x0ArIqcmgd_iVWVbSi1Oj15Diwb3Ok,3743
|
133
133
|
hdl/utils/llm/embs.py,sha256=Tf0FOYrOFZp7qQpEPiSCXzlgyHH0X9HVTUtsup74a9E,7174
|
134
134
|
hdl/utils/llm/extract.py,sha256=2sK_WJzmYIc8iuWaM9DA6Nw3_6q1O4lJ5pKpcZo-bBA,6512
|
@@ -139,7 +139,7 @@ hdl/utils/schedulers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hS
|
|
139
139
|
hdl/utils/schedulers/norm_lr.py,sha256=bDwCmdEK-WkgxQMFBiMuchv8Mm7C0-GZJ6usm-PQk14,4461
|
140
140
|
hdl/utils/weather/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
141
141
|
hdl/utils/weather/weather.py,sha256=k11o6wM15kF8b9NMlEfrg68ak-SfSYLN3nOOflFUv-I,4381
|
142
|
-
hjxdl-0.2.
|
143
|
-
hjxdl-0.2.
|
144
|
-
hjxdl-0.2.
|
145
|
-
hjxdl-0.2.
|
142
|
+
hjxdl-0.2.47.dist-info/METADATA,sha256=gEYH-JaU-n5NLVlI7EDkBejBIfnKnqc1sfCeLNfoHg4,836
|
143
|
+
hjxdl-0.2.47.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
|
144
|
+
hjxdl-0.2.47.dist-info/top_level.txt,sha256=-kxwTM5JPhylp06z3zAVO3w6_h7wtBfBo2zgM6YZoTk,4
|
145
|
+
hjxdl-0.2.47.dist-info/RECORD,,
|
File without changes
|
File without changes
|