hjxdl 0.1.6__py3-none-any.whl → 0.1.8__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.6'
16
- __version_tuple__ = version_tuple = (0, 1, 6)
15
+ __version__ = version = '0.1.8'
16
+ __version_tuple__ = version_tuple = (0, 1, 8)
hdl/utils/llm/chat.py CHANGED
@@ -1,10 +1,85 @@
1
1
  import typing as t
2
2
 
3
3
  from llama_cpp import Llama
4
- from jupyfuncs.llm.openapi import (
5
- chat_oai_invoke,
6
- chat_oai_stream
7
- )
4
+ from openai import OpenAI
5
+ # import traceback
6
+
7
+
8
+ def chat_oai_stream(
9
+ base_url="http://127.0.0.1:8000/v1",
10
+ api_key="dummy_key",
11
+ model="/data/models/Qwen-7B-Chat-Int4",
12
+ prompt="Who are you?",
13
+ *args,
14
+ **kwargs
15
+ ):
16
+ """Chat with OpenAI's GPT-3 model using the specified parameters.
17
+
18
+ Args:
19
+ base_url (str): The base URL for the OpenAI API. Default is "http://127.0.0.1:8000/v1".
20
+ api_key (str): The API key for accessing the OpenAI API. Default is "dummy_key".
21
+ model (str): The model ID to use for the chat. Default is "/data/models/Qwen-7B-Chat-Int4".
22
+ prompt (str): The initial prompt for the chat conversation.
23
+
24
+ Yields:
25
+ str: The generated content from the chat conversation.
26
+
27
+ """
28
+ client = OpenAI(
29
+ base_url=base_url,
30
+ api_key=api_key,
31
+ )
32
+ response = client.chat.completions.create(
33
+ model=model,
34
+ messages=[{
35
+ "role": "user",
36
+ "content": prompt
37
+ }],
38
+ stream=True,
39
+ *args,
40
+ **kwargs
41
+ )
42
+
43
+ for chunk in response:
44
+ content = chunk.choices[0].delta.content
45
+ yield content
46
+
47
+
48
+ def chat_oai_invoke(
49
+ base_url="http://127.0.0.1:8000/v1",
50
+ api_key="dummy_key",
51
+ model="/data/models/Qwen-7B-Chat-Int4",
52
+ prompt="Who are you?",
53
+ *args,
54
+ **kwargs
55
+ ):
56
+ """Invoke OpenAI chat API to generate a response based on the given prompt.
57
+
58
+ Args:
59
+ base_url (str): The base URL of the OpenAI API. Default is "http://127.0.0.1:8000/v1".
60
+ api_key (str): The API key for accessing the OpenAI API. Default is "dummy_key".
61
+ model (str): The model to use for generating the response. Default is "/data/models/Qwen-7B-Chat-Int4".
62
+ prompt (str): The prompt message to start the conversation. Default is "Who are you?".
63
+
64
+ Returns:
65
+ str: The response generated by the OpenAI chat API based on the prompt.
66
+ """
67
+ client = OpenAI(
68
+ base_url=base_url,
69
+ api_key=api_key,
70
+ )
71
+ response = client.chat.completions.create(
72
+ model=model,
73
+ messages=[{
74
+ "role": "user",
75
+ "content": prompt
76
+ }],
77
+ stream=False,
78
+ *args,
79
+ **kwargs
80
+ )
81
+
82
+ return response.choices[0].message.content
8
83
 
9
84
 
10
85
  class GGUF_M(Llama):
hdl/utils/llm/embs.py CHANGED
@@ -1,4 +1,4 @@
1
- from sentence_transformers import SentenceTransformer
1
+ import re
2
2
 
3
3
 
4
4
  class BEEmbedder():
@@ -118,6 +118,8 @@ class HFEmbedder():
118
118
  Returns:
119
119
  None
120
120
  """
121
+
122
+ from sentence_transformers import SentenceTransformer
121
123
 
122
124
  self.device = device
123
125
  self.emb_dir = emb_dir
@@ -185,4 +187,28 @@ class HFEmbedder():
185
187
  output_1 = self.encode(sentences_1, *args, **kwargs)
186
188
  output_2 = self.encode(sentences_2, *args, **kwargs)
187
189
  similarity = output_1 @ output_2.T
188
- return similarity
190
+ return similarity
191
+
192
+
193
+ def get_n_tokens(
194
+ paragraph,
195
+ model: str = ""
196
+ ):
197
+ """Get the number of tokens in a paragraph using a specified model.
198
+
199
+ Args:
200
+ paragraph (str): The input paragraph to tokenize.
201
+ model (str): The name of the model to use for tokenization. If None, a default CJK tokenization will be used.
202
+
203
+ Returns:
204
+ int: The number of tokens in the paragraph based on the specified model or default CJK tokenization.
205
+ """
206
+ if model == "":
207
+ cjk_regex = re.compile(u'[\u1100-\uFFFDh]+?')
208
+ trimed_cjk = cjk_regex.sub( ' a ', paragraph, 0)
209
+ return len(trimed_cjk.split())
210
+ else:
211
+ import tiktoken
212
+ encoding = tiktoken.encoding_for_model(model)
213
+ num_tokens = len(encoding.encode(paragraph))
214
+ return num_tokens
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: hjxdl
3
- Version: 0.1.6
3
+ Version: 0.1.8
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=5sZZNySv08wwfzJcSDssGTqUn9wlmDsR6R4XB8J8mFM,70
2
- hdl/_version.py,sha256=L9DFp_i-1xztMRdin6ZHqLFMlDKUn5bsVgheqXFwTTc,411
2
+ hdl/_version.py,sha256=PdJ7dZoz_SyEgX0MdrMfQYBFlGcwpemv6ibF8NKALBY,411
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
@@ -84,12 +84,12 @@ hdl/utils/database_tools/connect.py,sha256=KUnVG-8raifEJ_N0b3c8LkTTIfn9NIyw8LX6q
84
84
  hdl/utils/general/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
85
85
  hdl/utils/general/glob.py,sha256=8-RCnt6L297wMIfn34ZAMCsGCZUjHG3MGglGZI1cX0g,491
86
86
  hdl/utils/llm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
87
- hdl/utils/llm/chat.py,sha256=H2c8assJlSdZQKIfPkYrVZHqv66TsdsxtaLXv0kNe1w,11565
88
- hdl/utils/llm/embs.py,sha256=sC8tga7HgDwPI2m7TDWKp9kkxEIMxEyMtgmEhfRi4vI,6362
87
+ hdl/utils/llm/chat.py,sha256=gsbqWh8fTcJUENU6ZuMClZAuSOLFnD5VP8kXOxGh3Zw,13776
88
+ hdl/utils/llm/embs.py,sha256=Tf0FOYrOFZp7qQpEPiSCXzlgyHH0X9HVTUtsup74a9E,7174
89
89
  hdl/utils/llm/extract.py,sha256=2sK_WJzmYIc8iuWaM9DA6Nw3_6q1O4lJ5pKpcZo-bBA,6512
90
90
  hdl/utils/schedulers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
91
91
  hdl/utils/schedulers/norm_lr.py,sha256=bDwCmdEK-WkgxQMFBiMuchv8Mm7C0-GZJ6usm-PQk14,4461
92
- hjxdl-0.1.6.dist-info/METADATA,sha256=CN_P3ubA3O0BR-KbIh1hclbj2N8bS_BN-ZaBR2jJCXc,542
93
- hjxdl-0.1.6.dist-info/WHEEL,sha256=-oYQCr74JF3a37z2nRlQays_SX2MqOANoqVjBBAP2yE,91
94
- hjxdl-0.1.6.dist-info/top_level.txt,sha256=-kxwTM5JPhylp06z3zAVO3w6_h7wtBfBo2zgM6YZoTk,4
95
- hjxdl-0.1.6.dist-info/RECORD,,
92
+ hjxdl-0.1.8.dist-info/METADATA,sha256=a9BaE0EGy5G9EM3Tbsi4LMmIrCMFJUuDjFnmmu_nBW4,542
93
+ hjxdl-0.1.8.dist-info/WHEEL,sha256=R0nc6qTxuoLk7ShA2_Y-UWkN8ZdfDBG2B6Eqpz2WXbs,91
94
+ hjxdl-0.1.8.dist-info/top_level.txt,sha256=-kxwTM5JPhylp06z3zAVO3w6_h7wtBfBo2zgM6YZoTk,4
95
+ hjxdl-0.1.8.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (71.0.3)
2
+ Generator: setuptools (72.1.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5