forked from ShengranHu/ADAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.py
360 lines (305 loc) · 13.6 KB
/
search.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import argparse
import copy
import json
import os
import random
from collections import namedtuple
from concurrent.futures import ThreadPoolExecutor
import backoff
import numpy as np
import openai
from tqdm import tqdm
from mgsm_prompt import get_init_archive, get_prompt, get_reflexion_prompt
client = openai.OpenAI()
from utils import get_all_examples, random_id, bootstrap_confidence_interval, score_mgsm
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)
# 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}. Return ONLY an integer. DO NOT return anything other than the integer answer." 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 search(args):
file_path = os.path.join(args.save_dir, f"{args.expr_name}_run_archive.json")
if os.path.exists(file_path):
with open(file_path, 'r') as json_file:
archive = json.load(json_file)
if "generation" in archive[-1] and isinstance(archive[-1]['generation'], int):
start = archive[-1]['generation']
else:
start = 0
else:
archive = get_init_archive()
start = 0
for solution in archive:
if 'fitness' in solution:
continue
solution['generation'] = "initial"
print(f"============Initial Archive: {solution['name']}=================")
try:
acc_list = evaluate_forward_fn(args, solution["code"])
except Exception as e:
print("During evaluating initial archive:")
print(e)
continue
fitness_str = bootstrap_confidence_interval(acc_list)
solution['fitness'] = fitness_str
# save results
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, 'w') as json_file:
json.dump(archive, json_file, indent=4)
for n in range(start, args.n_generation):
print(f"============Generation {n + 1}=================")
system_prompt, prompt = get_prompt(archive)
msg_list = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": prompt},
]
try:
next_solution = get_json_response_from_gpt_reflect(msg_list, args.model)
Reflexion_prompt_1, Reflexion_prompt_2 = get_reflexion_prompt(archive[-1] if n > 0 else None)
# Reflexion 1
msg_list.append({"role": "assistant", "content": str(next_solution)})
msg_list.append({"role": "user", "content": Reflexion_prompt_1})
next_solution = get_json_response_from_gpt_reflect(msg_list, args.model)
# Reflexion 2
msg_list.append({"role": "assistant", "content": str(next_solution)})
msg_list.append({"role": "user", "content": Reflexion_prompt_2})
next_solution = get_json_response_from_gpt_reflect(msg_list, args.model)
except Exception as e:
print("During LLM generate new solution:")
print(e)
n -= 1
continue
acc_list = []
for _ in range(args.debug_max):
try:
acc_list = evaluate_forward_fn(args, next_solution["code"])
if np.mean(acc_list) < 0.01 and SEARCHING_MODE:
raise Exception("All 0 accuracy")
break
except Exception as e:
print("During evaluation:")
print(e)
msg_list.append({"role": "assistant", "content": str(next_solution)})
msg_list.append({"role": "user", "content": f"Error during evaluation:\n{e}\nCarefully consider where you went wrong in your latest implementation. Using insights from previous attempts, try to debug the current code to implement the same thought. Repeat your previous thought in 'thought', and put your thinking for debugging in 'debug_thought'"})
try:
next_solution = get_json_response_from_gpt_reflect(msg_list, args.model)
except Exception as e:
print("During LLM generate new solution:")
print(e)
continue
continue
if not acc_list:
n -= 1
continue
fitness_str = bootstrap_confidence_interval(acc_list)
next_solution['fitness'] = fitness_str
next_solution['generation'] = n + 1
if 'debug_thought' in next_solution:
del next_solution['debug_thought']
if 'reflection' in next_solution:
del next_solution['reflection']
archive.append(next_solution)
# save results
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, 'w') as json_file:
json.dump(archive, json_file, indent=4)
def evaluate(args):
file_path = os.path.join(args.save_dir, f"{args.expr_name}_run_archive.json")
eval_file_path = str(os.path.join(args.save_dir, f"{args.expr_name}_run_archive.json")).strip(".json") + "_evaluate.json"
with open(file_path, 'r') as json_file:
archive = json.load(json_file)
eval_archive = []
if os.path.exists(eval_file_path):
with open(eval_file_path, 'r') as json_file:
eval_archive = json.load(json_file)
current_idx = 0
while (current_idx < len(archive)):
with open(file_path, 'r') as json_file:
archive = json.load(json_file)
if current_idx < len(eval_archive):
current_idx += 1
continue
sol = archive[current_idx]
print(f"current_gen: {sol['generation']}, current_idx: {current_idx}")
current_idx += 1
try:
acc_list = evaluate_forward_fn(args, sol["code"])
except Exception as e:
print(e)
continue
fitness_str = bootstrap_confidence_interval(acc_list)
sol['test_fitness'] = fitness_str
eval_archive.append(sol)
# save results
os.makedirs(os.path.dirname(eval_file_path), exist_ok=True)
with open(eval_file_path, 'w') as json_file:
json.dump(eval_archive, 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 = get_all_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_answer = answers[q_idx]
correct = score_mgsm(correct_answer, extracted_answer)
except Exception as e:
acc_list.append(0)
continue
acc_list.append(1 if correct else 0)
print(f"acc: {bootstrap_confidence_interval(acc_list)}")
return acc_list
if __name__ == "__main__":
parser = argparse.ArgumentParser()
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('--save_dir', type=str, default='results/')
parser.add_argument('--expr_name', type=str, default="mgsm_gpt3.5_results")
parser.add_argument('--n_generation', type=int, default=30)
parser.add_argument('--debug_max', type=int, default=3)
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()
# search
SEARCHING_MODE = True
search(args)
# evaluate
SEARCHING_MODE = False
evaluate(args)