hjxdl 0.1.38__py3-none-any.whl → 0.1.40__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/llm/chat.py +61 -171
- hdl/utils/llm/llama_chat.py +122 -0
- {hjxdl-0.1.38.dist-info → hjxdl-0.1.40.dist-info}/METADATA +1 -1
- {hjxdl-0.1.38.dist-info → hjxdl-0.1.40.dist-info}/RECORD +7 -6
- {hjxdl-0.1.38.dist-info → hjxdl-0.1.40.dist-info}/WHEEL +0 -0
- {hjxdl-0.1.38.dist-info → hjxdl-0.1.40.dist-info}/top_level.txt +0 -0
hdl/_version.py
CHANGED
hdl/utils/llm/chat.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
import typing as t
|
2
|
+
import asyncio
|
3
|
+
from concurrent.futures import ProcessPoolExecutor
|
4
|
+
|
2
5
|
|
3
|
-
from llama_cpp import Llama
|
4
6
|
from openai import OpenAI
|
5
7
|
from ..desc.template import FN_TEMPLATE
|
6
8
|
from ..desc.func_desc import FN_DESC
|
@@ -85,126 +87,6 @@ def chat_oai_invoke(
|
|
85
87
|
return response.choices[0].message.content
|
86
88
|
|
87
89
|
|
88
|
-
class GGUF_M(Llama):
|
89
|
-
def __init__(
|
90
|
-
self,
|
91
|
-
model_path :str,
|
92
|
-
device: str='gpu',
|
93
|
-
generation_kwargs: dict = {},
|
94
|
-
server_ip: str = "127.0.0.1",
|
95
|
-
server_port: int = 8000,
|
96
|
-
*args,
|
97
|
-
**kwargs
|
98
|
-
):
|
99
|
-
"""Initialize the model with the specified parameters.
|
100
|
-
|
101
|
-
Args:
|
102
|
-
model_path (str): The path to the model.
|
103
|
-
device (str, optional): The device to use, either 'gpu' or 'cpu'. Defaults to 'gpu'.
|
104
|
-
generation_kwargs (dict, optional): Additional generation keyword arguments. Defaults to {}.
|
105
|
-
server_ip (str, optional): The IP address of the server. Defaults to "127.0.0.1".
|
106
|
-
server_port (int, optional): The port of the server. Defaults to 8000.
|
107
|
-
*args: Variable length argument list.
|
108
|
-
**kwargs: Arbitrary keyword arguments.
|
109
|
-
|
110
|
-
Raises:
|
111
|
-
KeyError: If 'num_threads' or 'max_context_length' is missing in generation_kwargs.
|
112
|
-
"""
|
113
|
-
print("正在从本地加载模型...")
|
114
|
-
if device.lower() == 'cpu':
|
115
|
-
super().__init__(
|
116
|
-
model_path=model_path,
|
117
|
-
n_threads=generation_kwargs['num_threads'],
|
118
|
-
n_ctx=generation_kwargs['max_context_length'],
|
119
|
-
*args,
|
120
|
-
**kwargs
|
121
|
-
)
|
122
|
-
else:
|
123
|
-
super().__init__(
|
124
|
-
model_path=model_path,
|
125
|
-
n_threads=generation_kwargs['num_threads'],
|
126
|
-
n_ctx=generation_kwargs['max_context_length'],
|
127
|
-
n_gpu_layers=-1,
|
128
|
-
flash_attn=True,
|
129
|
-
*args,
|
130
|
-
**kwargs
|
131
|
-
)
|
132
|
-
self.generation_kwargs = generation_kwargs
|
133
|
-
|
134
|
-
def invoke(
|
135
|
-
self,
|
136
|
-
prompt : str,
|
137
|
-
stop: list[str] | None = ["USER:", "ASSISTANT:"],
|
138
|
-
# history: list = [],
|
139
|
-
**kwargs: t.Any,
|
140
|
-
) -> str:
|
141
|
-
"""Invoke the model to generate a response based on the given prompt.
|
142
|
-
|
143
|
-
Args:
|
144
|
-
prompt (str): The prompt to be used for generating the response.
|
145
|
-
stop (list[str], optional): List of strings that indicate when the model should stop generating the response. Defaults to ["USER:", "ASSISTANT:"].
|
146
|
-
**kwargs: Additional keyword arguments to be passed to the model.
|
147
|
-
|
148
|
-
Returns:
|
149
|
-
str: The generated response based on the prompt.
|
150
|
-
"""
|
151
|
-
prompt_final = f"USER:\n{prompt}\nASSISTANT:\n"
|
152
|
-
|
153
|
-
result = self.create_completion(
|
154
|
-
prompt_final,
|
155
|
-
repeat_penalty=self.generation_kwargs["repetition_penalty"],
|
156
|
-
max_tokens=self.generation_kwargs["max_new_tokens"],
|
157
|
-
stop=stop,
|
158
|
-
echo=False,
|
159
|
-
temperature=self.generation_kwargs["temperature"],
|
160
|
-
mirostat_mode = 2,
|
161
|
-
mirostat_tau=4.0,
|
162
|
-
mirostat_eta=1.1
|
163
|
-
)
|
164
|
-
resp = result['choices'][0]['text']
|
165
|
-
# history.append(
|
166
|
-
# [prompt, resp]
|
167
|
-
# )
|
168
|
-
return resp
|
169
|
-
|
170
|
-
def stream(
|
171
|
-
self,
|
172
|
-
prompt: str,
|
173
|
-
stop: list[str] | None = ["USER:", "ASSISTANT:"],
|
174
|
-
# history: list = [],
|
175
|
-
**kwargs: t.Any,
|
176
|
-
):
|
177
|
-
"""Generate text responses based on the given prompt using the model.
|
178
|
-
|
179
|
-
Args:
|
180
|
-
prompt (str): The prompt to generate text responses.
|
181
|
-
stop (list[str], optional): List of strings to stop the generation. Defaults to ["USER:", "ASSISTANT:"].
|
182
|
-
**kwargs: Additional keyword arguments for the model.
|
183
|
-
|
184
|
-
Yields:
|
185
|
-
str: Text responses generated by the model based on the prompt.
|
186
|
-
"""
|
187
|
-
prompt = f"USER:\n{prompt}\nASSISTANT:\n"
|
188
|
-
output = self.create_completion(
|
189
|
-
prompt,
|
190
|
-
stream=True,
|
191
|
-
repeat_penalty=self.generation_kwargs["repetition_penalty"],
|
192
|
-
max_tokens=self.generation_kwargs["max_new_tokens"],
|
193
|
-
stop=stop,
|
194
|
-
echo=False,
|
195
|
-
temperature=self.generation_kwargs["temperature"],
|
196
|
-
mirostat_mode = 2,
|
197
|
-
mirostat_tau=4.0,
|
198
|
-
mirostat_eta=1.1
|
199
|
-
)
|
200
|
-
# history.append([])
|
201
|
-
for chunk in output:
|
202
|
-
item = chunk['choices'][0]['text']
|
203
|
-
# self.resps[-1].append(item)
|
204
|
-
yield chunk['choices'][0]['text']
|
205
|
-
# self.resps[-1] = "".join(self.resps[-1])
|
206
|
-
|
207
|
-
|
208
90
|
class OpenAI_M():
|
209
91
|
def __init__(
|
210
92
|
self,
|
@@ -316,47 +198,47 @@ class OpenAI_M():
|
|
316
198
|
if content:
|
317
199
|
yield content
|
318
200
|
|
319
|
-
def
|
201
|
+
def agent_response(
|
320
202
|
self,
|
321
|
-
prompt: str,
|
203
|
+
prompt : str,
|
204
|
+
stream = True,
|
322
205
|
**kwargs: t.Any
|
323
206
|
):
|
324
|
-
"""
|
207
|
+
"""'''Generate agent response based on the given prompt.
|
325
208
|
|
326
209
|
Args:
|
327
|
-
prompt (str): The prompt for
|
210
|
+
prompt (str): The prompt for which agent response is generated.
|
211
|
+
stream (bool, optional): Flag to determine if the response should be streamed. Defaults to True.
|
328
212
|
**kwargs: Additional keyword arguments.
|
329
213
|
|
330
214
|
Returns:
|
331
|
-
|
332
|
-
|
333
|
-
Example:
|
334
|
-
decision = get_decision("Should I buy this product?", option1="yes", option2="no")
|
215
|
+
str: The agent response based on the prompt.
|
216
|
+
'''
|
335
217
|
"""
|
218
|
+
decision_dict = self.get_decision(prompt, **kwargs)
|
219
|
+
if decision_dict.get("function_name", None) is None:
|
220
|
+
return self.stream(prompt, **kwargs)
|
221
|
+
else:
|
222
|
+
tool_result = str(self.get_tool_result(prompt, **kwargs))
|
223
|
+
prompt_final = "根据上下文回答最后的用户问题:\n上下文信息:\n"
|
224
|
+
prompt_final += tool_result
|
225
|
+
prompt_final += f"\n用户的问题:\n{prompt}"
|
226
|
+
if stream:
|
227
|
+
return self.stream(prompt_final, **kwargs)
|
228
|
+
else:
|
229
|
+
return self.invoke(prompt_final, **kwargs)
|
230
|
+
|
231
|
+
def get_decision(self, prompt: str, **kwargs: t.Any):
|
232
|
+
# 该方法与之前一致...
|
336
233
|
prompt_final = FN_TEMPLATE
|
337
234
|
for tool in self.tools:
|
338
235
|
prompt_final += self.tool_desc.get(tool.__name__, "")
|
339
236
|
prompt_final += f"\n用户的问题:\n{prompt}"
|
340
|
-
|
341
|
-
decision_dict_str = self.invoke(prompt_final ,**kwargs)
|
342
|
-
print(decision_dict_str)
|
237
|
+
decision_dict_str = self.invoke(prompt_final, **kwargs)
|
343
238
|
return decision_dict_str
|
344
|
-
# return json.loads(decision_dict)
|
345
239
|
|
346
|
-
def get_tool_result(
|
347
|
-
|
348
|
-
prompt: str,
|
349
|
-
**kwargs: t.Any
|
350
|
-
):
|
351
|
-
"""Get the result from a tool based on the provided prompt and keyword arguments.
|
352
|
-
|
353
|
-
Args:
|
354
|
-
prompt (str): The prompt to get the decision for.
|
355
|
-
**kwargs: Additional keyword arguments to pass to the decision function.
|
356
|
-
|
357
|
-
Returns:
|
358
|
-
str: The result from the selected tool based on the decision made.
|
359
|
-
"""
|
240
|
+
def get_tool_result(self, prompt: str, **kwargs: t.Any):
|
241
|
+
# 同步方法与之前一致...
|
360
242
|
decision_dict_str = self.get_decision(prompt, **kwargs)
|
361
243
|
try:
|
362
244
|
decision_dict = json.loads(decision_dict_str)
|
@@ -377,33 +259,41 @@ class OpenAI_M():
|
|
377
259
|
print(e)
|
378
260
|
return ""
|
379
261
|
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
prompt : str,
|
384
|
-
stream = True,
|
385
|
-
**kwargs: t.Any
|
386
|
-
):
|
387
|
-
"""'''Generate agent response based on the given prompt.
|
262
|
+
async def get_tool_result_async(self, prompt: str, **kwargs: t.Any):
|
263
|
+
"""
|
264
|
+
Asynchronous version of the get_tool_result function that can run in parallel using multiprocessing.
|
388
265
|
|
389
266
|
Args:
|
390
|
-
prompt (str): The prompt
|
391
|
-
|
392
|
-
**kwargs: Additional keyword arguments.
|
267
|
+
prompt (str): The prompt to get the decision for.
|
268
|
+
**kwargs: Additional keyword arguments to pass to the decision function.
|
393
269
|
|
394
270
|
Returns:
|
395
|
-
str: The
|
396
|
-
'''
|
271
|
+
str: The result from the selected tool based on the decision made.
|
397
272
|
"""
|
398
|
-
|
399
|
-
|
400
|
-
return
|
273
|
+
|
274
|
+
def run_tool_with_kwargs(tool, **kwargs):
|
275
|
+
return tool(**kwargs)
|
276
|
+
|
277
|
+
decision_dict_str = await asyncio.to_thread(self.get_decision, prompt, **kwargs)
|
278
|
+
try:
|
279
|
+
decision_dict = json.loads(decision_dict_str)
|
280
|
+
except Exception as e:
|
281
|
+
print(e)
|
282
|
+
return ""
|
283
|
+
func_name = decision_dict.get("function_name", None)
|
284
|
+
if func_name is None:
|
285
|
+
return ""
|
401
286
|
else:
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
287
|
+
try:
|
288
|
+
for tool in self.tools:
|
289
|
+
if tool.__name__ == func_name:
|
290
|
+
tool_final = tool
|
291
|
+
func_kwargs = decision_dict.get("params")
|
292
|
+
|
293
|
+
loop = asyncio.get_running_loop()
|
294
|
+
with ProcessPoolExecutor() as pool:
|
295
|
+
result = await loop.run_in_executor(pool, run_tool_with_kwargs, tool_final, **func_kwargs)
|
296
|
+
return result
|
297
|
+
except Exception as e:
|
298
|
+
print(e)
|
299
|
+
return ""
|
@@ -0,0 +1,122 @@
|
|
1
|
+
import typing as t
|
2
|
+
|
3
|
+
from llama_cpp import Llama
|
4
|
+
|
5
|
+
class GGUF_M(Llama):
|
6
|
+
def __init__(
|
7
|
+
self,
|
8
|
+
model_path :str,
|
9
|
+
device: str='gpu',
|
10
|
+
generation_kwargs: dict = {},
|
11
|
+
server_ip: str = "127.0.0.1",
|
12
|
+
server_port: int = 8000,
|
13
|
+
*args,
|
14
|
+
**kwargs
|
15
|
+
):
|
16
|
+
"""Initialize the model with the specified parameters.
|
17
|
+
|
18
|
+
Args:
|
19
|
+
model_path (str): The path to the model.
|
20
|
+
device (str, optional): The device to use, either 'gpu' or 'cpu'. Defaults to 'gpu'.
|
21
|
+
generation_kwargs (dict, optional): Additional generation keyword arguments. Defaults to {}.
|
22
|
+
server_ip (str, optional): The IP address of the server. Defaults to "127.0.0.1".
|
23
|
+
server_port (int, optional): The port of the server. Defaults to 8000.
|
24
|
+
*args: Variable length argument list.
|
25
|
+
**kwargs: Arbitrary keyword arguments.
|
26
|
+
|
27
|
+
Raises:
|
28
|
+
KeyError: If 'num_threads' or 'max_context_length' is missing in generation_kwargs.
|
29
|
+
"""
|
30
|
+
print("正在从本地加载模型...")
|
31
|
+
if device.lower() == 'cpu':
|
32
|
+
super().__init__(
|
33
|
+
model_path=model_path,
|
34
|
+
n_threads=generation_kwargs['num_threads'],
|
35
|
+
n_ctx=generation_kwargs['max_context_length'],
|
36
|
+
*args,
|
37
|
+
**kwargs
|
38
|
+
)
|
39
|
+
else:
|
40
|
+
super().__init__(
|
41
|
+
model_path=model_path,
|
42
|
+
n_threads=generation_kwargs['num_threads'],
|
43
|
+
n_ctx=generation_kwargs['max_context_length'],
|
44
|
+
n_gpu_layers=-1,
|
45
|
+
flash_attn=True,
|
46
|
+
*args,
|
47
|
+
**kwargs
|
48
|
+
)
|
49
|
+
self.generation_kwargs = generation_kwargs
|
50
|
+
|
51
|
+
def invoke(
|
52
|
+
self,
|
53
|
+
prompt : str,
|
54
|
+
stop: list[str] | None = ["USER:", "ASSISTANT:"],
|
55
|
+
# history: list = [],
|
56
|
+
**kwargs: t.Any,
|
57
|
+
) -> str:
|
58
|
+
"""Invoke the model to generate a response based on the given prompt.
|
59
|
+
|
60
|
+
Args:
|
61
|
+
prompt (str): The prompt to be used for generating the response.
|
62
|
+
stop (list[str], optional): List of strings that indicate when the model should stop generating the response. Defaults to ["USER:", "ASSISTANT:"].
|
63
|
+
**kwargs: Additional keyword arguments to be passed to the model.
|
64
|
+
|
65
|
+
Returns:
|
66
|
+
str: The generated response based on the prompt.
|
67
|
+
"""
|
68
|
+
prompt_final = f"USER:\n{prompt}\nASSISTANT:\n"
|
69
|
+
|
70
|
+
result = self.create_completion(
|
71
|
+
prompt_final,
|
72
|
+
repeat_penalty=self.generation_kwargs["repetition_penalty"],
|
73
|
+
max_tokens=self.generation_kwargs["max_new_tokens"],
|
74
|
+
stop=stop,
|
75
|
+
echo=False,
|
76
|
+
temperature=self.generation_kwargs["temperature"],
|
77
|
+
mirostat_mode = 2,
|
78
|
+
mirostat_tau=4.0,
|
79
|
+
mirostat_eta=1.1
|
80
|
+
)
|
81
|
+
resp = result['choices'][0]['text']
|
82
|
+
# history.append(
|
83
|
+
# [prompt, resp]
|
84
|
+
# )
|
85
|
+
return resp
|
86
|
+
|
87
|
+
def stream(
|
88
|
+
self,
|
89
|
+
prompt: str,
|
90
|
+
stop: list[str] | None = ["USER:", "ASSISTANT:"],
|
91
|
+
# history: list = [],
|
92
|
+
**kwargs: t.Any,
|
93
|
+
):
|
94
|
+
"""Generate text responses based on the given prompt using the model.
|
95
|
+
|
96
|
+
Args:
|
97
|
+
prompt (str): The prompt to generate text responses.
|
98
|
+
stop (list[str], optional): List of strings to stop the generation. Defaults to ["USER:", "ASSISTANT:"].
|
99
|
+
**kwargs: Additional keyword arguments for the model.
|
100
|
+
|
101
|
+
Yields:
|
102
|
+
str: Text responses generated by the model based on the prompt.
|
103
|
+
"""
|
104
|
+
prompt = f"USER:\n{prompt}\nASSISTANT:\n"
|
105
|
+
output = self.create_completion(
|
106
|
+
prompt,
|
107
|
+
stream=True,
|
108
|
+
repeat_penalty=self.generation_kwargs["repetition_penalty"],
|
109
|
+
max_tokens=self.generation_kwargs["max_new_tokens"],
|
110
|
+
stop=stop,
|
111
|
+
echo=False,
|
112
|
+
temperature=self.generation_kwargs["temperature"],
|
113
|
+
mirostat_mode = 2,
|
114
|
+
mirostat_tau=4.0,
|
115
|
+
mirostat_eta=1.1
|
116
|
+
)
|
117
|
+
# history.append([])
|
118
|
+
for chunk in output:
|
119
|
+
item = chunk['choices'][0]['text']
|
120
|
+
# self.resps[-1].append(item)
|
121
|
+
yield chunk['choices'][0]['text']
|
122
|
+
# self.resps[-1] = "".join(self.resps[-1])
|
@@ -1,5 +1,5 @@
|
|
1
1
|
hdl/__init__.py,sha256=GffnD0jLJdhkd-vo989v40N90sQbofkayRBwxc6TVhQ,72
|
2
|
-
hdl/_version.py,sha256=
|
2
|
+
hdl/_version.py,sha256=3IhTfHIu7NeAPXyE10KzRtPGq9MVXGPwZ6ggQQd3ZPs,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
|
@@ -127,14 +127,15 @@ hdl/utils/desc/template.py,sha256=a3NcSihzZMm9Bk76iDVe54_xBDceGmLebS0XMONE3nk,11
|
|
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/llm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
130
|
-
hdl/utils/llm/chat.py,sha256=
|
130
|
+
hdl/utils/llm/chat.py,sha256=esvDM5CPOMVexmQAcjgRf3G_2Dn0fHVfb5eCzktewno,9969
|
131
131
|
hdl/utils/llm/embs.py,sha256=Tf0FOYrOFZp7qQpEPiSCXzlgyHH0X9HVTUtsup74a9E,7174
|
132
132
|
hdl/utils/llm/extract.py,sha256=2sK_WJzmYIc8iuWaM9DA6Nw3_6q1O4lJ5pKpcZo-bBA,6512
|
133
|
+
hdl/utils/llm/llama_chat.py,sha256=watcHGOaz-bv3x-yDucYlGk5f8FiqfFhwWogrl334fk,4387
|
133
134
|
hdl/utils/schedulers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
134
135
|
hdl/utils/schedulers/norm_lr.py,sha256=bDwCmdEK-WkgxQMFBiMuchv8Mm7C0-GZJ6usm-PQk14,4461
|
135
136
|
hdl/utils/weather/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
136
137
|
hdl/utils/weather/weather.py,sha256=k11o6wM15kF8b9NMlEfrg68ak-SfSYLN3nOOflFUv-I,4381
|
137
|
-
hjxdl-0.1.
|
138
|
-
hjxdl-0.1.
|
139
|
-
hjxdl-0.1.
|
140
|
-
hjxdl-0.1.
|
138
|
+
hjxdl-0.1.40.dist-info/METADATA,sha256=mgTKZ6PKpPjlDHrrFVdwbLnaxc-LRoNhwJykiT3aQpg,737
|
139
|
+
hjxdl-0.1.40.dist-info/WHEEL,sha256=UvcQYKBHoFqaQd6LKyqHw9fxEolWLQnlzP0h_LgJAfI,91
|
140
|
+
hjxdl-0.1.40.dist-info/top_level.txt,sha256=-kxwTM5JPhylp06z3zAVO3w6_h7wtBfBo2zgM6YZoTk,4
|
141
|
+
hjxdl-0.1.40.dist-info/RECORD,,
|
File without changes
|
File without changes
|