-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
llms.py
466 lines (374 loc) · 14.1 KB
/
llms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import os
os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
import nltk, torch
from trl import setup_chat_format
from transformers import (
AutoTokenizer,
pipeline,
PretrainedConfig,
PreTrainedTokenizer,
PreTrainedTokenizerFast,
PretrainedConfig,
Pipeline,
AutoModelForCausalLM,
PreTrainedModel,
)
from app.core.config import settings
# nltk.download("punkt")
# nltk.download("punkt_tab")
# nltk.download('punkt')
# nltk.download('punkt_tab')
# tokenizer = AutoTokenizer.from_pretrained(settings.LLM_MODEL_ID, token=settings.HF_TKN)
# config = PretrainedConfig.from_pretrained(settings.LLM_MODEL_ID, token=settings.HF_TKN)
# config_dict = config.to_dict()
# config_dict["top_p"] = 0.8
# config_dict["top_k"] = 20
# config_dict["temperature"] = 0.7
# experiment with different values for the repetition_penalty.
# config_dict["repetition_penalty"] = 1.2
# repetition_penalty: contrls how much the model will avoid repeating the same token in the output
# (expoentialy scales the prob of tkn if its already in the output)
# config_dict["typical_p"] = 0.8
# typical_p: controlls how prob the next token can be (bsed on tkn ctx),
# if its to random it'll denies the tkn
# config.from_dict(config_dict)
relevant_keys = [
"top_p",
"top_k",
"temperature",
"repetition_penalty",
"typical_p",
"bos_token_id",
"eos_token_id",
"sep_token_id",
"num_beams",
]
special_keys = ["bos_token_id", "eos_token_id", "sep_token_id"]
# print("\n----- Model Configs ------\n")
# for key, value in config_dict.items():
# if key in relevant_keys:
# if key in special_keys:
# if isinstance(value, int):
# Konvertiere einzelne Token-IDs in Strings
# display_value = tokenizer.decode([value])
# elif isinstance(value, list):
# Konvertiere Listen von Token-IDs in Strings
# display_value = tokenizer.decode(value)
# else:
# Falls der Wert weder int noch Liste ist, gebe ihn direkt aus
# display_value = value
# else:
# display_value = value
# print(f"{key.upper()}: {display_value}")
# print("\n----- Model Configs End ------\n")
# Initialize the text-generation pipeline
# pipe = pipeline(
# "text-generation",
# tokenizer=tokenizer,
# model=settings.LLM_MODEL_ID,
# framework="pt",
# torch_dtype=torch.bfloat16,
# device_map="auto",
# token=settings.HF_TKN,
# config=config,
# )
# __all__ = ["pipe", "tokenizer", "config"]
############## TYPES ##############
from dataclasses import dataclass
from typing import Literal
# from dataclasses import asdict
@dataclass
class Message:
role: Literal["system", "user", "assistant"]
content: str
def __post_init__(self):
if not isinstance(self.role, str) or self.role not in (
"system",
"user",
"assistant",
):
print(f"Invalid role: {str(self.role)} for chat message")
raise ValueError(f"Invalid role: {self.role}")
if not isinstance(self.content, str):
print(f"Invalid content type: {type(self.content)} for chat message")
raise TypeError(f"Invalid content type: {type(self.content)}")
############## TYPES END ##############
class LLamaModel:
def __init__(self):
self.pipe: Pipeline | None = None
self.tokenizer: (PreTrainedTokenizer | PreTrainedTokenizerFast) | None = None
self.config: PretrainedConfig | None = None
self.occurred_errors = []
self.last_error = None
self.current_pipe_setting = "text-generation"
self.__initiate()
def __handle_errors(self, e: Exception, step: str, custom_message: str = None):
print(f"An error occurred in step {step}: {e}")
self.occurred_errors.append(
{"error": e, "step": step, "custom_message": custom_message}
)
self.last_error = e
return "An error occurred."
def __initiate(self):
if self.pipe and self.tokenizer and self.config:
return print("Model already initiated.")
print("Loading nltk...")
nltk.download("punkt")
nltk.download("punkt_tab")
print("Nltk loaded.")
exception = self.__load_tokenizer()
if exception:
raise Exception(self.last_error)
exception = self.__load_config()
if exception:
raise Exception(self.last_error)
exception = self.__load_pipe()
if exception:
raise Exception(self.last_error)
print("Model initiated.")
self.current_pipe_setting = "text-generation"
def __load_tokenizer(self):
print("Loading tokenizer...")
try:
self.tokenizer = AutoTokenizer.from_pretrained(
settings.LLM_MODEL_ID, token=settings.HF_TKN
)
return print("Tokenizer loaded.")
except Exception as e:
return self.__handle_errors(
e, "loading_tokenizer", "Failed to load tokenizer."
)
def __load_config(self):
print(f"Init configs for model {settings.LLM_MODEL_ID}...")
try:
config = PretrainedConfig.from_pretrained(
settings.LLM_MODEL_ID, token=settings.HF_TKN
)
config_dict = config.to_dict()
config_dict["top_p"] = 0.8
config_dict["top_k"] = 20
config_dict["temperature"] = 0.7
config_dict["repetition_penalty"] = 1.2
config_dict["typical_p"] = 0.8
config.from_dict(config_dict)
self.__print_configs(config_dict)
self.config = config
return print("Config loaded.")
except Exception as e:
return self.__handle_errors(e, "loading_config", "Failed to load config.")
def __load_pipe(self, task: str = "text-generation"):
if not self.tokenizer:
if self.__load_tokenizer() == "An error occurred.":
return self.__handle_errors(
self.last_error,
"loading_pipe",
"Failed to load pipeline, because cant load tokenizer...",
)
if not self.config:
if self.__load_config() == "An error occurred.":
return self.__handle_errors(
self.last_error,
"loading_pipe",
"Failed to load pipeline, because cant load config...",
)
print("Loading pipeline...")
try:
self.pipe = pipeline(
task,
tokenizer=self.tokenizer,
model=settings.LLM_MODEL_ID,
framework="pt",
torch_dtype=torch.bfloat16,
device_map="auto",
token=settings.HF_TKN,
config=self.config,
)
return print("Pipeline loaded.")
except Exception as e:
return self.__handle_errors(e, "loading_pipe", "Failed to load pipeline.")
def __initiate_custom_pipe_without_configs(self, task: str):
raise Exception("Not implemented yet.")
if not self.tokenizer:
if self.__load_tokenizer() == "An error occurred.":
return self.__handle_errors(
self.last_error,
"loading_pipe",
"Failed to load pipeline, because cant load tokenizer...",
)
if not self.config:
if self.__load_config() == "An error occurred.":
return self.__handle_errors(
self.last_error,
"loading_pipe",
"Failed to load pipeline, because cant load config...",
)
print("Loading pipeline...")
try:
pipe = pipeline(
task,
model=settings.LLM_MODEL_ID,
torch_dtype=torch.bfloat16,
token=settings.HF_TKN,
)
print("Pipeline loaded.")
return pipe
except Exception as e:
return self.__handle_errors(
e, "initiate_custom_pipe_without_configs", "Failed to load pipeline."
)
def __print_configs(
self,
config_dict: dict,
relevant_keys: list[str] = relevant_keys,
special_keys: list[str] = special_keys,
):
print("\n----- Model Configs ------\n")
for key, value in config_dict.items():
if key in relevant_keys:
if key in special_keys:
if isinstance(value, int):
display_value = self.tokenizer.decode([value])
elif isinstance(value, list):
display_value = self.tokenizer.decode(value)
else:
display_value = value
else:
display_value = value
print(f"{key.upper()}: {display_value}")
print("\n----- Model Configs End ------\n")
def __initiate_custom_configs(self, config_dict: dict):
config = PretrainedConfig.from_dict(config_dict)
configs = config.to_dict()
configs["top_p"] = config_dict.get("top_p", 0.8)
configs["top_k"] = config_dict.get("top_k", 20)
configs["temperature"] = config_dict.get("temperature", 0.7)
configs["repetition_penalty"] = config_dict.get("repetition_penalty", 1.2)
configs["typical_p"] = config_dict.get("typical_p", 0.8)
config.from_dict(configs)
self.__print_configs(configs)
self.config = config
def get_pipe(self):
if not self.pipe:
self.__initiate()
return self.pipe
def get_tokenizer(self):
if not self.tokenizer:
self.__initiate()
return self.tokenizer
def get_config(self):
if not self.config:
self.__initiate()
return self.config
def generate_chat_based_assistant(
self, instruction: str
) -> tuple[PreTrainedModel, PreTrainedTokenizer, list[dict[str, str]]] | str:
"""
Example:
instruction = 'You are a top-rated customer service agent named John.
Be polite to customers and answer all their questions.'
messages = [
{"role": "system", "content": instruction},
{"role": "user", "content": "I have to see what payment payment modalities are accepted"}
]
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = tokenizer(prompt, return_tensors='pt', padding=True, truncation=True).to("cuda")
outputs = model.generate(**inputs, max_new_tokens=150, num_return_sequences=1)
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(text.split("assistant")[1])
"""
try:
print("Generating chat-based assistant...")
print("Loading model...")
tokenizer = AutoTokenizer.from_pretrained(
settings.LLM_MODEL_ID, token=settings.HF_TKN
)
print("Setting device...")
device_index = torch.cuda.current_device()
torch.cuda.set_device(device_index)
print("initiating model...")
model = AutoModelForCausalLM.from_pretrained(
settings.LLM_MODEL_ID,
low_cpu_mem_usage=True,
return_dict=True,
torch_dtype=torch.float16,
device_map="auto",
)
print("applying chat format configurations...")
model, tokenizer = setup_chat_format(model, tokenizer)
print("prepare template messages using the provided system instructions...")
# template messages
messages = [
{"role": "system", "content": instruction},
]
print("done!")
return (
model,
tokenizer,
messages,
)
except Exception as e:
return self.__handle_errors(
e,
"generate_chat_based_assistant",
"Failed to generate chat-based assistant.",
)
class LLMUtils:
def __check_messages(self, messages: list[dict[str, str]]):
try:
[Message(**message) for message in messages if isinstance(message, dict)]
except ValueError:
print("Invalid messages")
return "Invalid messages"
except TypeError:
print("Invalid messages")
return "Invalid messages"
except Exception as e:
print(f"An error occurred: {e}")
return "An error occurred."
return None
def generate_output_from_model(
self,
model: PreTrainedModel,
tokenizer: PreTrainedTokenizer,
messages: list[dict[str, str]],
max_new_tokens: int = 150,
):
print("validate messages...")
if self.__check_messages(messages):
raise Exception("Invalid messages")
print("Generating output from model...")
print("applying chat template...")
prompt = tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
print("tokenizing prompt...")
inputs = tokenizer(
prompt, return_tensors="pt", padding=True, truncation=True
).to("cuda")
print("generating outputs...")
outputs = model.generate(
**inputs, max_new_tokens=max_new_tokens, num_return_sequences=1
)
print("decoding outputs...")
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
print("done!")
return text.split("assistant")[1]
def generate_output_from_pipe(
self,
pipe: Pipeline,
tokenizer: PreTrainedTokenizer,
complete_prompt: str,
max_new_tokens: int = 150,
):
print("Generating output from pipe...")
streamed = pipe(
complete_prompt,
max_new_tokens=max_new_tokens,
pad_token_id=tokenizer.pad_token_id,
eos_token_id=tokenizer.eos_token_id,
)[-1]["generated_text"]
print("done!")
return streamed
llama = LLamaModel()
utils = LLMUtils()
__all__ = ["llama", "utils"]