-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbatch_evaluate.py
More file actions
155 lines (133 loc) · 5.33 KB
/
batch_evaluate.py
File metadata and controls
155 lines (133 loc) · 5.33 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
152
153
154
155
import copy
import os
import time
import json
import argparse
from tqdm import tqdm
import requests
from openai import OpenAI
from prompt import SCORER_TEMPLATE
from utils import try_parse_llm_score
def get_chat_response(client, model_version, content, max_tokens, retries=5):
messages = [
{
"role": "system",
"content": "You are a helpful and precise assistant for checking the correctness of the answer.",
},
{"role": "user", "content": content},
]
payload = {
"model": model_version,
"messages": messages,
"temperature": 0.0,
"max_tokens": max_tokens,
}
for attempt in range(retries):
try:
response = client.chat.completions.create(**payload)
content = response.choices[0].message.content.strip()
return content
except requests.exceptions.RequestException as e:
print(f"Request failed on attempt {attempt + 1}: {e}")
time.sleep(5.0)
if attempt == retries - 1:
print(f"Failed to get response after {retries} attempts")
return ""
except Exception as e:
print(f"Error on attempt {attempt + 1}: {e}")
return ""
if __name__ == '__main__':
# parse argments (input file and output file)
parser = argparse.ArgumentParser(description='Evaluate model performance.')
parser.add_argument('--base_url', type=str, required=False, default='')
parser.add_argument('--api_key', type=str, required=False, default='')
parser.add_argument('--model_version', type=str, required=False, default='o3')
args = parser.parse_args()
client = OpenAI(base_url=args.base_url, api_key=args.api_key)
# read instruct.json
instruct_file = './data/data-20260121.json'
_veriGUI = json.load(open(instruct_file, 'r', encoding='utf-8'))
veriGUI = {}
for item in _veriGUI:
name = item['name']
instruction = item['instruction']
answer = item['answer']
veriGUI[name] = {
"name": name,
"instruction": instruction,
"answer": answer,
"type": item['type'],
}
# iterate all json files in the 'predictions' directory
for filename in os.listdir('./predictions'):
if not (filename.endswith(".json") or filename.endswith(".jsonl")):
continue
input_file = os.path.join('./predictions', filename)
output_file = os.path.join('./evaluated', filename.replace("jsonl", "json"))
if os.path.exists(output_file):
continue
print(f"Evaluating {input_file}, output will be saved to {output_file}")
if input_file.endswith(".json"):
with open(input_file, 'r') as file:
data = json.load(file)
elif input_file.endswith(".jsonl"):
data = []
with open(input_file, 'r') as file:
for line in file:
data.append(json.loads(line.strip()))
else:
raise ValueError(f"Unsupported file format: {input_file}")
formatted_data = {}
for datum in data:
if "name" in datum:
name = datum["name"]
elif "folder" in datum:
name = datum["folder"]
else:
raise ValueError(f"name not found")
if "prediction" in datum:
prediction = datum["prediction"]
elif "result" in datum:
prediction = datum["result"]
elif "answer" in datum:
prediction = datum["answer"]
elif "model_output" in datum:
prediction = datum["model_output"]
else:
print("No prediction found for", name)
prediction = ""
if "nsteps" in datum:
n_steps = datum["nsteps"]
elif "executor_trace" in datum:
n_steps = len(datum.get("executor_trace", []))
elif "tool_call_count" in datum:
n_steps = int(datum.get("tool_call_count", 0))
else:
n_steps = 0
if prediction is None or prediction == "[executor reach max turns]":
prediction = ""
n_steps = 0
formatted_data[name] = {
"prediction": prediction,
"nsteps": n_steps,
}
output = []
for vname, vdata in tqdm(veriGUI.items()):
_vdata = copy.deepcopy(vdata)
if vname not in formatted_data:
_vdata["score"] = 0
_vdata["nsteps"] = 0
else:
question = _vdata["instruction"]
answer = _vdata["answer"]
prediction = formatted_data[vname]["prediction"]
nsteps = formatted_data[vname]["nsteps"]
judge_prompt = SCORER_TEMPLATE.format(question=question, answer=answer, pred=prediction)
score = get_chat_response(client, args.model_version, judge_prompt, max_tokens=1024 * 32)
score = try_parse_llm_score(score)
_vdata["prediction"] = prediction
_vdata["score"] = score
_vdata["nsteps"] = nsteps
output.append(_vdata)
with open(output_file, 'w') as file:
json.dump(output, file, indent=4, ensure_ascii=False)