-
Notifications
You must be signed in to change notification settings - Fork 4
/
get_openai_data.py
334 lines (297 loc) · 11.6 KB
/
get_openai_data.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
"""
Retrieve data used for calibration for a given split using an OpenAI model through their API.
This requires the data already generated through run_regression_experiment.py.
"""
# STD
import argparse
import os
import time
import warnings
# EXT
import dill
from httpx import HTTPStatusError
import numpy as np
from openai import OpenAI
from tqdm import tqdm
# PROJECT
from src.constants import (
CALIBRATION_MODEL_IDENTIFIER,
DATASETS,
DATA_DIR,
NUM_IN_CONTEXT_SAMPLES,
MODEL_IDENTIFIER,
OPENAI_MODEL_IDENTIFIER,
DATASET_SPLIT_SIZES,
)
from src.eval import check_answer_correctness
from src.prompts import (
QUAL_VERBALIZED_CONFIDENCE_PROMPT,
QUANT_VERBALIZED_CONFIDENCE_PROMPT,
QA_COT_PROMPT,
)
SECRET_IMPORTED = False
try:
from secret import (
OPENAI_API_KEY,
OPENAI_ORGANIZATION_ID,
)
SECRET_IMPORTED = True
except ImportError:
warnings.warn("secret.py could not be imported.")
def extract_openai_data(
model_identifier: str,
source_data_model_identifier: str,
num_in_context_samples: int,
data_dir: str,
dataset_name: str,
):
"""
Extract calibration data from the OpenAI API.
Parameters
----------
model_identifier: str
Identifier of the OpenAI to use.
source_data_model_identifier: str
Identifier of the model whose dataloader to re-use (this would correspond to a local HF model).
num_in_context_samples: int
Number of in-context samples that were used with the source data model.
data_dir: str
Path to data directory. This is not the exact path to the dataloaders but rather the parent directory.
dataset_name: str
Name of the dataset to work on.
"""
source_data_dir = os.path.join(
data_dir,
dataset_name,
source_data_model_identifier.replace("/", "_"),
"calibration_data",
f"in_context_{num_in_context_samples}",
)
calibration_data_dir = os.path.join(
data_dir,
dataset_name,
model_identifier.replace("/", "_"),
"calibration_data",
f"in_context_{num_in_context_samples}",
)
if not os.path.exists(calibration_data_dir):
os.makedirs(calibration_data_dir)
split_names = list(DATASET_SPLIT_SIZES[dataset_name].keys())
# Load data
if any(
[
not os.path.exists(
os.path.join(source_data_dir, f"calibration_data_{split}.dill")
)
for split in split_names
]
):
raise FileNotFoundError(
"Some of the necessary files have not been found. Please execute run_regression_experiment.py first."
)
else:
split_calibration_data = {}
for split in split_names:
with open(
os.path.join(source_data_dir, f"calibration_data_{split}.dill"), "rb"
) as calibration_file:
split_calibration_data[split] = dill.load(calibration_file)
client = OpenAI(organization=OPENAI_ORGANIZATION_ID, api_key=OPENAI_API_KEY)
for split in split_names:
open_ai_calibration_data = {}
try:
calibration_split_path = os.path.join(
calibration_data_dir, f"calibration_data_{split}.dill"
)
if os.path.exists(calibration_split_path):
print(f"Found existing data for {split} split, skipping.")
continue
calibration_data = split_calibration_data[split]
del calibration_data["included_questions"]
for question_id, question_data in tqdm(
calibration_data.items(), total=len(calibration_data)
):
# Copy over data that is the same between models
question = question_data["question"]
question_in_context = question_data["question_in_context"]
gold_answer = question_data["gold_answer"]
open_ai_question_data = {
"question": question,
"question_in_context": question_in_context,
"gold_answer": gold_answer,
"question_embedding": question_data["question_embedding"],
}
# Get normal model answer
answer_completion = client.chat.completions.create(
model=model_identifier,
messages=[{"role": "user", "content": question_in_context}],
logprobs=True,
)
answer = answer_completion.choices[0].message.content
answer_likelihood = np.exp(
np.mean(
[
lp.logprob
for lp in answer_completion.choices[0].logprobs.content
]
)
)
# Ask for verbalized uncertainty
qual_uncertainty = (
client.chat.completions.create(
model=model_identifier,
messages=[
{"role": "user", "content": question},
{"role": "assistant", "content": answer},
{
"role": "user",
"content": QUAL_VERBALIZED_CONFIDENCE_PROMPT,
},
],
max_tokens=10,
)
.choices[0]
.message.content
)
quant_uncertainty = (
client.chat.completions.create(
model=model_identifier,
messages=[
{"role": "user", "content": question},
{"role": "assistant", "content": answer},
{
"role": "user",
"content": QUANT_VERBALIZED_CONFIDENCE_PROMPT,
},
],
max_tokens=10,
)
.choices[0]
.message.content
)
# Get model answer with Chain-of-though prompting
cot_answer_completion = client.chat.completions.create(
model=model_identifier,
messages=[
{"role": "system", "content": QA_COT_PROMPT},
{"role": "user", "content": question},
],
logprobs=True,
)
cot_answer = cot_answer_completion.choices[0].message.content
cot_answer_likelihood = np.exp(
np.mean(
[
lp.logprob
for lp in cot_answer_completion.choices[0].logprobs.content
]
)
)
# Ask for verbalized uncertainty
cot_qual_uncertainty = (
client.chat.completions.create(
model=model_identifier,
messages=[
{"role": "user", "content": question},
{"role": "assistant", "content": cot_answer},
{
"role": "user",
"content": QUAL_VERBALIZED_CONFIDENCE_PROMPT,
},
],
max_tokens=10,
)
.choices[0]
.message.content
)
cot_quant_uncertainty = (
client.chat.completions.create(
model=model_identifier,
messages=[
{"role": "user", "content": question},
{"role": "assistant", "content": cot_answer},
{
"role": "user",
"content": QUANT_VERBALIZED_CONFIDENCE_PROMPT,
},
],
max_tokens=10,
)
.choices[0]
.message.content
)
# Check correctness
answer_correctness, cot_answer_correctness = check_answer_correctness(
correct_answers=[gold_answer] * 2,
model_answers=[answer, cot_answer],
)
open_ai_question_data.update(
{
"answer": answer,
"seq_likelihood": answer_likelihood,
"verbalized_qual": qual_uncertainty,
"verbalized_quant": quant_uncertainty,
"accuracy": int(answer_correctness),
"cot_answer": cot_answer,
"cot_accuracy": int(cot_answer_correctness),
"cot_seq_likelihood": cot_answer_likelihood,
"verbalized_cot_qual": cot_qual_uncertainty,
"verbalized_cot_quant": cot_quant_uncertainty,
}
)
# Make sure we have all the required fields
assert question_data.keys() == open_ai_question_data.keys(), (
f"Some of the fields are missing for this question: "
f"{', '.join(list(set(question_data.keys()) - set(open_ai_question_data.keys())))}"
)
# Add to new dataset
open_ai_calibration_data[question_id] = open_ai_question_data
# Introduce a sleep phase here to avoid OpenAI rate limits
time.sleep(0.1)
except HTTPStatusError:
print("API rate limit exceeded, dumping partial results.")
calibration_split_path = os.path.join(
calibration_data_dir, f"calibration_data_{split}_partial.dill"
)
finally:
if len(open_ai_calibration_data) > 0:
# Save the calibration data
with open(calibration_split_path, "wb") as calibration_file:
dill.dump(open_ai_calibration_data, calibration_file)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--model-identifier",
type=str,
default=OPENAI_MODEL_IDENTIFIER,
help="OpenAI identifier for model.",
)
parser.add_argument(
"--calibration-model-identifier",
type=str,
default=CALIBRATION_MODEL_IDENTIFIER,
help="Identifier of the Huggingface model used for calibration purposes.",
)
parser.add_argument(
"--source-data-model-identifier",
type=str,
default=MODEL_IDENTIFIER,
help="Identifier of the Huggingface model for which the data was originally preprocessed for.",
)
parser.add_argument(
"--dataset-name", type=str, help="Name of the dataset.", choices=DATASETS
)
parser.add_argument(
"--num-in-context-samples", type=int, default=NUM_IN_CONTEXT_SAMPLES
)
parser.add_argument(
"--data-dir", type=str, default=DATA_DIR, help="Directory containing data."
)
args = parser.parse_args()
extract_openai_data(
model_identifier=args.model_identifier,
source_data_model_identifier=args.source_data_model_identifier,
dataset_name=args.dataset_name,
num_in_context_samples=args.num_in_context_samples,
data_dir=args.data_dir,
)