-
Notifications
You must be signed in to change notification settings - Fork 4
/
run.py
185 lines (145 loc) · 6.64 KB
/
run.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
import argparse
import datetime
import json
import logging
import os
import random
import sys
from tqdm import tqdm
from da_agent.envs.da_agent import DA_Agent_Env
from da_agent.agent.agents import PromptAgent
# Logger Configs {{{ #
logger = logging.getLogger("da_agent")
logger.setLevel(logging.DEBUG)
datetime_str: str = datetime.datetime.now().strftime("%Y%m%d@%H%M%S")
file_handler = logging.FileHandler(os.path.join("logs", "normal-{:}.log".format(datetime_str)), encoding="utf-8")
debug_handler = logging.FileHandler(os.path.join("logs", "debug-{:}.log".format(datetime_str)), encoding="utf-8")
stdout_handler = logging.StreamHandler(sys.stdout)
sdebug_handler = logging.FileHandler(os.path.join("logs", "sdebug-{:}.log".format(datetime_str)), encoding="utf-8")
file_handler.setLevel(logging.INFO)
debug_handler.setLevel(logging.DEBUG)
stdout_handler.setLevel(logging.INFO)
sdebug_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter(
fmt="\x1b[1;33m[%(asctime)s \x1b[31m%(levelname)s \x1b[32m%(module)s/%(lineno)d-%(processName)s\x1b[1;33m] \x1b[0m%(message)s")
file_handler.setFormatter(formatter)
debug_handler.setFormatter(formatter)
stdout_handler.setFormatter(formatter)
sdebug_handler.setFormatter(formatter)
stdout_handler.addFilter(logging.Filter("da_agent"))
sdebug_handler.addFilter(logging.Filter("da_agent"))
logger.addHandler(file_handler)
logger.addHandler(debug_handler)
logger.addHandler(stdout_handler)
logger.addHandler(sdebug_handler)
# }}} Logger Configs #
def config() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Run end-to-end evaluation on the benchmark"
)
parser.add_argument("--max_steps", type=int, default=20)
parser.add_argument("--max_memory_length", type=int, default=15)
parser.add_argument("--suffix", '-s', type=str, default="")
parser.add_argument("--model",'-m',type=str, default="gpt-4o")
parser.add_argument("--temperature", type=float, default=0.0)
parser.add_argument("--top_p", type=float, default=0.9)
parser.add_argument("--max_tokens", type=int, default=1500)
parser.add_argument("--stop_token", type=str, default=None)
# example config
parser.add_argument("--task_config","-t", type=str, default="da_code/configs/task/examples.jsonl")
parser.add_argument("--source_dir", type=str, default="da_code/source")
parser.add_argument("--example_index", "-i", type=str, default="all", help="index range of the examples to run, e.g., '0-10', '2,3', 'all'")
parser.add_argument("--example_name", "-n", type=str, default="", help="name of the example to run")
parser.add_argument("--overwriting", action="store_true", default=False)
parser.add_argument("--retry_failed", action="store_true", default=False)
# output related
parser.add_argument("--output_dir", type=str, default="output")
args = parser.parse_args()
return args
def test(
args: argparse.Namespace,
test_all_meta: dict = None
) -> None:
scores = []
# log args
logger.info("Args: %s", args)
if args.suffix == "":
logger.warning("No suffix is provided, the experiment id will be the model name.")
experiment_id = args.model.split("/")[-1]
else:
experiment_id = args.model.split("/")[-1] + "-" + args.suffix
env_config = \
{
"image_name": "da_agent-image",
"init_args": {
"name": experiment_id,
"work_dir": "/workspace",
}
}
agent = PromptAgent(
model=args.model,
max_tokens=args.max_tokens,
top_p=args.top_p,
temperature=args.temperature,
max_memory_length=args.max_memory_length,
max_steps=args.max_steps,
)
## load task configs
assert os.path.exists(args.task_config) and args.task_config.endswith(".jsonl"), f"Invalid task_config, must be a valid jsonl file: {args.task_config}"
with open(args.task_config, "r", encoding="utf-8") as f:
task_configs = [json.loads(line) for line in f]
if args.example_name != "":
task_configs = [task for task in task_configs if args.example_name in task["id"]]
else:
if args.example_index != "all":
if "-" in args.example_index:
start, end = map(int, args.example_index.split("-"))
task_configs = task_configs[start:end]
else:
indices = list(map(int, args.example_index.split(",")))
task_configs = [task_configs[i] for i in indices]
for task_config in task_configs:
instance_id = experiment_id +"/"+ task_config["id"]
output_dir = os.path.join(args.output_dir, instance_id)
result_json_path =os.path.join(output_dir, "dabench/result.json")
if not args.overwriting and os.path.exists(result_json_path):
logger.info("Skipping %s", instance_id)
continue
elif os.path.exists(result_json_path):
logger.info("Overwriting %s", instance_id)
else:
logger.info("Running %s", instance_id)
if args.retry_failed and os.path.exists(result_json_path):
with open(result_json_path, "r") as f:
result = json.load(f)
if result["finished"] and (not "FAIL" in result["result"]) and (not "error" in result["result"].lower()):
logger.info("Skipping %s", instance_id)
continue
logger.info("Retrying %s", instance_id)
if os.path.exists(output_dir):
os.system(f"rm -rf {output_dir}")
logger.info("Removed existing %s", output_dir)
os.makedirs(output_dir, exist_ok=True)
env_config["init_args"]["name"] = experiment_id +"-"+ task_config["id"]
env = DA_Agent_Env(
env_config=env_config,
task_config=task_config,
source_dir=args.source_dir,
cache_dir="./cache",
mnt_dir=output_dir
)
agent.set_env_and_task(env)
logger.info('Task input:' + task_config['instruction'])
done, result_output = agent.run()
trajectory = agent.get_trajectory()
os.makedirs(os.path.join(output_dir, "dabench"), exist_ok=True)
result_files = env.post_process()
dabench_result = {"finished": done, "steps": len(trajectory["trajectory"]),
"result": result_output,"result_files": result_files, **trajectory}
with open(os.path.join(output_dir, "dabench/result.json"), "w") as f:
json.dump(dabench_result, f, indent=2)
logger.info("Finished %s", instance_id)
env.close()
if __name__ == '__main__':
args = config()
test(args)