hjxdl 0.0.2__py3-none-any.whl → 0.0.3__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.0.2'
16
- __version_tuple__ = version_tuple = (0, 0, 2)
15
+ __version__ = version = '0.0.3'
16
+ __version_tuple__ = version_tuple = (0, 0, 3)
File without changes
hdl/utils/llm/chat.py ADDED
@@ -0,0 +1,164 @@
1
+ import typing as t
2
+
3
+ from llama_cpp import Llama
4
+ from jupyfuncs.llm.openapi import (
5
+ chat_oai_invoke,
6
+ chat_oai_stream
7
+ )
8
+
9
+
10
+ class GGUF_M():
11
+ def __init__(
12
+ self,
13
+ model_path :str,
14
+ device: str='gpu',
15
+ generation_kwargs: dict = {},
16
+ server_ip: str = "127.0.0.1",
17
+ server_port: int = 8000,
18
+ ):
19
+ """Initialize the model from a local path.
20
+ Here it does not manage chat histories.
21
+
22
+ Args:
23
+ model_path (str): The path to the model.
24
+ device (str, optional): The device to use for model initialization. Defaults to 'gpu'.
25
+ generation_kwargs (dict, optional): Additional keyword arguments for model generation. Defaults to {}.
26
+ """
27
+ # 从本地初始化模型
28
+ super().__init__()
29
+ self.generation_kwargs = generation_kwargs
30
+ self.questions = []
31
+ self.resps = []
32
+ print("正在从本地加载模型...")
33
+ if device == 'cpu':
34
+ self.model = Llama(
35
+ model_path=model_path,
36
+ n_threads=self.generation_kwargs['num_threads'],
37
+ n_ctx=self.generation_kwargs['max_context_length'],
38
+ )
39
+ else:
40
+ self.model = Llama(
41
+ model_path=model_path,
42
+ n_threads=self.generation_kwargs['num_threads'],
43
+ n_ctx=self.generation_kwargs['max_context_length'],
44
+ n_gpu_layers=-1
45
+ )
46
+
47
+ print("完成本地模型的加载")
48
+
49
+ def invoke(
50
+ self,
51
+ prompt : str,
52
+ stop: list[str] | None = ["USER:", "ASSISTANT:"],
53
+ # history: list = [],
54
+ **kwargs: t.Any,
55
+ ) -> str:
56
+ """Invoke the model to generate a response based on the given prompt.
57
+
58
+ Args:
59
+ prompt (str): The prompt to be used for generating the response.
60
+ stop (list[str], optional): List of strings that indicate when the model should stop generating the response. Defaults to ["USER:", "ASSISTANT:"].
61
+ **kwargs: Additional keyword arguments to be passed to the model.
62
+
63
+ Returns:
64
+ str: The generated response based on the prompt.
65
+ """
66
+ prompt_final = f"USER:\n{prompt}\nASSISTANT:\n"
67
+
68
+ result = self.model.create_completion(
69
+ prompt_final,
70
+ repeat_penalty=self.generation_kwargs["repetition_penalty"],
71
+ max_tokens=self.generation_kwargs["max_new_tokens"],
72
+ stop=stop,
73
+ echo=False,
74
+ temperature=self.generation_kwargs["temperature"],
75
+ mirostat_mode = 2,
76
+ mirostat_tau=4.0,
77
+ mirostat_eta=1.1
78
+ )
79
+ resp = result['choices'][0]['text']
80
+ # history.append(
81
+ # [prompt, resp]
82
+ # )
83
+ return resp
84
+
85
+ def stream(
86
+ self,
87
+ prompt: str,
88
+ stop: list[str] | None = ["USER:", "ASSISTANT:"],
89
+ # history: list = [],
90
+ **kwargs: t.Any,
91
+ ):
92
+ """Generate text responses based on the given prompt using the model.
93
+
94
+ Args:
95
+ prompt (str): The prompt to generate text responses.
96
+ stop (list[str], optional): List of strings to stop the generation. Defaults to ["USER:", "ASSISTANT:"].
97
+ **kwargs: Additional keyword arguments for the model.
98
+
99
+ Yields:
100
+ str: Text responses generated by the model based on the prompt.
101
+ """
102
+ self.questions.append(prompt)
103
+ prompt = f"USER:\n{prompt}\nASSISTANT:\n"
104
+ output = self.model.create_completion(
105
+ prompt,
106
+ stream=True,
107
+ repeat_penalty=self.generation_kwargs["repetition_penalty"],
108
+ max_tokens=self.generation_kwargs["max_new_tokens"],
109
+ stop=stop,
110
+ echo=False,
111
+ temperature=self.generation_kwargs["temperature"],
112
+ mirostat_mode = 2,
113
+ mirostat_tau=4.0,
114
+ mirostat_eta=1.1
115
+ )
116
+ # history.append([])
117
+ for chunk in output:
118
+ item = chunk['choices'][0]['text']
119
+ # self.resps[-1].append(item)
120
+ yield chunk['choices'][0]['text']
121
+ # self.resps[-1] = "".join(self.resps[-1])
122
+
123
+
124
+ class OpenAI_M():
125
+ def __init__(
126
+ self,
127
+ model_path: str = None,
128
+ device: str='gpu',
129
+ generation_kwargs: dict = {},
130
+ server_ip: str = "172.28.1.2",
131
+ server_port: int = 8000,
132
+ ):
133
+ self.model_path = model_path
134
+ self.server_ip = server_ip
135
+ self.server_port = server_port
136
+ self.base_url = "http://{self.server_ip}:{str(self.server_port)}/v1"
137
+
138
+ def invoke(
139
+ self,
140
+ prompt : str,
141
+ stop: list[str] | None = ["USER:", "ASSISTANT:"],
142
+ # history: list = [],
143
+ **kwargs: t.Any,
144
+ ) -> str:
145
+ resp = chat_oai_invoke(
146
+ base_url=self.base_url,
147
+ model=self.model_path,
148
+ prompt=prompt
149
+ )
150
+ return resp
151
+
152
+ def stream(
153
+ self,
154
+ prompt : str,
155
+ stop: list[str] | None = ["USER:", "ASSISTANT:"],
156
+ # history: list = [],
157
+ **kwargs: t.Any,
158
+ ) -> str:
159
+ resp = chat_oai_stream(
160
+ base_url=self.base_url,
161
+ model=self.model_path,
162
+ prompt=prompt
163
+ )
164
+ return resp
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: hjxdl
3
- Version: 0.0.2
3
+ Version: 0.0.3
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=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- hdl/_version.py,sha256=NDHlyIcJZjLz8wKlmD1-pr6me5FHBAYwO_ynLG-37N8,411
2
+ hdl/_version.py,sha256=hB095avW4HuDZxn8qPHRG1UMzSSonb8ZDAsLxt9hmk8,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
@@ -83,9 +83,11 @@ hdl/utils/database_tools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZ
83
83
  hdl/utils/database_tools/connect.py,sha256=KUnVG-8raifEJ_N0b3c8LkTTIfn9NIyw8LX6qvpA3YU,723
84
84
  hdl/utils/general/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
85
85
  hdl/utils/general/glob.py,sha256=8-RCnt6L297wMIfn34ZAMCsGCZUjHG3MGglGZI1cX0g,491
86
+ hdl/utils/llm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
87
+ hdl/utils/llm/chat.py,sha256=SoODp3Sf6FNYdajoI39swvPJm64fSd6bFYFdta4xD28,5344
86
88
  hdl/utils/schedulers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
87
89
  hdl/utils/schedulers/norm_lr.py,sha256=bDwCmdEK-WkgxQMFBiMuchv8Mm7C0-GZJ6usm-PQk14,4461
88
- hjxdl-0.0.2.dist-info/METADATA,sha256=_ZhNiflTqXZrDBYdt5v1uG6REZc3wRpmW_76BjYgvP0,525
89
- hjxdl-0.0.2.dist-info/WHEEL,sha256=y4mX-SOX4fYIkonsAGA5N0Oy-8_gI4FXw5HNI1xqvWg,91
90
- hjxdl-0.0.2.dist-info/top_level.txt,sha256=-kxwTM5JPhylp06z3zAVO3w6_h7wtBfBo2zgM6YZoTk,4
91
- hjxdl-0.0.2.dist-info/RECORD,,
90
+ hjxdl-0.0.3.dist-info/METADATA,sha256=IMDR9eTW1PeEGxs6eZESV2ys8LsI0j7aZp6qO9eXZWg,525
91
+ hjxdl-0.0.3.dist-info/WHEEL,sha256=y4mX-SOX4fYIkonsAGA5N0Oy-8_gI4FXw5HNI1xqvWg,91
92
+ hjxdl-0.0.3.dist-info/top_level.txt,sha256=-kxwTM5JPhylp06z3zAVO3w6_h7wtBfBo2zgM6YZoTk,4
93
+ hjxdl-0.0.3.dist-info/RECORD,,
File without changes