-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun_exp.py
145 lines (117 loc) · 4.18 KB
/
run_exp.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
import argparse
import numpy as np
import sys
import os
import json
project_root_path = os.path.dirname(os.path.abspath(__file__))
if project_root_path not in sys.path:
sys.path.insert(0, project_root_path)
from copy import deepcopy
from chinatravel.data.load_datasets import load_query, save_json_file
from chinatravel.agent.load_model import init_agent, init_llm
from chinatravel.environment.world_env import WorldEnv
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="argparse testing")
parser.add_argument(
"--splits",
"-s",
type=str,
default="easy",
help="query subset",
)
parser.add_argument("--index", "-id", type=str, default=None, help="query index")
parser.add_argument(
"--skip", "-sk", type=int, default=0, help="skip if the plan exists"
)
parser.add_argument(
"--agent",
"-a",
type=str,
default=None,
choices=["RuleNeSy", "LLMNeSy", "LLM-modulo", "ReAct", "Act"],
)
parser.add_argument(
"--llm",
"-l",
type=str,
default=None
)
parser.add_argument('--oracle_translation', action='store_true', help='Set this flag to enable oracle translation.')
parser.add_argument('--preference_search', action='store_true', help='Set this flag to enable preference search.')
parser.add_argument('--refine_steps', type=int, default=10, help='Steps for refine-based method, such as LLM-modulo, Reflection')
args = parser.parse_args()
print(args)
query_index, query_data = load_query(args)
print(len(query_index), "samples")
if args.index is not None:
query_index = [args.index]
cache_dir = os.path.join(project_root_path, "cache")
method = args.agent + "_" + args.llm
if args.agent == "LLM-modulo":
method += f"_{args.refine_steps}steps"
if args.oracle_translation:
method = method + "_oracletranslation"
if args.preference_search:
method = method + "_preferencesearch"
res_dir = os.path.join(
project_root_path, "results", method
)
log_dir = os.path.join(
project_root_path, "cache", method
)
if not os.path.exists(res_dir):
os.makedirs(res_dir)
if not os.path.exists(log_dir):
os.makedirs(log_dir)
print("res_dir: ", res_dir)
print("log_dir:", log_dir)
kwargs = {
"method": args.agent,
"env": WorldEnv(),
"backbone_llm": init_llm(args.llm),
"cache_dir": cache_dir,
"log_dir": log_dir,
"debug": True,
"refine_steps": args.refine_steps,
}
agent = init_agent(kwargs)
white_list = []
succ_count, eval_count = 0, 0
for i, data_idx in enumerate(query_index):
sys.stdout = sys.__stdout__
print("------------------------------")
print(
"Process [{}/{}], Success [{}/{}]:".format(
i, len(query_index), succ_count, eval_count
)
)
print("data uid: ", data_idx)
if args.skip and os.path.exists(os.path.join(res_dir, f"{data_idx}.json")):
continue
if i in white_list:
continue
eval_count += 1
symbolic_input = query_data[data_idx]
print(symbolic_input)
if args.agent in ["ReAct", "Act"]:
plan_log = agent(symbolic_input["nature_language"])
plan = plan_log["ans"]
if isinstance(plan, str):
try:
plan = json.loads(plan)
except:
plan = plan
log = plan_log["log"]
save_json_file(
json_data=log, file_path=os.path.join(log_dir, f"{data_idx}.json")
)
succ = 1
elif args.agent in ["LLM-modulo"]:
succ, plan = agent.solve(symbolic_input, prob_idx=data_idx, oracle_verifier=True)
else:
succ, plan = agent.run(symbolic_input, load_cache=True, oralce_translation=args.oracle_translation, preference_search=args.preference_search)
if succ:
succ_count += 1
save_json_file(
json_data=plan, file_path=os.path.join(res_dir, f"{data_idx}.json")
)