-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.py
158 lines (141 loc) · 5.05 KB
/
model.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
import openai
from openai import OpenAI
from transformers import AutoTokenizer
import torch
import transformers
from prompts import identity
import pdb
from pprint import pprint
import time
def get_model(args):
model_name, temperature, max_new_tokens = args.model, args.temperature, args.max_new_tokens
if 'gpt' in model_name:
model = GPT(args.api_key, model_name, temperature)
return model
elif 'Llama-2' in model_name:
return LLaMA2(model_name, temperature, max_new_tokens)
elif 'Llama-3' in model_name:
return LLaMA3(model_name, temperature, max_new_tokens)
class Model(object):
def __init__(self):
self.post_process_fn = identity
def set_post_process_fn(self, post_process_fn):
self.post_process_fn = post_process_fn
class GPT(Model):
def __init__(self, api_key, model_name, temperature):
super().__init__()
self.model_name = model_name
self.temperature = temperature
self.client = OpenAI(api_key=api_key)
def get_response(self, **kwargs):
try:
res = self.client.chat.completions.create(**kwargs)
return res
except openai.APIConnectionError as e:
print('APIConnectionError')
time.sleep(30)
return self.get_response(**kwargs)
except openai.RateLimitError as e:
print('RateLimitError')
time.sleep(10)
return self.get_response(**kwargs)
except openai.APITimeoutError as e:
print('APITimeoutError')
time.sleep(30)
return self.get_response(**kwargs)
except openai.BadRequestError as e:
print('BadRequestError')
kwargs['messages'] = [{
"role": "user", "content": "Randomly return one letter from A, B, C, D."
}]
return self.get_response(**kwargs)
def forward(self, head, prompts):
messages = [
{"role": "system", "content": head}
]
info = {}
for i, prompt in enumerate(prompts):
messages.append(
{"role": "user", "content": prompt}
)
response = self.get_response(
model=self.model_name,
messages=messages,
temperature=self.temperature,
)
messages.append(
{"role": "assistant", "content": response.choices[0].message.content}
)
info = dict(response.usage) # completion_tokens, prompt_tokens, total_tokens
info['response'] = messages[-1]["content"]
info['message'] = messages
return self.post_process_fn(info['response']), info
class LLaMA2(Model):
def __init__(self, model_name, temperature, max_new_tokens):
super().__init__()
self.model_name = model_name
self.temperature = temperature
self.max_new_tokens = max_new_tokens
tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.pad_token = "[PAD]"
tokenizer.padding_side = "left"
self.tokenizer = tokenizer
self.pipeline = transformers.pipeline(
"text-generation",
model=model_name,
torch_dtype=torch.float16,
device_map="auto",
tokenizer=tokenizer,
temperature=temperature
)
def forward(self, head, prompts):
prompt = prompts[0]
sequences = self.pipeline(
prompt,
do_sample=False,
top_k=1,
num_return_sequences=1,
eos_token_id=self.tokenizer.eos_token_id,
max_new_tokens=self.max_new_tokens,
)
response = sequences[0]['generated_text'] # str
info = {
'message': prompt,
'response': response
}
return self.post_process_fn(info['response']), info
class LLaMA3(Model):
def __init__(self, model_name, temperature, max_new_tokens):
super().__init__()
self.model_name = model_name
self.temperature = temperature
self.max_new_tokens = max_new_tokens
self.pipeline = transformers.pipeline(
"text-generation",
model=model_name,
model_kwargs={"torch_dtype": torch.float16},
device_map="auto",
)
self.terminators = [
self.pipeline.tokenizer.eos_token_id,
self.pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")
]
def forward(self, head, prompts):
prompt = prompts[0]
messages = [
{"role": "system", "content": head},
{"role": "user", "content": prompt}
]
sequences = self.pipeline(
messages,
max_new_tokens=self.max_new_tokens,
eos_token_id=self.terminators,
do_sample=False,
temperature=self.temperature,
)
response = sequences[0]["generated_text"][-1]["content"]
info = {
'message': prompt,
'response': response
}
return self.post_process_fn(info['response']), info