-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.py
259 lines (222 loc) · 10.2 KB
/
generator.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
from abc import ABC, abstractmethod
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
import torch
from openai import OpenAI
import os
from mistralai import Mistral
import time
import json
from pydantic import BaseModel
class Boolean_Output(BaseModel):
boolean_query: str
class Generator(ABC):
def __init__(self, model_name=None, max_new_tokens=2048, max_length=None, quantization=None, **kwargs):
if not model_name:
raise ValueError("A model_name must be provided.")
self.max_length = max_length
self.model_name = model_name
self.quantization = quantization
self.max_new_tokens = max_new_tokens
self.json_output = kwargs.get('json_output', False)
@abstractmethod
def generate_batch(self, batched_instructions):
pass
@abstractmethod
def compile_prompt(self, prompt, existing_prompt_list=None, **kwargs):
pass
import re
class Llama(Generator):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
attn = "sdpa" if "tinyllama" in self.model_name.lower() else "flash_attention_2"
if self.quantization == "int4":
quant_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16
)
self.model = AutoModelForCausalLM.from_pretrained(
self.model_name,
quantization_config=quant_config,
attn_implementation=attn,
torch_dtype=torch.bfloat16,
device_map="auto"
)
else:
self.model = AutoModelForCausalLM.from_pretrained(
self.model_name,
attn_implementation=attn,
torch_dtype=torch.bfloat16,
device_map="auto"
)
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
self.tokenizer.padding_side = "left"
self.tokenizer.pad_token = self.tokenizer.bos_token
self.temperature = kwargs.get('temperature', 1.0)
self.seed = kwargs.get('seed', 42)
def set_temperature(self, temperature):
self.model.generation_config.temperature = temperature
def set_seed(self, seed):
self.model.generation_config.seed = seed
self.seed = seed
def generate_batch(self, batched_instructions, target="boolean_query"):
instructions_tokenized = self.tokenizer(
batched_instructions,
return_tensors="pt",
padding=True,
truncation=True
).to('cuda')
input_len = instructions_tokenized['input_ids'].shape[1]
with torch.no_grad():
generated_responses = self.model.generate(
**instructions_tokenized,
max_new_tokens=self.max_new_tokens,
do_sample=True,
temperature=self.temperature,
#seed = self.seed
)
generated_responses = generated_responses[:, input_len:]
final_generated = self.tokenizer.batch_decode(generated_responses, skip_special_tokens=True)
for i in range(len(final_generated)):
if self.json_output:
try:
final_generated[i] = json.loads(final_generated[i])[target]
except:
final_generated[i] = None
return final_generated
def compile_prompt(self, prompt, existing_prompt_dict=None, **kwargs):
"""
Dynamically fills placeholders found in prompt['user'] using kwargs.
"""
extracted_placeholders = re.findall(r"{(.*?)}", prompt["user"]) # Find all placeholders
replacement_dict = {key: kwargs.get(key, f"Default {key}") for key in extracted_placeholders}
def format_prompt(text, replace_dict):
return text.format(**replace_dict)
# Initialize prompt dictionary if needed
if not existing_prompt_dict or not existing_prompt_dict.get("system"):
existing_prompt_dict = {
"system": [{"role": "system", "content": prompt["system"]}],
"example": [],
"user": []
}
# Process examples
if prompt.get("example"):
for example in prompt["example"]:
example_replacements = {key: example.get(key, f"Default {key}") for key in extracted_placeholders}
existing_prompt_dict["example"].extend([
{"role": "user", "content": format_prompt(prompt["user"], example_replacements)},
{"role": "assistant", "content": example["response"]}
])
# Process user input
existing_prompt_dict["user"].append(
{"role": "user", "content": format_prompt(prompt["user"], replacement_dict)}
)
existing_prompt_list = (
existing_prompt_dict["system"] +
existing_prompt_dict["example"] +
existing_prompt_dict["user"]
)
return existing_prompt_dict, self.tokenizer.apply_chat_template(
existing_prompt_list, add_generation_prompt=True, tokenize=False
)
class APIModel(Generator):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Ensure we have the json_output flag from kwargs
self.json_output = kwargs.get('json_output', False)
if "gpt" in self.model_name or "o3" in self.model_name or "o1" in self.model_name:
api_key = kwargs.get('api_key', os.environ.get('OPENAI_API_KEY'))
self.model = OpenAI(api_key=api_key)
elif "mistral" in self.model_name or "mixtral" in self.model_name:
api_key = kwargs.get('api_key', os.environ.get('MISTRAL_API_KEY'))
self.model = Mistral(api_key=api_key)
self.temperature = kwargs.get('temperature', 1.0)
self.seed = kwargs.get('seed', 42)
def set_temperature(self, temperature):
self.temperature = temperature
def set_seed(self, seed):
self.seed = seed
def generate_batch(self, batched_instructions, target="boolean_query"):
generated_responses = []
for instruction in batched_instructions:
if "gpt" in self.model_name or "o3" in self.model_name or "o1" in self.model_name:
if self.json_output:
completion = self.model.chat.completions.create(
model=self.model_name,
messages=instruction,
temperature=self.temperature,
seed=self.seed,
response_format={"type": "json_object"},
)
try:
response = json.loads(completion.choices[0].message.content)[target]
except:
response = None
else:
completion = self.model.chat.completions.create(
model=self.model_name,
messages=instruction,
temperature=self.temperature,
seed=self.seed
)
response = completion.choices[0].message.content
elif "mistral" in self.model_name or "mixtral" in self.model_name:
if self.json_output:
completion = self.model.chat.complete(
model=self.model_name,
messages=instruction,
temperature=self.temperature,
random_seed=self.seed,
response_format={"type": "json_object"}
)
try:
response = json.loads(completion.choices[0].message.content)[target]
print(response)
except:
response = None
else:
completion = self.model.chat.complete(
model=self.model_name,
messages=instruction,
temperature=self.temperature,
random_seed=self.seed
)
response = completion.choices[0].message.content
time.sleep(0.5)
generated_responses.append(response)
return generated_responses
def compile_prompt(self, prompt, existing_prompt_dict=None, **kwargs):
"""
Dynamically fills placeholders found in prompt['user'] using kwargs.
This version does NOT use tokenization. If json_output is True,
returns the prompt as a JSON-formatted string.
"""
extracted_placeholders = re.findall(r"{(.*?)}", prompt["user"])
replacement_dict = {key: kwargs.get(key, f"Default {key}") for key in extracted_placeholders}
def format_prompt(text, replace_dict):
return text.format(**replace_dict)
if not existing_prompt_dict or not existing_prompt_dict.get("system"):
existing_prompt_dict = {
"system": [{"role": "system", "content": prompt["system"]}],
"example": [],
"user": []
}
if prompt.get("example"):
for example in prompt["example"]:
example_replacements = {key: example.get(key, f"Default {key}") for key in extracted_placeholders}
existing_prompt_dict["example"].extend([
{"role": "user", "content": format_prompt(prompt["user"], example_replacements)},
{"role": "assistant", "content": example["response"]}
])
formatted_user_prompt = format_prompt(prompt["user"], replacement_dict)
# For Mistral/Mixtral, prepend the system message to the user prompt
if "mistral" in self.model_name or "mixtral" in self.model_name:
if "system" in prompt:
formatted_user_prompt = f"{prompt['system']}\n{formatted_user_prompt}"
existing_prompt_dict["user"].append({"role": "user", "content": formatted_user_prompt})
existing_prompt_list = (
existing_prompt_dict["system"] +
existing_prompt_dict["example"] +
existing_prompt_dict["user"]
)
return existing_prompt_dict, existing_prompt_list