-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_openai.py
304 lines (265 loc) · 9.82 KB
/
generate_openai.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
import json
import logging
import time
from dataclasses import dataclass, field
from logging.config import fileConfig
from pathlib import Path
from typing import Literal
import pandas as pd
import simple_parsing
import tiktoken
from openai import OpenAI
from tqdm import tqdm
from tqdm.contrib.logging import logging_redirect_tqdm
from peerqa.data_loader import PaperLoader, QuestionLoader
from peerqa.generate_utils import make_prompts_full_text, make_prompts_rag
from peerqa.prompts import PROMPTS
fileConfig("logging.ini")
logger = logging.getLogger(__name__)
logging.getLogger("httpcore").setLevel(logging.CRITICAL)
@dataclass
class Args:
openai_api_key: str
model: Literal[
"gpt-35-turbo-0613-16k",
"gpt-4o-2024-08-06",
"gpt-4o-mini-2024-07-18",
] = "gpt-4o-2024-08-06"
estimate_only: bool = False
retrieval_file: Path = field(
default=Path("out/run-paragraphs-naver_splade-v3-unsanswerable-dot.json")
)
output_dir: Path = field(default=Path("out"))
qa_file: Path = field(default=Path("data/qa.jsonl"))
papers_file: Path = field(default=Path("data/papers.jsonl"))
prompt_selection: Literal[
"answerability-full-text",
"answerability-rag",
"full-text",
"rag",
] = "rag"
prompt_template: str = None
sort_by: Literal["score", "document"] = "score"
context_setting: int | str = None
def __post_init__(self):
if "full-text" in self.prompt_selection:
if self.context_setting is None:
self.context_setting = "full-text"
if self.context_setting != "full-text":
raise ValueError(
f"Invalid context_setting: {self.context_setting}. Must be 'full-text' for full-text prompts."
)
if "rag" in self.prompt_selection:
if self.context_setting is None:
raise ValueError(
f"Invalid context_setting: {self.context_setting}. Must be an integer or 'gold' for RAG prompts."
)
if (
not isinstance(self.context_setting, int)
and self.context_setting != "gold"
):
raise ValueError(
f"Invalid context_setting: {self.context_setting}. Must be an integer or 'gold' for RAG prompts."
)
MODEL_TOKEN_COST = {
"gpt-35-turbo-0613-16k": {
"input_token": 0.003 / 1_000,
"output_token": 0.004 / 1_000,
},
"gpt-4o-2024-08-06": {
"input_token": 0.0025 / 1_000,
"output_token": 0.01 / 1_000,
},
"gpt-4o-mini-2024-07-18": {
"input_token": 0.00015 / 1_000,
"output_token": 0.0006 / 1_000,
},
}
def openai_generate(client, model, inputs, ids):
def complete(messages):
completion = client.chat.completions.create(
model=model,
messages=messages,
temperature=0,
top_p=1,
)
return completion
total_cost = 0
completions = []
with tqdm(total=len(inputs)) as pbar:
for messages in inputs:
retries = 0
while True:
try:
completion = complete(messages)
break
except Exception as e:
logger.error(e)
retries += 1
logger.info(f"Retrying... {retries}")
time.sleep(3 * retries)
retries = 0
completions.append(completion)
# calculate cost
input_tokens = completion.usage.prompt_tokens
output_tokens = completion.usage.completion_tokens
cost = (
input_tokens * MODEL_TOKEN_COST[model]["input_token"]
+ output_tokens * MODEL_TOKEN_COST[model]["output_token"]
)
total_cost += cost
pbar.update(1)
pbar.set_description(desc=f"Cost: {total_cost:.4f}$")
time.sleep(0.5)
return completions
def main(args: Args):
# load the data
qa_loader = QuestionLoader(args.qa_file)
paper_loader = PaperLoader(args.papers_file)
logger.info(f"Loaded data from {args.qa_file} and {args.papers_file}")
if args.model == "gpt-35-turbo-0613-16k":
max_model_len = 16385
elif args.model == "gpt-4o-2024-08-06":
max_model_len = 128_000
elif args.model == "gpt-4o-mini-2024-07-18":
max_model_len = 128_000
else:
raise ValueError(args.model)
# create prompts
prompt_template = (
PROMPTS[args.prompt_selection]
if args.prompt_template is None
else args.prompt_template
)
logger.info(f"Using prompt template: {prompt_template}")
tokenizer = tiktoken.encoding_for_model(args.model)
if "full-text" in args.prompt_selection:
logger.info("Creating full-text prompts")
inputs, ids = make_prompts_full_text(
tokenizer=tokenizer,
prompt_template=prompt_template,
max_model_len=max_model_len,
model=args.model,
qa_loader=qa_loader,
paper_loader=paper_loader,
apply_vllm_chat_template=False,
)
elif "rag" in args.prompt_selection:
# load and process the retrieval results (or gold paragraphs)
if args.context_setting == "gold":
retrieval_file = "out/qrels.paragraphs.json"
elif isinstance(args.context_setting, int):
retrieval_file = args.retrieval_file
else:
raise ValueError(args.context_setting)
logger.info(f"Loading retrieval results from {retrieval_file}")
with open(retrieval_file) as fh:
run = json.load(fh)
df_run = []
for question_id, document_id_to_score in run.items():
for document_id, score in document_id_to_score.items():
df_run.append(
{
"question_id": question_id,
"document_id": document_id,
"score": score,
}
)
df_run = pd.DataFrame(df_run)
logger.info("Creating RAG prompts")
inputs, ids = make_prompts_rag(
tokenizer=tokenizer,
prompt_template=prompt_template,
max_model_len=max_model_len,
context_setting=args.context_setting,
model=args.model,
sort_by=args.sort_by,
df_run=df_run,
qa_loader=qa_loader,
paper_loader=paper_loader,
apply_vllm_chat_template=False,
)
else:
raise ValueError(args.prompt_selection)
# run model inference on the prompts
exp = f"{args.model}-{max_model_len//1000}k-{args.prompt_selection}"
if "rag" in args.prompt_selection:
exp += f"-{args.context_setting}"
if "full-text" in args.prompt_selection:
logger.info("Creating full-text prompts")
inputs, ids = make_prompts_full_text(
tokenizer=tokenizer,
prompt_template=prompt_template,
max_model_len=max_model_len,
model=args.model,
qa_loader=qa_loader,
paper_loader=paper_loader,
apply_vllm_chat_template=False,
)
elif "rag" in args.prompt_selection:
# load and process the retrieval results (or gold paragraphs)
if args.context_setting == "gold":
retrieval_file = "out/qrels.paragraphs.json"
elif isinstance(args.context_setting, int):
retrieval_file = args.retrieval_file
else:
raise ValueError(args.context_setting)
logger.info(f"Loading retrieval results from {retrieval_file}")
with open(retrieval_file) as fh:
run = json.load(fh)
df_run = []
for question_id, document_id_to_score in run.items():
for document_id, score in document_id_to_score.items():
df_run.append(
{
"question_id": question_id,
"document_id": document_id,
"score": score,
}
)
df_run = pd.DataFrame(df_run)
logger.info("Creating RAG prompts")
inputs, ids = make_prompts_rag(
tokenizer=tokenizer,
prompt_template=prompt_template,
max_model_len=max_model_len,
context_setting=args.context_setting,
model=args.model,
sort_by=args.sort_by,
df_run=df_run,
qa_loader=qa_loader,
paper_loader=paper_loader,
apply_vllm_chat_template=False,
)
else:
raise ValueError(args.prompt_selection)
# estimate costs
input_tokens = 0
for i in inputs:
for message in i:
input_tokens += len(tokenizer.encode(message["content"]))
logger.info(f"Input tokens: {input_tokens}")
estimated_average_output_tokens = 256
estimated_cost = (
input_tokens * MODEL_TOKEN_COST[args.model]["input_token"]
+ estimated_average_output_tokens * MODEL_TOKEN_COST[args.model]["output_token"]
)
logger.info(f"Estimated costs: {estimated_cost:.4f}$")
if args.estimate_only:
return
client = OpenAI(
api_key=args.openai_api_key,
)
completions = openai_generate(client, args.model, inputs, ids)
generations = []
for _id, completion in zip(ids, completions):
generations.append({**_id, "generation": completion.choices[0].message.content})
out_file = f"{str(args.output_dir)}/generations-{exp}.jsonl"
logger.info(f"Writing generations to {out_file}")
df_generations = pd.DataFrame(generations)
df_generations.to_json(out_file, orient="records", lines=True)
if __name__ == "__main__":
args, _ = simple_parsing.parse_known_args(Args)
with logging_redirect_tqdm():
logger.info(args)
main(args)