-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy patheval_exp.py
More file actions
218 lines (161 loc) · 7.57 KB
/
Copy patheval_exp.py
File metadata and controls
218 lines (161 loc) · 7.57 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
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
import argparse
import os
project_root_path = os.path.dirname(os.path.abspath(__file__))
def default_method_list(llm_name=None, *, lang="zh"):
from chinatravel.agent.load_model import build_method_name, resolve_llm_name
resolved_llm_name = resolve_llm_name(llm_name)
if not resolved_llm_name:
raise ValueError(
"--method all requires --llm <model> or CHINATRAVEL_OPENAI_MODEL/OPENAI_MODEL."
)
return [
build_method_name("RuleNeSy", "rule", lang=lang),
build_method_name("LLMNeSy", resolved_llm_name, lang=lang),
build_method_name("Act", resolved_llm_name, lang=lang),
build_method_name("ReAct", resolved_llm_name, lang=lang),
build_method_name("ReAct0", resolved_llm_name, lang=lang),
build_method_name(
"LLM-modulo",
resolved_llm_name,
lang=lang,
refine_steps=10,
oracle_translation=True,
),
build_method_name("TPCAgent", resolved_llm_name, lang=lang),
]
def load_result(args, query_index, verbose=False):
def load_result_for_method(method):
plans = {}
for query_id in query_index:
result_file = os.path.join(
"results/", method, "{}.json".format(query_id)
)
try:
if os.path.exists(result_file):
from chinatravel.evaluation.utils import load_json_file
result = load_json_file(result_file)
plans[query_id] = result
else:
plans[query_id] = {}
except:
plans[query_id] = {}
return plans
result = {}
if args.method == "all":
method_list = default_method_list(
getattr(args, "llm", None),
lang=getattr(args, "lang", "zh"),
)
else:
method_list = [args.method]
for method in method_list:
result[method] = load_result_for_method(method)
if verbose:
print(result)
return method_list, result
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--splits", "-s", type=str, default="example")
parser.add_argument(
"--method", "-m", type=str, default="example"
)
parser.add_argument(
"--llm",
"-l",
type=str,
default=None,
help="Model name used to derive default result directories when --method all.",
)
parser.add_argument("--preference", "-p", action="store_true", default=False)
parser.add_argument("--lang", "--locale", choices=["zh", "en"], default="zh")
args = parser.parse_args()
if args.method == "all":
from chinatravel.agent.load_model import resolve_llm_name
if not resolve_llm_name(args.llm):
parser.error(
"--method all requires --llm <model> or CHINATRAVEL_OPENAI_MODEL/OPENAI_MODEL."
)
if args.method != "all":
from chinatravel.agent.load_model import ensure_method_language
args.method = ensure_method_language(args.method, args.lang)
# print(args.splits)
from chinatravel.data.load_datasets import load_query
from chinatravel.evaluation.commonsense_constraint import (
evaluate_commonsense_constraints,
)
from chinatravel.evaluation.hard_constraint import evaluate_hard_constraints_v2
from chinatravel.evaluation.preference import evaluate_preference_v2
from chinatravel.evaluation.schema_constraint import evaluate_schema_constraints
from chinatravel.evaluation.utils import load_json_file
query_index, query_data = load_query(args)
method_list, result_data = load_result(args, query_index)
# print(result_data)
schema_file_path = 'chinatravel/evaluation/output_schema.json'
schema = load_json_file(schema_file_path)
if not os.path.exists("eval_res/"):
os.makedirs("eval_res/")
if not os.path.exists("eval_res/splits_{}/".format(args.splits)):
os.makedirs("eval_res/splits_{}/".format(args.splits))
for method in method_list:
print("method: ", method)
plan_count = sum(1 for plan in result_data[method].values() if plan)
print("There are {} results...".format(plan_count))
print("Method: {}".format(method))
if not os.path.exists("eval_res/splits_{}/{}/".format(args.splits, method)):
os.makedirs("eval_res/splits_{}/{}/".format(args.splits, method))
schema_rate, schema_result_agg, schema_pass_id = evaluate_schema_constraints(
query_index, result_data[method], schema=schema
)
res_file = "eval_res/splits_{}/{}/schema.csv".format(args.splits, method)
schema_result_agg.to_csv(res_file, index=False)
print("save to {}".format(res_file))
print("Schema Pass Rate:", schema_rate)
macro_comm, micro_comm, common_result_agg, commonsense_pass_id = evaluate_commonsense_constraints(
query_index, query_data, result_data[method], verbose=False, lang=args.lang
)
res_file = "eval_res/splits_{}/{}/commonsense.csv".format(args.splits, method)
common_result_agg.to_csv(res_file, index=False)
print("save to {}".format(res_file))
print("Commonsense constraints:")
print("micro accuracy: {}".format(micro_comm))
print("macro accuracy: {}".format(macro_comm))
# print("Logical constraints (flat version):")
# macro_logi, micro_logi, logi_result_agg, logi_pass_id_flat = evaluate_hard_constraints(
# query_index, query_data, result_data[method], verbose=False
# )
# print("micro accuracy: {}".format(micro_logi))
# print("macro accuracy: {}".format(macro_logi))
# res_file = "eval_res/splits_{}/{}/logical.csv".format(args.splits, method)
# logi_result_agg.to_csv(res_file, index=False)
# print("save to {}".format(res_file))
print("Logical constraints (python version):")
macro_logi, micro_logi, conditional_macro_logi, conditional_micro_logi, logi_result_agg, logi_pass_id = evaluate_hard_constraints_v2(
query_index, query_data, result_data[method], env_pass_id=commonsense_pass_id, verbose=False, lang=args.lang
)
print("micro accuracy: {}".format(micro_logi))
print("macro accuracy: {}".format(macro_logi))
print("conditional micro accuracy: {}".format(conditional_micro_logi))
print("conditional macro accuracy: {}".format(conditional_macro_logi))
print("Conditional LPR: {}".format(conditional_micro_logi))
res_file = "eval_res/splits_{}/{}/logical_py.csv".format(args.splits, method)
logi_result_agg.to_csv(res_file, index=False)
print("save to {}".format(res_file))
# record the index of the queries that pass the logical constraints
logical_pass_info = logi_result_agg.iloc[:, 1:]
id_list = logi_result_agg.iloc[:, 0].tolist()
all_pass_id = list(set(schema_pass_id) & set(commonsense_pass_id) & set(logi_pass_id))
print("All pass ratio: ", 1. * len(all_pass_id) / len(query_index) * 100)
if args.preference:
print("Preference:")
result_agg = evaluate_preference_v2(
query_index,
query_data,
result_data[method],
list(set(commonsense_pass_id) & set(logi_pass_id)),
lang=args.lang,
)
res_file = "eval_res/splits_{}/{}/preference.csv".format(
args.splits, method
)
result_agg.to_csv(res_file, index=False)
print("save to {}".format(res_file))