hjxdl 0.1.84__py3-none-any.whl → 0.1.86__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 CHANGED
@@ -12,5 +12,5 @@ __version__: str
12
12
  __version_tuple__: VERSION_TUPLE
13
13
  version_tuple: VERSION_TUPLE
14
14
 
15
- __version__ = version = '0.1.84'
16
- __version_tuple__ = version_tuple = (0, 1, 84)
15
+ __version__ = version = '0.1.86'
16
+ __version_tuple__ = version_tuple = (0, 1, 86)
hdl/utils/llm/chat.py CHANGED
@@ -154,22 +154,19 @@ class OpenAI_M():
154
154
  stream: bool = True,
155
155
  **kwargs: t.Any,
156
156
  ):
157
- """Get response from chat completion model.
157
+ """Get response from chatbot based on the provided prompt and optional images.
158
158
 
159
159
  Args:
160
- prompt (str): The prompt text to generate a response for.
161
- images (list, optional): List of image URLs to include in the prompt. Defaults to [].
160
+ prompt (str): The prompt to provide to the chatbot.
161
+ images (list, optional): List of images to include in the response. Defaults to [].
162
162
  image_keys (tuple, optional): Tuple containing keys for image data. Defaults to ("image", "image").
163
- stop (list[str] | None, optional): List of strings to stop the conversation. Defaults to ["USER:", "ASSISTANT:"].
163
+ stop (list[str] | None, optional): List of strings that indicate the end of the conversation. Defaults to ["USER:", "ASSISTANT:"].
164
164
  model (str, optional): The model to use for generating the response. Defaults to "default_model".
165
- stream (bool, optional): Whether to stream the response or not. Defaults to True.
166
- **kwargs: Additional keyword arguments to pass to the chat completion API.
167
-
168
- Yields:
169
- str: The generated response content.
165
+ stream (bool, optional): Whether to stream the response. Defaults to True.
166
+ **kwargs: Additional keyword arguments to pass to the chatbot API.
170
167
 
171
168
  Returns:
172
- str: The generated response content if stream is False.
169
+ dict: The response from the chatbot.
173
170
  """
174
171
  content = [
175
172
  {"type": "text", "text": prompt},
@@ -196,19 +193,13 @@ class OpenAI_M():
196
193
  model=model,
197
194
  **kwargs
198
195
  )
199
- if not stream:
200
- return response.choices[0].message.content
201
- else:
202
- for chunk in response:
203
- content = chunk.choices[0].delta.content
204
- if content:
205
- yield content
196
+ return response
206
197
 
207
198
  def invoke(
208
199
  self,
209
200
  *args,
210
201
  **kwargs
211
- ) -> str:
202
+ ):
212
203
  """Invoke the function with the given arguments and keyword arguments.
213
204
 
214
205
  Args:
@@ -216,25 +207,160 @@ class OpenAI_M():
216
207
  **kwargs: Arbitrary keyword arguments.
217
208
 
218
209
  Returns:
219
- str: The response obtained by calling the get_resp method with the provided arguments and keyword arguments.
210
+ str: The content of the first choice message in the response.
220
211
  """
221
- return self.get_resp(*args, stream=False, **kwargs)
212
+ response = self.get_resp(*args, stream=False, **kwargs)
213
+ return response.choices[0].message.content
222
214
 
223
215
  def stream(
224
216
  self,
225
217
  *args,
226
218
  **kwargs
227
219
  ):
228
- """Stream data from the server.
220
+ """Stream content from the response in chunks.
221
+
222
+ Args:
223
+ *args: Variable length argument list.
224
+ **kwargs: Arbitrary keyword arguments.
225
+
226
+ Yields:
227
+ str: Content in chunks from the response.
228
+ """
229
+ response = self.get_resp(*args, stream=True, **kwargs)
230
+ for chunk in response:
231
+ content = chunk.choices[0].delta.content
232
+ if content:
233
+ yield content
234
+
235
+
236
+ def chat(self, *args, stream=True, **kwargs):
237
+ """Call either the stream or invoke method based on the value of the stream parameter.
229
238
 
230
239
  Args:
231
240
  *args: Variable length argument list.
241
+ stream (bool): A flag to determine whether to call the stream method (default is True).
232
242
  **kwargs: Arbitrary keyword arguments.
233
243
 
234
244
  Returns:
235
- Response from the server with streaming enabled.
245
+ The result of calling either the stream or invoke method based on the value of the stream parameter.
246
+ """
247
+ if stream:
248
+ return self.stream(*args, **kwargs)
249
+ else:
250
+ return self.invoke(*args, **kwargs)
251
+
252
+
253
+ def invoke_response(
254
+ self,
255
+ prompt : str,
256
+ images: list = [],
257
+ image_keys: tuple = ("image", "image"),
258
+ stop: list[str] | None = ["USER:", "ASSISTANT:"],
259
+ model="default_model",
260
+ **kwargs: t.Any,
261
+ ):
262
+ """Get response from chat completion model.
263
+
264
+ Args:
265
+ prompt (str): The prompt text to generate a response for.
266
+ images (list, optional): List of image URLs to include in the prompt. Defaults to [].
267
+ image_keys (tuple, optional): Tuple containing keys for image data. Defaults to ("image", "image").
268
+ stop (list[str] | None, optional): List of strings to stop the conversation. Defaults to ["USER:", "ASSISTANT:"].
269
+ model (str, optional): The model to use for generating the response. Defaults to "default_model".
270
+ stream (bool, optional): Whether to stream the response or not. Defaults to True.
271
+ **kwargs: Additional keyword arguments to pass to the chat completion API.
272
+
273
+ Yields:
274
+ str: The generated response content.
275
+
276
+ Returns:
277
+ str: The generated response content if stream is False.
278
+ """
279
+ content = [
280
+ {"type": "text", "text": prompt},
281
+ ]
282
+ if images:
283
+ if isinstance(images, str):
284
+ images = [images]
285
+ for img in images:
286
+ content.append({
287
+ "type": image_keys[0],
288
+ image_keys[0]: {
289
+ image_keys[1]: img
290
+ }
291
+ })
292
+ else:
293
+ content = prompt
294
+
295
+ response = self.client.chat.completions.create(
296
+ messages=[{
297
+ "role": "user",
298
+ "content": content
299
+ }],
300
+ stream=False,
301
+ model=model,
302
+ **kwargs
303
+ )
304
+
305
+ return response.choices[0].message.content
306
+
307
+
308
+ def stream_response(
309
+ self,
310
+ prompt : str,
311
+ images: list = [],
312
+ image_keys: tuple = ("image", "image"),
313
+ stop: list[str] | None = ["USER:", "ASSISTANT:"],
314
+ model="default_model",
315
+ **kwargs: t.Any,
316
+ ):
317
+ """Get response from chat completion model.
318
+
319
+ Args:
320
+ prompt (str): The prompt text to generate a response for.
321
+ images (list, optional): List of image URLs to include in the prompt. Defaults to [].
322
+ image_keys (tuple, optional): Tuple containing keys for image data. Defaults to ("image", "image").
323
+ stop (list[str] | None, optional): List of strings to stop the conversation. Defaults to ["USER:", "ASSISTANT:"].
324
+ model (str, optional): The model to use for generating the response. Defaults to "default_model".
325
+ stream (bool, optional): Whether to stream the response or not. Defaults to True.
326
+ **kwargs: Additional keyword arguments to pass to the chat completion API.
327
+
328
+ Yields:
329
+ str: The generated response content.
330
+
331
+ Returns:
332
+ str: The generated response content if stream is False.
236
333
  """
237
- return self.get_resp(*args, stream=True, **kwargs)
334
+ content = [
335
+ {"type": "text", "text": prompt},
336
+ ]
337
+ if images:
338
+ if isinstance(images, str):
339
+ images = [images]
340
+ for img in images:
341
+ content.append({
342
+ "type": image_keys[0],
343
+ image_keys[0]: {
344
+ image_keys[1]: img
345
+ }
346
+ })
347
+ else:
348
+ content = prompt
349
+
350
+ response = self.client.chat.completions.create(
351
+ messages=[{
352
+ "role": "user",
353
+ "content": content
354
+ }],
355
+ stream=True,
356
+ model=model,
357
+ **kwargs
358
+ )
359
+
360
+ for chunk in response:
361
+ content = chunk.choices[0].delta.content
362
+ if content:
363
+ yield content
238
364
 
239
365
  def agent_response(
240
366
  self,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: hjxdl
3
- Version: 0.1.84
3
+ Version: 0.1.86
4
4
  Summary: A collection of functions for Jupyter notebooks
5
5
  Home-page: https://github.com/huluxiaohuowa/hdl
6
6
  Author: Jianxing Hu
@@ -1,5 +1,5 @@
1
1
  hdl/__init__.py,sha256=GffnD0jLJdhkd-vo989v40N90sQbofkayRBwxc6TVhQ,72
2
- hdl/_version.py,sha256=d0L59EfPQwDJVDuCyDuNn4UyYbrKwPsGT6cg6MNTnD0,413
2
+ hdl/_version.py,sha256=mZDLtoh_fZR4n83Vo2yyLdXtACoTaSE2Hm8mc_lRCCM,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,7 +127,7 @@ hdl/utils/desc/template.py,sha256=a0UAkkKctt_EHY9UECsIIAwVkGPcM1Hr01HSkRMeIuw,12
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=XU_tEwRGRDlg7KPdLetE3K6dbD3aPcmWGU7Vd56g0XQ,11748
130
+ hdl/utils/llm/chat.py,sha256=hkFfPHjTwTJwXS3Dvy7j3nqARHsKoWsqJasfDdCjqgY,16211
131
131
  hdl/utils/llm/embs.py,sha256=Tf0FOYrOFZp7qQpEPiSCXzlgyHH0X9HVTUtsup74a9E,7174
132
132
  hdl/utils/llm/extract.py,sha256=2sK_WJzmYIc8iuWaM9DA6Nw3_6q1O4lJ5pKpcZo-bBA,6512
133
133
  hdl/utils/llm/llama_chat.py,sha256=watcHGOaz-bv3x-yDucYlGk5f8FiqfFhwWogrl334fk,4387
@@ -136,7 +136,7 @@ hdl/utils/schedulers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hS
136
136
  hdl/utils/schedulers/norm_lr.py,sha256=bDwCmdEK-WkgxQMFBiMuchv8Mm7C0-GZJ6usm-PQk14,4461
137
137
  hdl/utils/weather/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
138
138
  hdl/utils/weather/weather.py,sha256=k11o6wM15kF8b9NMlEfrg68ak-SfSYLN3nOOflFUv-I,4381
139
- hjxdl-0.1.84.dist-info/METADATA,sha256=NmWiAiAY4fIeJrJKSEs_FsD7wI76d8QafZW_N4zNWhI,903
140
- hjxdl-0.1.84.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
141
- hjxdl-0.1.84.dist-info/top_level.txt,sha256=-kxwTM5JPhylp06z3zAVO3w6_h7wtBfBo2zgM6YZoTk,4
142
- hjxdl-0.1.84.dist-info/RECORD,,
139
+ hjxdl-0.1.86.dist-info/METADATA,sha256=PqxFzQa-kw7zcPAfn8_8lKyfbgLIdUonwSH-CXFyyP8,903
140
+ hjxdl-0.1.86.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
141
+ hjxdl-0.1.86.dist-info/top_level.txt,sha256=-kxwTM5JPhylp06z3zAVO3w6_h7wtBfBo2zgM6YZoTk,4
142
+ hjxdl-0.1.86.dist-info/RECORD,,
File without changes