forked from ShengranHu/ADAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluation_DROP.py
234 lines (198 loc) · 8.71 KB
/
evaluation_DROP.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
import os
from collections import OrderedDict, namedtuple
from typing import Union
import numpy as np
import json
import argparse
import copy
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
from tqdm import tqdm
import pandas
import random
import re
import openai
import backoff
client = openai.OpenAI()
from DROP_utils import random_id, bootstrap_confidence_interval, load_drop, drop_metric
Info = namedtuple('Info', ['name', 'author', 'content', 'iteration_idx'])
FORMAT_INST = lambda request_keys: f"""Reply EXACTLY with the following JSON format.\n{str(request_keys)}\nDO NOT MISS ANY REQUEST FIELDS and ensure that your response is a well-formed JSON object!\n"""
ROLE_DESC = lambda role: f"You are a {role}."
SYSTEM_MSG = ""
PRINT_LLM_DEBUG = False
SEARCHING_MODE = True
@backoff.on_exception(backoff.expo, openai.RateLimitError)
def get_json_response_from_gpt(
msg,
model,
system_message,
temperature=0.5
):
response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": msg},
],
temperature=temperature, max_tokens=4096, stop=None, response_format={"type": "json_object"}
)
content = response.choices[0].message.content
json_dict = json.loads(content, strict=False)
# cost = response.usage.completion_tokens / 1000000 * 15 + response.usage.prompt_tokens / 1000000 * 5
assert not json_dict is None
return json_dict
@backoff.on_exception(backoff.expo, openai.RateLimitError)
def get_json_response_from_gpt_reflect(
msg_list,
model,
temperature=0.8
):
response = client.chat.completions.create(
model=model,
messages=msg_list,
temperature=temperature, max_tokens=4096, stop=None, response_format={"type": "json_object"}
)
content = response.choices[0].message.content
json_dict = json.loads(content)
assert not json_dict is None
return json_dict
class LLMAgentBase():
"""
Attributes:
"""
def __init__(self, output_fields: list, agent_name: str,
role='helpful assistant', model='gpt-3.5-turbo-0125', temperature=0.5) -> None:
self.output_fields = output_fields
self.agent_name = agent_name
self.role = role
self.model = model
self.temperature = temperature
# give each instance a unique id
self.id = random_id()
def generate_prompt(self, input_infos, instruction) -> str:
# construct system prompt
output_fields_and_description = {key: f"Your {key}." if not 'answer' in key else f"Your {key}. Only include the answer. Keep it very concise." for key in self.output_fields}
system_prompt = ROLE_DESC(self.role) + "\n\n" + FORMAT_INST(output_fields_and_description)
# construct input infos text
input_infos_text = ''
for input_info in input_infos:
if isinstance(input_info, Info):
(field_name, author, content, iteration_idx) = input_info
else:
continue
if author == self.__repr__():
author += ' (yourself)'
if field_name == 'task':
input_infos_text += f'# Your Task:\n{content}\n\n'
elif iteration_idx != -1:
input_infos_text += f'### {field_name} #{iteration_idx+1} by {author}:\n{content}\n\n'
else:
input_infos_text += f'### {field_name} by {author}:\n{content}\n\n'
prompt = input_infos_text + instruction
return system_prompt, prompt
def query(self, input_infos: list, instruction, iteration_idx=-1) -> dict:
system_prompt, prompt = self.generate_prompt(input_infos, instruction)
try:
response_json = {}
response_json = get_json_response_from_gpt(prompt, self.model, system_prompt, self.temperature)
assert len(response_json) == len(self.output_fields), "not returning enough fields"
except Exception as e:
# print(e)
if "maximum context length" in str(e) and SEARCHING_MODE:
raise AssertionError("The context is too long. Please try to design the agent to have shorter context.")
#try to fill in the missing field
for key in self.output_fields:
if not key in response_json and len(response_json) < len(self.output_fields):
response_json[key] = ''
for key in copy.deepcopy(list(response_json.keys())):
if len(response_json) > len(self.output_fields) and not key in self.output_fields:
del response_json[key]
output_infos = []
for key, value in response_json.items():
info = Info(key, self.__repr__(), value, iteration_idx)
output_infos.append(info)
return output_infos
def __repr__(self):
return f"{self.agent_name} {self.id}"
def __call__(self, input_infos: list, instruction, iteration_idx=-1):
return self.query(input_infos, instruction, iteration_idx=iteration_idx)
class AgentSystem():
def __init__(self) -> None:
pass
def evaluate(args):
eval_file_path = args.eval_file_path
if os.path.exists(eval_file_path):
with open(eval_file_path, 'r') as json_file:
test_entries = json.load(json_file)
else:
raise AssertionError(f"File {eval_file_path} does not exist.")
for sol in test_entries:
print(f"{sol['name']}")
acc_list = evaluate_forward_fn(args, sol['code'])
sol['test_fitness_DROP'] = bootstrap_confidence_interval(acc_list)
# Step 5: Save the test entries
with open(eval_file_path, 'w') as json_file:
json.dump(test_entries, json_file, indent=4)
def evaluate_forward_fn(args, forward_str):
# dynamically define forward()
# modified from https://github.com/luchris429/DiscoPOP/blob/main/scripts/launch_evo.py
namespace = {}
exec(forward_str, globals(), namespace)
names = list(namespace.keys())
if len(names) != 1:
raise AssertionError(f"{len(names)} things in namespace. Please only provide 1")
func = namespace[names[0]]
if not callable(func):
raise AssertionError(f"{func} is not callable")
setattr(AgentSystem, "forward", func)
# set seed 0 for valid set
examples = load_drop(args.data_filename)[1:-1] # first one and the last one is for few-shot examples
random.seed(args.shuffle_seed)
random.shuffle(examples)
if SEARCHING_MODE:
examples = examples[:args.valid_size] * args.n_repreat
else:
examples = examples[args.valid_size:args.valid_size+args.test_size] * args.n_repreat
questions = [example['inputs'] for example in examples]
answers = [example['targets'] for example in examples]
print(f"problem length: {len(examples)}")
max_workers = min(len(examples), args.max_workers) if args.multiprocessing else 1
task_queue = []
for q in questions:
taskInfo = Info('task', 'User', q, -1)
task_queue.append(taskInfo)
agentSystem = AgentSystem()
acc_list = []
with ThreadPoolExecutor(max_workers=max_workers) as executor:
results = list(tqdm(executor.map(agentSystem.forward, task_queue), total=len(task_queue)))
for q_idx, res in enumerate(results):
try:
if isinstance(res, Info):
extracted_answer = res.content
else:
extracted_answer = res
correct_answers = answers[q_idx]
em_score, f1_score = drop_metric(extracted_answer, correct_answers)
except Exception as e:
acc_list.append(0)
continue
acc_list.append(f1_score)
print(f"acc: {bootstrap_confidence_interval(acc_list)}")
return acc_list
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--data_filename', type=str, default="dataset/drop_v0_dev.jsonl.gz")
parser.add_argument('--valid_size', type=int, default=128)
parser.add_argument('--test_size', type=int, default=800)
parser.add_argument('--shuffle_seed', type=int, default=0)
parser.add_argument('--n_repreat', type=int, default=1)
parser.add_argument('--multiprocessing', action='store_true', default=True)
parser.add_argument('--max_workers', type=int, default=48)
parser.add_argument('--debug', action='store_true', default=True)
parser.add_argument('--eval_file_path', type=str, default='')
parser.add_argument('--model',
type=str,
default='gpt-4o-2024-05-13',
choices=['gpt-4-turbo-2024-04-09', 'gpt-3.5-turbo-0125', 'gpt-4o-2024-05-13'])
args = parser.parse_args()
SEARCHING_MODE = False
evaluate(args)