-
Notifications
You must be signed in to change notification settings - Fork 358
/
eval_mt_bench.py
326 lines (296 loc) · 9.48 KB
/
eval_mt_bench.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
"""
Usage:
python gen_judgment.py --model-list [LIST-OF-MODEL-ID] --parallel [num-concurrent-api-call] --mode [single|pairwise-baseline|pairwise-all]
"""
import argparse
from concurrent.futures import ThreadPoolExecutor
import json
import numpy as np
from tqdm import tqdm
from fastchat.llm_judge.common import (
load_questions,
load_model_answers,
load_judge_prompts,
check_data,
play_a_match_pair,
play_a_match_single,
get_model_list,
Judge,
MatchPair,
MatchSingle,
NEED_REF_CATS,
)
def make_match(
questions,
models,
model_answers,
judge,
baseline_model,
ref_answers=None,
multi_turn=False,
):
matches = []
for q in questions:
if multi_turn and len(q["turns"]) != 2:
continue
for i in range(len(models)):
q_id = q["question_id"]
m_1 = models[i]
m_2 = baseline_model
if m_1 == m_2:
continue
a_1 = model_answers[m_1][q_id]
print(model_answers.keys())
a_2 = model_answers[baseline_model][q_id]
if ref_answers is not None:
ref = ref_answers[judge.model_name][q_id]
match = MatchPair(
dict(q),
m_1,
m_2,
a_1,
a_2,
judge,
ref_answer=ref,
multi_turn=multi_turn,
)
else:
match = MatchPair(
dict(q), m_1, m_2, a_1, a_2, judge, multi_turn=multi_turn
)
matches.append(match)
return matches
def make_match_all_pairs(
questions,
models,
model_answers,
judge,
baseline_model=None,
ref_answers=None,
multi_turn=False,
):
matches = []
for q in questions:
if multi_turn and len(q["turns"]) != 2:
continue
for i in range(len(models)):
for j in range(i + 1, len(models)):
q_id = q["question_id"]
m_1 = models[i]
m_2 = models[j]
a_1 = model_answers[m_1][q_id]
a_2 = model_answers[m_2][q_id]
if ref_answers is not None:
ref = ref_answers[judge.model_name][q_id]
match = MatchPair(
dict(q),
m_1,
m_2,
a_1,
a_2,
judge,
ref_answer=ref,
multi_turn=multi_turn,
)
else:
match = MatchPair(
dict(q), m_1, m_2, a_1, a_2, judge, multi_turn=multi_turn
)
matches.append(match)
return matches
def make_match_single(
questions,
models,
model_answers,
judge,
baseline_model=None,
ref_answers=None,
multi_turn=False,
):
matches = []
for q in questions:
if multi_turn and len(q["turns"]) != 2:
continue
for i in range(len(models)):
q_id = q["question_id"]
m = models[i]
a = model_answers[m][q_id]
if ref_answers is not None:
ref = ref_answers[judge.model_name][q_id]
matches.append(
MatchSingle(
dict(q), m, a, judge, ref_answer=ref, multi_turn=multi_turn
)
)
else:
matches.append(MatchSingle(dict(q), m, a, judge, multi_turn=multi_turn))
return matches
def make_judge_pairwise(judge_model, judge_prompts):
judges = {}
judges["default"] = Judge(judge_model, judge_prompts["pair-v2"])
judges["math"] = Judge(judge_model, judge_prompts["pair-math-v1"], ref_based=True)
judges["default-mt"] = Judge(
judge_model, judge_prompts["pair-v2-multi-turn"], multi_turn=True
)
judges["math-mt"] = Judge(
judge_model,
judge_prompts["pair-math-v1-multi-turn"],
ref_based=True,
multi_turn=True,
)
return judges
def make_judge_single(judge_model, judge_prompts):
judges = {}
judges["default"] = Judge(judge_model, judge_prompts["single-v1"])
judges["math"] = Judge(judge_model, judge_prompts["single-math-v1"], ref_based=True)
judges["default-mt"] = Judge(
judge_model, judge_prompts["single-v1-multi-turn"], multi_turn=True
)
judges["math-mt"] = Judge(
judge_model,
judge_prompts["single-math-v1-multi-turn"],
ref_based=True,
multi_turn=True,
)
return judges
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--bench-name",
type=str,
default="mt_bench",
help="The name of the benchmark question set.",
)
parser.add_argument(
"--judge-file",
type=str,
default="FastChat/fastchat/llm_judge/data/judge_prompts.jsonl",
help="The file of judge prompts.",
)
parser.add_argument("--judge-model", type=str, default="gpt-4")
parser.add_argument("--baseline-model", type=str, default="gpt-4")
parser.add_argument(
"--mode",
type=str,
default="single",
choices=["pairwise-baseline", "pairwise-all", "single"],
help=(
"Evaluation mode. "
"`pairwise-baseline` runs pairwise comparision against a baseline. "
"`pairwise-all` runs pairwise comparision between all pairs. "
"`single` runs single answer grading."
),
)
parser.add_argument(
"--model-list",
type=str,
nargs="+",
default=None,
help="A list of models to be evaluated",
)
parser.add_argument(
"--parallel", type=int, default=1, help="The number of concurrent API calls."
)
parser.add_argument(
"--first-n", type=int, help="A debug option. Only run the first `n` judgments."
)
args = parser.parse_args()
question_file = f"FastChat/fastchat/llm_judge/data/{args.bench_name}/question.jsonl"
answer_dir = f"outputs/mt_bench"
ref_answer_dir = (
f"FastChat/fastchat/llm_judge/data/{args.bench_name}/reference_answer"
)
# Load questions
questions = load_questions(question_file, None, None)
# Load answers
model_answers = load_model_answers(answer_dir)
ref_answers = load_model_answers(ref_answer_dir)
# Load judge
judge_prompts = load_judge_prompts(args.judge_file)
if args.first_n:
questions = questions[: args.first_n]
if args.model_list is None:
models = get_model_list(answer_dir)
else:
models = args.model_list
if args.mode == "single":
judges = make_judge_single(args.judge_model, judge_prompts)
play_a_match_func = play_a_match_single
output_file = (
f"outputs/{args.bench_name}/model_judgment/{args.judge_model}_single.jsonl"
)
make_match_func = make_match_single
baseline_model = None
else:
judges = make_judge_pairwise(args.judge_model, judge_prompts)
play_a_match_func = play_a_match_pair
output_file = (
f"outputs/{args.bench_name}/model_judgment/{args.judge_model}_pair.jsonl"
)
if args.mode == "pairwise-all":
make_match_func = make_match_all_pairs
baseline_model = None
else:
make_match_func = make_match
baseline_model = args.baseline_model
check_data(questions, model_answers, ref_answers, models, judges)
question_math = [q for q in questions if q["category"] in NEED_REF_CATS]
question_default = [q for q in questions if q["category"] not in NEED_REF_CATS]
# Make matches
matches = []
matches += make_match_func(
question_default, models, model_answers, judges["default"], baseline_model
)
matches += make_match_func(
question_math,
models,
model_answers,
judges["math"],
baseline_model,
ref_answers,
)
matches += make_match_func(
question_default,
models,
model_answers,
judges["default-mt"],
baseline_model,
multi_turn=True,
)
matches += make_match_func(
question_math,
models,
model_answers,
judges["math-mt"],
baseline_model,
ref_answers,
multi_turn=True,
)
match_stat = {}
match_stat["bench_name"] = args.bench_name
match_stat["mode"] = args.mode
match_stat["judge"] = args.judge_model
match_stat["baseline"] = baseline_model
match_stat["model_list"] = models
match_stat["total_num_questions"] = len(questions)
match_stat["total_num_matches"] = len(matches)
match_stat["output_path"] = output_file
# Show match stats and prompt enter to continue
print("Stats:")
print(json.dumps(match_stat, indent=4))
# Skip confirmation
# input("Press Enter to confirm...")
# Play matches
if args.parallel == 1:
for match in tqdm(matches):
play_a_match_func(match, output_file=output_file)
else:
def play_a_match_wrapper(match):
play_a_match_func(match, output_file=output_file)
np.random.seed(0)
np.random.shuffle(matches)
with ThreadPoolExecutor(args.parallel) as executor:
for match in tqdm(
executor.map(play_a_match_wrapper, matches), total=len(matches)
):
pass