-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbenchmark.py
More file actions
151 lines (122 loc) · 5.67 KB
/
Copy pathbenchmark.py
File metadata and controls
151 lines (122 loc) · 5.67 KB
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
import argparse
from base_types import *
import json
import grader
import serialization
import querier
import sys
import os
import validation
import datetime
def load_problems(base_path):
return serialization.get_problems(base_path)
def validate_problems(base_path):
validation_results = {}
problemsJSON = serialization.get_problems_json(base_path)
for fileName, json in problemsJSON.items():
validation_results[fileName] = validation.validate_problem_json(json)
print(f'{fileName}: {validation_results[fileName]}')
return validation_results
def generate_solutions(base_path, problem_definitions, models):
solutions = []
for model in models:
for problem_definition in problem_definitions:
inputs = problem_definition.get_llm_problem_inputs()
for problem_input in inputs:
solution = model.generate_solution(problem_input)
solutions.append(solution)
serialization.save_solution(base_path, solution)
return solutions
def load_solutions(base_path, models):
solutions = []
for model in models:
solutions += serialization.load_solutions(base_path, model.model_identifier)
return solutions
def grade_solutions(base_path, problem_definitions, models, graders, current_report_paths):
gradingOutputs = []
for grader in graders:
if not grader.can_grade(problem_definitions):
continue
for model in models:
print(f'Grading solutions for {base_path} from model {model.model_identifier} with grader {grader.identifier}')
solutions = serialization.get_solutions(base_path, model.model_identifier)
grades = grader.grade(problem_definitions, solutions)
current_report_path = current_report_paths[model]
serialization.save_grades(base_path, grades, current_report_path)
gradingOutputs.append(grades)
print(gradingOutputs)
return gradingOutputs
def load_grades(base_path, models, graders):
gradingOutputs = []
for grader in graders:
for model in models:
gradingOutputs.append(serialization.get_grades(base_path, model.model_identifier, grader.identifier))
return gradingOutputs
def print_header(text, symbol='#'):
# Convert text to uppercase
text = text.upper()
# Get the length of the text, plus 4 to account for spaces and surrounding symbols
length = len(text) + 4
# Create the decorative line
decoration = symbol * length
# Combine everything together
result = f"{decoration}\n{symbol} {text} {symbol}\n{decoration}"
print(f'\n{result}\n')
def main():
parser = argparse.ArgumentParser(description="Run specified phases of the grading process.")
parser.add_argument('--base_path', nargs='*', default=None, help="The base path(s) for data files. If this arg is not set, run all problem sets in ./problem_sets")
parser.add_argument('--validate', action='store_true', help="Validate the problem definition JSON.")
parser.add_argument('--generate', action='store_true', help="Generate solutions for problems.")
parser.add_argument('--grade', action='store_true', help="Grade the generated solutions.")
parser.add_argument('--model', required='--generate' in sys.argv or '--grade' in sys.argv, nargs='+', help=f"The model(s) to use for generating solutions The following model names can be queried through the OpenAI API: {querier.OpenAIModelQuerier.supported_model_names()}")
parser.add_argument('--grader', required='--grade' in sys.argv, nargs='+', help=f"The grader(s) to use for grading solutions. Valid graders: {grader.Grader.all_graders()}")
parser.add_argument('--force-human', action='store_true', help="Always use the interactive human model querier.")
parser.add_argument('--report-path', default=None, help="Location in which to store reports generated during each run. Default= ./reports")
args = parser.parse_args()
problem_definitions = []
if args.model:
models = querier.AIModelQuerier.resolve_queriers(args.model, args.force_human)
if args.grader:
graders = grader.Grader.resolve_graders(args.grader)
if args.base_path is None:
args.base_path = [os.path.join('problem_sets', d) for d in os.listdir('problem_sets') if os.path.isdir(os.path.join('problem_sets', d))]
if args.report_path is None:
args.report_path = 'reports'
if args.validate:
print_header('Validation')
print("Validating problems…")
all_validation_results = {x: validate_problems(x) for x in args.base_path}
print("Validation results:")
for base_path, validation_results in all_validation_results.items():
print(f"{base_path}:")
for fileName, validation_result in validation_results.items():
print(f"\t{fileName}: {validation_result}")
if args.generate or args.grade:
# generate timestamp to identify final report:
timestamp = datetime.datetime.now().strftime("%m-%d-%Y--%H-%M-%S")
current_report_paths = {m: os.path.join(args.report_path, "report-" + m.model_identifier + "-" + timestamp + ".json") for m in models}
print_header('Problems')
print("Loading problems…")
problem_sets = {x: load_problems(x) for x in args.base_path}
# Run benchmarks on all problem sets sequentially
for base_path, problem_definitions in problem_sets.items():
print(f"\n***\n*** Problem set {base_path}\n***\n")
for problem_definition in problem_definitions:
print(problem_definition)
print()
if args.generate:
print_header('Generation')
print("Generating solutions…")
solutions = generate_solutions(base_path, problem_definitions, models)
print(solutions)
if args.grade:
print_header('Grading')
print("Grading solutions…")
grading_outputs = grade_solutions(base_path, problem_definitions, models, graders, current_report_paths)
for output in grading_outputs:
print(output.str_including_solutions())
print()
for output in grading_outputs:
print(output)
if __name__ == "__main__":
main()