-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_eval_openai_batch.py
182 lines (147 loc) · 5.37 KB
/
run_eval_openai_batch.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
import json
import os
import random
from argparse import ArgumentParser
from openai import OpenAI
from utils.data_utils import get_load_func, get_save_func
from utils.prompts import (
ABS_SYSTEM_PROMPT,
ABS_USER_PROMPT_TEMPLATE,
SAMPLING_PARAMS_OPENAI,
)
class BatchClient:
def __init__(self, api_key=None):
api_key = api_key or os.getenv("OPENAI_API_KEY")
self.client = OpenAI(api_key=api_key)
def create_batch(self, input_file, description=None):
batch_input_file = self.client.files.create(
file=open(input_file, "rb"), purpose="batch"
)
batch_input_file_id = batch_input_file.id
batch_obj = self.client.batches.create(
input_file_id=batch_input_file_id,
endpoint="/v1/chat/completions",
completion_window="24h",
metadata={"description": description},
)
return batch_obj
def check_batch(self, batch_input_file_id):
batch_obj = self.client.batches.retrieve(batch_input_file_id)
return batch_obj
def retrieve_batch(self, batch_input_file_id):
content = self.client.files.content(batch_input_file_id)
return content
def cancel_batch(self, batch_input_file_id):
self.client.batches.cancel(batch_input_file_id)
def list_batches(self):
return self.client.batches.list()
def prepare_inputs(
records,
eval_model_name: str,
system_key=None,
user_key="user_prompt",
answer_key="reference_answer",
rubric_key="rubrics",
):
inputs = []
for record in records:
id = record["id"]
response = record["response"]
if system_key and system_key in record:
instruction = f"{record[system_key]}\n{record[user_key]}"
else:
instruction = record[user_key]
reference_answer = record[answer_key]
rubrics = record[rubric_key]
for i, rubric in enumerate(rubrics):
rubric_str = json.dumps(rubric, indent=4)
content = ABS_USER_PROMPT_TEMPLATE.format(
instruction=instruction,
response=response,
reference_answer=reference_answer,
score_rubric=rubric_str,
).strip()
messages = [
{"role": "system", "content": ABS_SYSTEM_PROMPT},
{"role": "user", "content": content},
]
inputs.append(
{
"custom_id": f"{id}_{i}",
"method": "POST",
"url": "/v1/chat/completions",
"body": {
"model": eval_model_name,
"messages": messages,
**SAMPLING_PARAMS_OPENAI,
},
}
)
random_inputs = random.sample(inputs, 3)
width = 20
for input_str in random_inputs:
print("-" * width)
print("Example inputs:")
print(input_str)
print("-" * width)
return inputs
def prepare_input_file(input_file, response_file, eval_model_name):
load_func_i = get_load_func(input_file)
data_dict = {d["id"]: d for d in load_func_i(input_file)}
data_list = list(data_dict.values())
with open(response_file, "r") as f:
responses = json.load(f)
for id, response_record in responses.items():
if id not in data_dict.keys():
assert 0
data_dict[id].update({"response": response_record["response"]})
data_list = list(data_dict.values())
inputs = prepare_inputs(data_list, eval_model_name)
batch_eval_input_file = response_file.replace(
"_responses.json", "_responses_batch_eval_input.jsonl"
)
save_func = get_save_func(batch_eval_input_file)
save_func(inputs, batch_eval_input_file)
return batch_eval_input_file
def main(args):
client = BatchClient()
if args.mode == "create":
batch_eval_input_file = prepare_input_file(
args.input_file, args.response_file, args.model
)
batch_obj = client.create_batch(batch_eval_input_file, args.description)
print(batch_obj)
elif args.mode == "check":
batch_obj = client.check_batch(args.batch_id)
print(batch_obj)
elif args.mode == "retrieve":
content = client.retrieve_batch(args.batch_output_file_id)
with open(args.output_file, "w") as f:
for line in content.iter_lines():
f.write(line + "\n")
elif args.mode == "cancel":
client.cancel_batch(args.batch_id)
elif args.mode == "list":
batches = client.list_batches()
print(batches)
else:
raise ValueError("Invalid mode")
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("-m", "--model", type=str)
parser.add_argument("-i", "--input_file", type=str, help="MPA test data")
parser.add_argument("-r", "--response_file", type=str)
parser.add_argument("-d", "--description", type=str)
parser.add_argument(
"-o",
"--output_file",
type=str,
help="Output .jsonl file to write the retrieved content",
)
parser.add_argument("-b", "--batch_id", type=str)
parser.add_argument("--batch_output_file_id", type=str)
parser.add_argument(
"--mode", type=str, choices=["create", "check", "retrieve", "cancel", "list"]
)
args = parser.parse_args()
main(args)