hjxdl 0.1.6__py3-none-any.whl → 0.1.7__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 +79 -4
- hdl/utils/llm/embs.py +25 -1
- {hjxdl-0.1.6.dist-info → hjxdl-0.1.7.dist-info}/METADATA +1 -1
- {hjxdl-0.1.6.dist-info → hjxdl-0.1.7.dist-info}/RECORD +7 -7
- {hjxdl-0.1.6.dist-info → hjxdl-0.1.7.dist-info}/WHEEL +1 -1
- {hjxdl-0.1.6.dist-info → hjxdl-0.1.7.dist-info}/top_level.txt +0 -0
hdl/_version.py
CHANGED
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
|
5
|
-
|
6
|
-
|
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
@@ -185,4 +185,28 @@ class HFEmbedder():
|
|
185
185
|
output_1 = self.encode(sentences_1, *args, **kwargs)
|
186
186
|
output_2 = self.encode(sentences_2, *args, **kwargs)
|
187
187
|
similarity = output_1 @ output_2.T
|
188
|
-
return similarity
|
188
|
+
return similarity
|
189
|
+
|
190
|
+
|
191
|
+
def get_n_tokens(
|
192
|
+
paragraph,
|
193
|
+
model: str = None
|
194
|
+
):
|
195
|
+
"""Get the number of tokens in a paragraph using a specified model.
|
196
|
+
|
197
|
+
Args:
|
198
|
+
paragraph (str): The input paragraph to tokenize.
|
199
|
+
model (str): The name of the model to use for tokenization. If None, a default CJK tokenization will be used.
|
200
|
+
|
201
|
+
Returns:
|
202
|
+
int: The number of tokens in the paragraph based on the specified model or default CJK tokenization.
|
203
|
+
"""
|
204
|
+
if model is None:
|
205
|
+
cjk_regex = re.compile(u'[\u1100-\uFFFDh]+?')
|
206
|
+
trimed_cjk = cjk_regex.sub( ' a ', paragraph, 0)
|
207
|
+
return len(trimed_cjk.split())
|
208
|
+
else:
|
209
|
+
import tiktoken
|
210
|
+
encoding = tiktoken.encoding_for_model(model)
|
211
|
+
num_tokens = len(encoding.encode(paragraph))
|
212
|
+
return num_tokens
|
@@ -1,5 +1,5 @@
|
|
1
1
|
hdl/__init__.py,sha256=5sZZNySv08wwfzJcSDssGTqUn9wlmDsR6R4XB8J8mFM,70
|
2
|
-
hdl/_version.py,sha256=
|
2
|
+
hdl/_version.py,sha256=0A08Kvw-SYs_CkLPEV1KmD8lb9IPH1psSxb5iQEGtI8,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=
|
88
|
-
hdl/utils/llm/embs.py,sha256=
|
87
|
+
hdl/utils/llm/chat.py,sha256=gsbqWh8fTcJUENU6ZuMClZAuSOLFnD5VP8kXOxGh3Zw,13776
|
88
|
+
hdl/utils/llm/embs.py,sha256=ntT9Noax4ao7Rm1kjVMtXa3j0ykbRq0cABAci147Bu0,7159
|
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.
|
93
|
-
hjxdl-0.1.
|
94
|
-
hjxdl-0.1.
|
95
|
-
hjxdl-0.1.
|
92
|
+
hjxdl-0.1.7.dist-info/METADATA,sha256=MMDp3uVtFocfPk5vDF056atHLM0dBDzPu6GrLZXbGDg,542
|
93
|
+
hjxdl-0.1.7.dist-info/WHEEL,sha256=R0nc6qTxuoLk7ShA2_Y-UWkN8ZdfDBG2B6Eqpz2WXbs,91
|
94
|
+
hjxdl-0.1.7.dist-info/top_level.txt,sha256=-kxwTM5JPhylp06z3zAVO3w6_h7wtBfBo2zgM6YZoTk,4
|
95
|
+
hjxdl-0.1.7.dist-info/RECORD,,
|
File without changes
|