-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
420 lines (340 loc) · 16.6 KB
/
models.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
import os
from abc import ABC
import concurrent.futures
from tqdm import tqdm
import time
from typing import Optional
import torch
from openai import OpenAI
from transformers import AutoModelForCausalLM, AutoTokenizer
try:
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
from mistral_common.protocol.instruct.messages import UserMessage
from mistral_common.protocol.instruct.request import ChatCompletionRequest
except ImportError:
print("Mistral is not installed. Related models will not work.")
class LLMBaseModel(ABC):
"""
Abstract base class for language model interfaces.
This class provides a common interface for various language models and includes methods for prediction.
Parameters:
-----------
model : str
The name of the language model.
max_new_tokens : int
The maximum number of new tokens to be generated.
temperature : float
The temperature for text generation (default is 0).
device: str
The device to use for inference (default is 'auto').
Methods:
--------
predict(input_text, **kwargs)
Generates a prediction based on the input text.
__call__(input_text, **kwargs)
Shortcut for predict method.
"""
def __init__(self, model_name, max_new_tokens, temperature, device='auto'):
self.model_name = model_name
self.max_new_tokens = max_new_tokens
self.temperature = temperature
if device == 'auto':
device = 'cuda' if torch.cuda.is_available() else 'cpu'
self.device = device
def predict(self, input_text, **kwargs):
raise NotImplementedError("The predict method must be implemented in the derived class.")
def __call__(self, input_text, **kwargs):
return self.predict(input_text, **kwargs)
class InternLMModel(LLMBaseModel):
def __init__(self, model_name, max_new_tokens, temperature, device, dtype):
super(InternLMModel, self).__init__(model_name, max_new_tokens, temperature, device)
from transformers import AutoTokenizer, AutoModelForCausalLM
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name, trust_remote_code=True, torch_dtype=dtype, device_map=device)
self.model = AutoModelForCausalLM.from_pretrained(self.model_name, trust_remote_code=True, torch_dtype=dtype, device_map=device)
self.model = self.model.eval()
def predict(self, input_text, **kwargs):
response, history = self.model.chat(self.tokenizer, input_text, history=[])
return response
class YiModel(LLMBaseModel):
"""
Language model class for the Yi model.
Inherits from LLMBaseModel and sets up the Yi language model for use.
Parameters:
-----------
model : str
The name of the Yi model.
max_new_tokens : int
The maximum number of new tokens to be generated.
temperature : float
The temperature for text generation (default is 0).
device: str
The device to use for inference (default is 'auto').
"""
def __init__(self, model_name, max_new_tokens, temperature, device, dtype, system_prompt=None):
super(YiModel, self).__init__(model_name, max_new_tokens, temperature, device)
from transformers import AutoTokenizer, AutoModelForCausalLM
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name, torch_dtype=dtype, device_map=device)
self.model = AutoModelForCausalLM.from_pretrained(self.model_name, torch_dtype=dtype, device_map=device).eval()
self.system_prompt = system_prompt if system_prompt is not None else "You are a helpful assistant."
def predict(self, input_text, **kwargs):
messages = [{"role": "system", "content": self.system_prompt}, {"role": "user", "content": input_text}]
input_ids = self.tokenizer.apply_chat_template(conversation=messages, tokenize=True, add_generation_prompt=True, return_tensors='pt')
output_ids = self.model.generate(
input_ids.to(self.device),
temperature=self.temperature if self.temperature > 1e-3 else None,
top_p=0.9 if self.temperature > 1e-3 else None,
max_new_tokens=self.max_new_tokens,
do_sample=True if self.temperature > 1e-3 else False,
)
response = self.tokenizer.decode(output_ids[0][input_ids.shape[1]:], skip_special_tokens=True)
# Model response: "Hello! How can I assist you today?"
return response
class MistralModel(LLMBaseModel):
"""
Language model class for the Mistral model.
Inherits from LLMBaseModel and sets up the Mistral language model for use.
Parameters:
-----------
model : str
The name of the Mistral model.
max_new_tokens : int
The maximum number of new tokens to be generated.
temperature : float
The temperature for text generation (default is 0).
device: str
The device to use for inference (default is 'auto').
dtype: str
The dtype to use for inference (default is 'auto').
"""
def __init__(self, model_name, max_new_tokens, temperature, device, dtype):
temperature = max(temperature, 0.01)
super(MistralModel, self).__init__(model_name, max_new_tokens, temperature, device)
self.tokenizer = MistralTokenizer.v1()
self.model = AutoModelForCausalLM.from_pretrained(self.model_name, torch_dtype=dtype, device_map=device)
def predict(self, input_text, **kwargs):
completion_request = ChatCompletionRequest(messages=[UserMessage(content=input_text)])
tokens = self.tokenizer.encode_chat_completion(completion_request).tokens
tokens = torch.tensor(tokens).unsqueeze(0).to(self.device)
generated_ids = self.model.generate(
tokens,
max_new_tokens=self.max_new_tokens,
temperature=self.temperature if self.temperature > 1e-3 else None,
top_p=0.9 if self.temperature > 1e-3 else None,
do_sample=True if self.temperature > 1e-3 else False,
pad_token_id=self.tokenizer.instruct_tokenizer.tokenizer.eos_id,
**kwargs,
)
# decode with mistral tokenizer
result = self.tokenizer.decode(generated_ids[0].tolist())
# Return the content after [/INST]
response = result.split("[/INST]")[1]
return response
class LlamaModel(LLMBaseModel):
"""
Language model class for the Llama model.
Inherits from LLMBaseModel and sets up the Llama language model for use.
Parameters:
-----------
model : str
The name of the Llama model.
max_new_tokens : int
The maximum number of new tokens to be generated.
temperature : float
The temperature for text generation (default is 0).
device: str
The device to use for inference (default is 'auto').
dtype: str
The dtype to use for inference (default is 'auto').
system_prompt : str
The system prompt to be used (default is 'You are a helpful assistant.').
"""
def __init__(self, model_name, max_new_tokens, temperature, device, dtype, system_prompt):
super(LlamaModel, self).__init__(model_name, max_new_tokens, temperature, device)
if system_prompt is None:
self.system_prompt = "You are a helpful assistant."
else:
self.system_prompt = system_prompt
self.tokenizer = AutoTokenizer.from_pretrained(model_name, device_map=device, torch_dtype=dtype)
self.model = AutoModelForCausalLM.from_pretrained(model_name, device_map=device, torch_dtype=dtype)
def predict(self, input_text, **kwargs):
input_text = f"<s>[INST] <<SYS>>{self.system_prompt}<</SYS>>\n{input_text}[/INST]"
input_ids = self.tokenizer(input_text, return_tensors="pt").input_ids.to(self.device)
outputs = self.model.generate(input_ids,
max_new_tokens=self.max_new_tokens,
temperature=self.temperature if self.temperature > 1e-3 else None,
top_p=0.9 if self.temperature > 1e-3 else None,
do_sample=True if self.temperature > 1e-3 else False,
**kwargs)
out = self.tokenizer.decode(outputs[0],
skip_special_tokens=True,
clean_up_tokenization_spaces=False)
return out[len(input_text)-1:]
class VicunaModel(LLMBaseModel):
"""
Language model class for the Vicuna model.
# https://github.com/lm-sys/FastChat/blob/main/fastchat/serve/huggingface_api.py
Inherits from LLMBaseModel and sets up the Vicuna language model for use.
Parameters:
-----------
model : str
The name of the Vicuna model.
max_new_tokens : int
The maximum number of new tokens to be generated.
temperature : float, optional
The temperature for text generation (default is 0).
device: str
The device to use for inference (default is 'auto').
dtype: str
The dtype to use for inference (default is 'auto').
"""
def __init__(self, model_name, max_new_tokens, temperature, device, dtype):
super(VicunaModel, self).__init__(model_name, max_new_tokens, temperature, device)
self.tokenizer = AutoTokenizer.from_pretrained(model_name, device_map=device, torch_dtype=dtype, use_fast=False)
self.model = AutoModelForCausalLM.from_pretrained(model_name, device_map=device, torch_dtype=dtype)
def predict(self, input_text, **kwargs):
input_ids = self.tokenizer(input_text, return_tensors="pt").input_ids.to(self.device)
print(self.temperature)
output_ids = self.model.generate(
input_ids,
max_new_tokens=self.max_new_tokens,
temperature=self.temperature if self.temperature > 1e-3 else None,
do_sample=True if self.temperature > 1e-3 else False,
repetition_penalty=1.2, # https://github.com/lm-sys/FastChat/blob/main/fastchat/serve/huggingface_api.py
**kwargs
)
output_ids = output_ids[0][len(input_ids[0]) :]
outputs = self.tokenizer.decode(
output_ids, skip_special_tokens=True, spaces_between_special_tokens=False
)
return outputs
class OpenAIModel(LLMBaseModel):
"""
Language model class for interfacing with OpenAI's GPT models or Llama API models.
Inherits from LLMBaseModel and sets up a model interface for OpenAI GPT models.
Parameters:
-----------
model : str
The name of the OpenAI model.
max_new_tokens : int
The maximum number of new tokens to be generated.
temperature : float
The temperature for text generation (default is 0).
system_prompt : str
The system prompt to be used (default is 'You are a helpful assistant.').
openai_key : str
The OpenAI API key (default is None).
Methods:
--------
predict(input_text)
Predicts the output based on the given input text using the OpenAI model.
"""
def __init__(self, model_name, max_new_tokens, temperature, system_prompt=None, openai_key=None):
super(OpenAIModel, self).__init__(model_name, max_new_tokens, temperature)
self.openai_key = openai_key
self.system_prompt = system_prompt
def predict(self, input_text, kwargs={}):
client = OpenAI(api_key=self.openai_key if self.openai_key is not None else os.environ['OPENAI_API_KEY'])
if self.system_prompt is None:
system_messages = {'role': "system", 'content': "You are a helpful assistant."}
else:
system_messages = {'role': "system", 'content': self.system_prompt}
if isinstance(input_text, list):
messages = input_text
elif isinstance(input_text, dict):
messages = [input_text]
else:
messages = [{"role": "user", "content": input_text}]
messages.insert(0, system_messages)
# extra parameterss
n = kwargs['n'] if 'n' in kwargs else 1
temperature = kwargs['temperature'] if 'temperature' in kwargs else self.temperature
max_new_tokens = kwargs['max_new_tokens'] if 'max_new_tokens' in kwargs else self.max_new_tokens
response_format = kwargs['response_format'] if 'response_format' in kwargs else None
for attempt in range(1000):
try:
response = client.chat.completions.create(
model=self.model_name,
messages=messages,
temperature=temperature,
max_tokens=max_new_tokens,
n=n,
response_format={"type": "json_object"} if response_format=="json" else None,
)
break
except Exception as e:
print(f"Error: {e}")
print(f"Retrying ({attempt + 1})...")
time.sleep(1)
if n > 1:
result = [choice.message.content for choice in response.choices]
else:
result = response.choices[0].message.content
return result
def multi_predict(self, input_texts, **kwargs):
"""
An example of input_texts:
input_texts = ["Hello!", "How are you?", "Tell me a joke."]
"""
with concurrent.futures.ThreadPoolExecutor() as executor:
args = [(messages, kwargs) for messages in input_texts]
contents = executor.map(lambda p: self.predict(*p), args)
return list(contents)
def batch_predict(self, input_texts, **kwargs):
assert "n" not in kwargs or kwargs["n"] == 1, "n > 1 is not supported for batch prediction."
responses_list = []
batch_size = kwargs["batch_size"] if "batch_size" in kwargs else 50
for start_idx in tqdm(range(0, len(input_texts), batch_size), disable=False):
end_idx = min(start_idx + batch_size, len(input_texts))
batch_input_texts = input_texts[start_idx: end_idx]
batch_results_list = self.multi_predict(batch_input_texts, **kwargs)
responses_list.extend(batch_results_list)
# Save responses to file
# with open(f"temp-file-responses-{self.model_name}.txt", "a") as f:
# for response in batch_results_list:
# f.write(response + "\n")
return responses_list
class LlamaAPIModel(OpenAIModel):
def __init__(self, model_name, max_new_tokens, temperature, system_prompt=None, llama_key=None):
super(LlamaAPIModel, self).__init__(model_name, max_new_tokens, temperature, system_prompt, llama_key)
self.system_prompt = system_prompt
self.llama_key = llama_key
def predict(self, input_text, kwargs={}):
client = OpenAI(
api_key = self.llama_key if self.llama_key is not None else os.environ['LLAMA_API_KEY'],
base_url = "https://api.llama-api.com"
)
if self.system_prompt is None:
system_messages = {'role': "system", 'content': "You are a helpful assistant."}
else:
system_messages = {'role': "system", 'content': self.system_prompt}
if isinstance(input_text, list):
messages = input_text
elif isinstance(input_text, dict):
messages = [input_text]
else:
messages = [{"role": "user", "content": input_text}]
messages.insert(0, system_messages)
# extra parameterss
n = kwargs['n'] if 'n' in kwargs else 1
temperature = kwargs['temperature'] if 'temperature' in kwargs else self.temperature
max_new_tokens = kwargs['max_new_tokens'] if 'max_new_tokens' in kwargs else self.max_new_tokens
response_format = kwargs['response_format'] if 'response_format' in kwargs else None
response = client.chat.completions.create(
model=self.model_name,
messages=messages,
temperature=temperature,
max_tokens=max_new_tokens,
n=n,
response_format={"type": "json_object"} if response_format=="json" else None,
)
if n > 1:
result = [choice.message.content for choice in response.choices]
else:
result = response.choices[0].message.content
return result
if __name__ == "__main__":
model_name = "llama3.1-405b"
model = LlamaAPIModel(model_name, max_new_tokens=4096, temperature=0)
user_prompt = "Hi there"
response = model.predict(user_prompt)
print(response)