hjxdl 0.3.20__py3-none-any.whl → 0.3.22__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/llm_wrapper.py +139 -5
- {hjxdl-0.3.20.dist-info → hjxdl-0.3.22.dist-info}/METADATA +1 -1
- {hjxdl-0.3.20.dist-info → hjxdl-0.3.22.dist-info}/RECORD +7 -7
- {hjxdl-0.3.20.dist-info → hjxdl-0.3.22.dist-info}/LICENSE +0 -0
- {hjxdl-0.3.20.dist-info → hjxdl-0.3.22.dist-info}/WHEEL +0 -0
- {hjxdl-0.3.20.dist-info → hjxdl-0.3.22.dist-info}/top_level.txt +0 -0
hdl/_version.py
CHANGED
hdl/utils/llm/llm_wrapper.py
CHANGED
@@ -2,7 +2,6 @@ import yaml
|
|
2
2
|
import typing as t
|
3
3
|
|
4
4
|
from openai import OpenAI
|
5
|
-
import instructor
|
6
5
|
|
7
6
|
|
8
7
|
class OpenAIWrapper(object):
|
@@ -14,6 +13,32 @@ class OpenAIWrapper(object):
|
|
14
13
|
*args,
|
15
14
|
**kwargs
|
16
15
|
):
|
16
|
+
"""
|
17
|
+
Initializes the client configuration for the class.
|
18
|
+
|
19
|
+
Args:
|
20
|
+
client_conf (dict, optional): A dictionary containing client configuration. If None,
|
21
|
+
client configuration will be loaded from the specified directory.
|
22
|
+
client_conf_dir (str, optional): The directory from which to load client configuration
|
23
|
+
if `client_conf` is None. Must be provided in that case.
|
24
|
+
load_conf (bool, optional): A flag indicating whether to load the client
|
25
|
+
configuration from the directory. Defaults to True.
|
26
|
+
*args: Variable length argument list for client initialization.
|
27
|
+
**kwargs: Arbitrary keyword arguments for client initialization.
|
28
|
+
|
29
|
+
Raises:
|
30
|
+
AssertionError: If `client_conf` is None and `client_conf_dir` is also None.
|
31
|
+
|
32
|
+
Note:
|
33
|
+
The method will create a client for each configuration found in `client_conf`,
|
34
|
+
initializing the client with the specified `base_url` and `api_key`.
|
35
|
+
Examples:
|
36
|
+
>>> llm = OpenAIWrapper(
|
37
|
+
>>> client_conf_dir="/some/path/model_conf.yaml",
|
38
|
+
>>> # load_conf=False
|
39
|
+
>>> )
|
40
|
+
)
|
41
|
+
"""
|
17
42
|
self.client_conf = {}
|
18
43
|
if client_conf is None:
|
19
44
|
assert client_conf_dir is not None
|
@@ -41,6 +66,31 @@ class OpenAIWrapper(object):
|
|
41
66
|
api_key: str = "dummy_key",
|
42
67
|
**kwargs
|
43
68
|
):
|
69
|
+
"""
|
70
|
+
Add a new client configuration to the client manager.
|
71
|
+
|
72
|
+
This method stores the configuration details for a new client identified by the
|
73
|
+
provided client ID. It constructs the host URL based on the input parameters
|
74
|
+
and initializes an OpenAI client instance.
|
75
|
+
|
76
|
+
Args:
|
77
|
+
client_id (str): Unique identifier for the client.
|
78
|
+
host (str): Hostname or IP address of the client.
|
79
|
+
port (int, optional): Port number for the client connection. Defaults to None.
|
80
|
+
model (str, optional): Model to use for the client. Defaults to "default_model".
|
81
|
+
api_key (str, optional): API key for authentication. Defaults to "dummy_key".
|
82
|
+
**kwargs: Additional keyword arguments passed to the OpenAI client.
|
83
|
+
|
84
|
+
Raises:
|
85
|
+
ValueError: If both host and port are not valid for constructing a URL.
|
86
|
+
Examples:
|
87
|
+
>>> llm.add_client(
|
88
|
+
>>> client_id="rone",
|
89
|
+
>>> host="127.0.0.1",
|
90
|
+
>>> port=22299,
|
91
|
+
>>> model="ictrek/rone:1.5b32k",
|
92
|
+
>>> )
|
93
|
+
"""
|
44
94
|
self.client_conf[client_id] = {}
|
45
95
|
if not host.startswith('http') and port:
|
46
96
|
host = f"http://{host}:{port}/v1"
|
@@ -53,6 +103,22 @@ class OpenAIWrapper(object):
|
|
53
103
|
)
|
54
104
|
|
55
105
|
def load_clients(self):
|
106
|
+
"""
|
107
|
+
Loads client configuration from a YAML file and updates the 'host' field
|
108
|
+
for each client entry, ensuring the correct URL format.
|
109
|
+
|
110
|
+
This method reads the client configuration from the specified path,
|
111
|
+
updates the 'host' field to include the appropriate port and the
|
112
|
+
'http' protocol if not already specified, and stores the updated
|
113
|
+
configuration in the `client_conf` attribute.
|
114
|
+
|
115
|
+
Attributes:
|
116
|
+
client_conf_path (str): The file path to the client configuration YAML file.
|
117
|
+
client_conf (dict): The updated client configuration after processing.
|
118
|
+
|
119
|
+
Returns:
|
120
|
+
None
|
121
|
+
"""
|
56
122
|
with open(self.client_conf_path, 'r') as file:
|
57
123
|
data = yaml.safe_load(file)
|
58
124
|
|
@@ -79,11 +145,32 @@ class OpenAIWrapper(object):
|
|
79
145
|
response_model = None,
|
80
146
|
**kwargs: t.Any,
|
81
147
|
):
|
148
|
+
"""
|
149
|
+
Generates a response from a chat model based on the given prompt and additional context.
|
150
|
+
|
151
|
+
Args:
|
152
|
+
prompt (str): The main text prompt to send to the chat model.
|
153
|
+
client_id (str, optional): Identifier for the client configuration. Defaults to None.
|
154
|
+
history (list, optional): A list of previous messages to provide context for the conversation. Each message should be a dictionary with "role" and "content". Defaults to None.
|
155
|
+
sys_info (str, optional): System-level information to set the context of the chat. Defaults to None.
|
156
|
+
assis_info (str, optional): Information from the assistant to be included in the conversation. Defaults to None.
|
157
|
+
images (list, optional): A list of images to include in the message content. Defaults to None.
|
158
|
+
image_keys (tuple, optional): Keys to format the image data. Must be of length 1 or 2. Defaults to ("image_url", "url").
|
159
|
+
model (str, optional): The model to use for generating the response. If not provided, it defaults to the one in client configuration for the given client_id.
|
160
|
+
tools (list, optional): List of tools to be available during the chat. Defaults to None.
|
161
|
+
stream (bool, optional): Whether to stream the response. Defaults to True.
|
162
|
+
response_model (optional): Specifies the response model to use. Defaults to None.
|
163
|
+
**kwargs (Any): Additional configuration parameters.
|
164
|
+
|
165
|
+
Returns:
|
166
|
+
Response: The response object from the chat model.
|
167
|
+
"""
|
82
168
|
if not model:
|
83
169
|
model = self.client_conf[client_id]['model']
|
84
170
|
|
85
171
|
client = self.client_conf[client_id]['client']
|
86
172
|
if response_model:
|
173
|
+
import instructor #TODO 有些模型支持这个 instructor 的结构化输出,但实际上它调用的还是openai api的功能,以后适时删除或补全
|
87
174
|
client = instructor.from_openai(client)
|
88
175
|
|
89
176
|
messages = []
|
@@ -157,6 +244,29 @@ class OpenAIWrapper(object):
|
|
157
244
|
prompt,
|
158
245
|
**kwargs
|
159
246
|
):
|
247
|
+
"""
|
248
|
+
Invoke the API to get a response based on the provided prompt.
|
249
|
+
|
250
|
+
Args:
|
251
|
+
prompt (str): The input prompt to be processed.
|
252
|
+
**kwargs: Additional keyword arguments to customize the API request.
|
253
|
+
|
254
|
+
Returns:
|
255
|
+
dict: A dictionary containing the type of response and its contents.
|
256
|
+
The possible keys are:
|
257
|
+
- 'type' (str): Indicates the type of response ('text' or 'tool_calls').
|
258
|
+
- 'contents' (str, optional): The text content if the response type is 'text'.
|
259
|
+
- 'tool_params' (dict, optional): The parameters of the tool called if the response type is 'tool_calls'.
|
260
|
+
|
261
|
+
Examples:
|
262
|
+
>>> llm.invoke(
|
263
|
+
>>> client_id="glm_4_flash",
|
264
|
+
>>> prompt="深圳天气怎么样?",
|
265
|
+
>>> tools=[TOOL_DICT['get_weather']],
|
266
|
+
>>> )
|
267
|
+
{'type': 'tool_calls',
|
268
|
+
'tool_parmas': Function(arguments='{"location": "Shenzhen"}', name='get_weather')}
|
269
|
+
"""
|
160
270
|
answer_dict = {}
|
161
271
|
|
162
272
|
resp = self.get_resp(
|
@@ -174,6 +284,34 @@ class OpenAIWrapper(object):
|
|
174
284
|
return answer_dict
|
175
285
|
|
176
286
|
def stream(self, prompt, **kwargs):
|
287
|
+
"""
|
288
|
+
Streams responses based on the provided prompt, yielding chunks of data.
|
289
|
+
|
290
|
+
This function calls the `get_resp` method with the prompt and additional keyword arguments,
|
291
|
+
streaming the response in chunks. It processes each chunk to yield either tool call parameters
|
292
|
+
or textual content. If an error occurs while processing the chunks, it yields an error message.
|
293
|
+
|
294
|
+
Args:
|
295
|
+
prompt (str): The input prompt to generate responses for.
|
296
|
+
**kwargs: Additional keyword arguments to be passed to the `get_resp` method.
|
297
|
+
|
298
|
+
Yields:
|
299
|
+
dict: A dictionary with the following possible keys:
|
300
|
+
- type (str): Indicates the type of the response ('tool_calls', 'text', or 'error').
|
301
|
+
- tool_params (dict, optional): Parameters of the tool call if the type is 'tool_calls'.
|
302
|
+
- content (str, optional): The generated text content if the type is 'text'.
|
303
|
+
- message (str, optional): An error message if the type is 'error'.
|
304
|
+
|
305
|
+
Examplse:
|
306
|
+
>>> resp = llm.stream(
|
307
|
+
>>> client_id="r1", #此模型可以进行cot
|
308
|
+
>>> prompt=prompt,
|
309
|
+
>>> # tools=[TOOL_DICT['get_weather']],
|
310
|
+
>>> )
|
311
|
+
>>> for i in resp:
|
312
|
+
>>> if i['type'] == 'text' and i['content']:
|
313
|
+
>>> print(i['content'], flush=True, end="")
|
314
|
+
"""
|
177
315
|
resp = self.get_resp(prompt=prompt, stream=True, **kwargs)
|
178
316
|
|
179
317
|
for chunk in resp:
|
@@ -206,7 +344,3 @@ class OpenAIWrapper(object):
|
|
206
344
|
return
|
207
345
|
|
208
346
|
return
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
@@ -1,5 +1,5 @@
|
|
1
1
|
hdl/__init__.py,sha256=GffnD0jLJdhkd-vo989v40N90sQbofkayRBwxc6TVhQ,72
|
2
|
-
hdl/_version.py,sha256=
|
2
|
+
hdl/_version.py,sha256=q4JPltmBqBzXzF4sbXugC3X2R4f0B_3ypR7bRe2rY5c,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
|
@@ -134,15 +134,15 @@ hdl/utils/llm/chatgr.py,sha256=5F5PJHe8vz3iCfi4TT54DCLRi1UeJshECdVtgvvvao0,3696
|
|
134
134
|
hdl/utils/llm/embs.py,sha256=Tf0FOYrOFZp7qQpEPiSCXzlgyHH0X9HVTUtsup74a9E,7174
|
135
135
|
hdl/utils/llm/extract.py,sha256=2sK_WJzmYIc8iuWaM9DA6Nw3_6q1O4lJ5pKpcZo-bBA,6512
|
136
136
|
hdl/utils/llm/llama_chat.py,sha256=watcHGOaz-bv3x-yDucYlGk5f8FiqfFhwWogrl334fk,4387
|
137
|
-
hdl/utils/llm/llm_wrapper.py,sha256=
|
137
|
+
hdl/utils/llm/llm_wrapper.py,sha256=DVpt4TeFN3uWsL1L6BQT4FUKFOvilSNhbR9tfVJIQus,13663
|
138
138
|
hdl/utils/llm/vis.py,sha256=SSP6tOwKLq0hWcpM3twI9TitqzBmKjlcGrnXEWYlCzM,26055
|
139
139
|
hdl/utils/llm/visrag.py,sha256=0i-VrxqgiV-J7R3VPshu9oc7-rKjFJOldYik3HDXj6M,10176
|
140
140
|
hdl/utils/schedulers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
141
141
|
hdl/utils/schedulers/norm_lr.py,sha256=bDwCmdEK-WkgxQMFBiMuchv8Mm7C0-GZJ6usm-PQk14,4461
|
142
142
|
hdl/utils/weather/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
143
143
|
hdl/utils/weather/weather.py,sha256=k11o6wM15kF8b9NMlEfrg68ak-SfSYLN3nOOflFUv-I,4381
|
144
|
-
hjxdl-0.3.
|
145
|
-
hjxdl-0.3.
|
146
|
-
hjxdl-0.3.
|
147
|
-
hjxdl-0.3.
|
148
|
-
hjxdl-0.3.
|
144
|
+
hjxdl-0.3.22.dist-info/LICENSE,sha256=lkMiSbeZHBQLB9LJEkS9-L3Z-LBC4yGnKrzHSG8RkPM,2599
|
145
|
+
hjxdl-0.3.22.dist-info/METADATA,sha256=F8oNEqwUBDPVS0izQtAppzqat2aXaMm4uE68j6pGyGE,1336
|
146
|
+
hjxdl-0.3.22.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
|
147
|
+
hjxdl-0.3.22.dist-info/top_level.txt,sha256=-kxwTM5JPhylp06z3zAVO3w6_h7wtBfBo2zgM6YZoTk,4
|
148
|
+
hjxdl-0.3.22.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|