-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_transfer.py
189 lines (167 loc) · 8.7 KB
/
test_transfer.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
import json
import re
from utils import process_code, chat_api
# ================================ #
task = "Tooltransfer"
# Choose the mode from "normal" and "hint"
mode = "normal"
# ================================ #
start_key = 0
temperature = 0.3
prompt1 = open(f"{task}/prompt_lib/prompt_CREATOR_creation.md", "r").read().strip()
prompt2 = open(f"{task}/prompt_lib/prompt_CREATOR_decision.md", "r").read().strip()
prompt_t = open(f"{task}/prompt_lib/prompt_tool_transfer.md", "r").read().strip()
err_prompt = open(f"{task}/prompt_lib/prompt_rectification.md", "r").read().strip()
save_file = f"{task}/results/results_tooltransfer_test.md"
code_file = "code_exec/tmp0"
gen_func = chat_api
sys_msg = "You are a helpful assistant in answering questions with tools."
# ================================ #
if mode == "hint":
prompt1 = open("ToolTransfer/prompt_lib/prompt_CREATOR_creation_hint.md", "r").read().strip()
# ================================ #
bad_exec = 0
good_ans = 0
bad_ans = 0
good_transfer = 0
good_transfer_time = 0
all_transfer_data = []
all_separate_correct = []
f = open(save_file, "w")
f.close()
f = open("ToolTransfer/dataset/transfer_300.jsonl", "r")
lines = f.readlines()
f.close()
for line in lines:
print("======================new question begin========================")
data = json.loads(line)
# ========= hints ======== (can apply / not apply based on the mode chosen)
hints = f"create a tool based on the following introduction:\n{data['intro'].strip()}"
# ========================
env = prompt1
separate_correct = 0
useful_tools = []
print("Answering problems one by one...")
for i in range(3):
print("in problem", i)
answer = data[f"ans{i+1}"]
question = data[f"scn{i+1}"]
env = env.replace("===problem===", question).replace("===hints===", hints)
res = gen_func(env, start_key, sys_msg, temperature=temperature)
try:
tool_code = res.split("```python")[1].split("```")[0].strip()
env_cont = prompt2
env_cont = env_cont.replace("===problem===", question).replace("===hints===", hints).replace("===tool===", tool_code)
res_cont = gen_func(env_cont, start_key, sys_msg, temperature=temperature)
call_code = res_cont.split("```python")[1].split("```")[0].strip()
response = "```python\n" + tool_code + "\n\n" + call_code + "\n```"
if_succ, info = process_code(response, code_file)
if not if_succ:
raise Exception(info)
else:
print("~~~ Runing successfully ~~~")
model_ans = re.findall(r'-?\d+\.?\d*', info)
model_ans = [float(ans) for ans in model_ans]
correct_flag = False
for ans in model_ans:
if round(ans,2) == round(answer,2):
print("~~~ Correct Answer ~~~")
correct_flag = True
f = open(save_file, "a")
f.write("### Problem\n" + question.strip() + "\n### Response\n" + response.strip() + "\n\n### Return Info\n" + info.strip() + f"\n\nCorrect Answer!\nThe correct answer should be {answer}")
f.write("\n\n=============================split line=============================\n\n")
f.close()
good_ans += 1
separate_correct += 1
useful_tools.append({"tool": "```python\n" + tool_code + "\n```", "num": i})
break
if not correct_flag:
print("!!! Wrong Answer !!!")
f = open(save_file, "a")
f.write("### Problem\n" + question.strip() + "\n### Response\n" + response.strip() + "\n\n### Return Info\n" + info.strip() + f"\n\nWrong Answer!\nThe correct answer should be {answer}")
f.write("\n\n=============================split line=============================\n\n")
f.close()
bad_ans += 1
except Exception as e:
bad_exec += 1
print("!!! Bad Execution !!!")
f = open(save_file, "a")
f.write("Met Unknown Error! Stop!")
f.write("\n\n=============================split line=============================\n\n")
f.close()
if i == 2:
f = open(save_file, "a")
f.write(f"Separate Correct: {separate_correct}")
f.write("\n\n=============================split line=============================\n\n")
f.close()
f = open(save_file, "a")
f.write(f"Separate Correct: {separate_correct}")
f.write("\n\n=============================split line=============================\n\n")
f.write("\n\n=============================begin transfer test=============================\n\n")
f.close()
all_separate_correct.append(separate_correct)
print("Bad Execution: ", bad_exec, "Good Answer: ", good_ans, "Bad Answer: ", bad_ans)
print("======================Test transfer begin========================")
print(f"Begin Test Useful Tools, total number: {len(useful_tools)}")
success_rate = []
for tool in useful_tools:
success_transfer = 0
for i in range(3):
if i == tool["num"]:
continue
answer = data[f"ans{i+1}"]
question = data[f"scn{i+1}"]
env_cont = prompt_t
tool_code = tool["tool"]
env_cont = env_cont.replace("===problem===", question).replace("===hints===", hints).replace("===tool===", tool_code)
res_cont = gen_func(env_cont, start_key, sys_msg, temperature=temperature)
try:
call_code = res_cont.split("```python")[1].split("```")[0].strip()
tool_code = tool_code.split("```python")[1].split("```")[0].strip()
response = "```python\n" + tool_code + "\n\n" + call_code + "\n```"
if_succ, info = process_code(response, code_file)
if not if_succ:
raise Exception(info)
else:
print("~~~ Runing successfully ~~~")
model_ans = re.findall(r'-?\d+\.?\d*', info)
model_ans = [float(ans) for ans in model_ans]
correct_flag = False
for ans in model_ans:
if round(ans,2) == round(answer,2):
print("~~~ Transfer Correct Answer ~~~")
correct_flag = True
f = open(save_file, "a")
f.write("### Problem\n" + question.strip() + "\n### Response\n" + response.strip() + "\n\n### Return Info\n" + info.strip() + f"\n\nCorrect Transfer Answer!\nThe correct answer should be {answer}")
f.write("\n\n=============================split line=============================\n\n")
f.close()
success_transfer += 1
break
if not correct_flag:
print("!!! Wrong Answer !!!")
f = open(save_file, "a")
f.write("### Problem\n" + question.strip() + "\n### Response\n" + response.strip() + "\n\n### Return Info\n" + info.strip() + f"\n\nWrong Transfer Answer!\nThe correct answer should be {answer}")
f.write("\n\n=============================split line=============================\n\n")
f.close()
except:
print("!!! Transfer Bad Execution !!!")
f = open(save_file, "a")
f.write("Met Unknown Error! Stop! Bad Transfer Parsing!")
f.write("\n\n=============================split line=============================\n\n")
f.close()
success_rate.append(success_transfer)
success_transfer = max(success_rate) if len(success_rate) > 0 else 0
if len(success_rate) > 0:
good_transfer_time += 1
# originally self success!
good_transfer += (success_transfer + 1)
all_transfer_data.append(success_rate)
f = open(save_file, "a")
f.write("\n\n============================end transfer test=============================\n\n")
f.close()
f = open(save_file, "a")
f.write(f"Good Answer: {good_ans}\nBad Answer: {bad_ans}\nBad Execution: {bad_exec}\n")
f.write(f"Good Transfer: {good_transfer}\nGood Transfer Time: {good_transfer_time}\n")
f.write(f"Transfer Data:\n{str(all_transfer_data)}\n")
f.write(f"Separate Correct:\n{str(all_separate_correct)}\n")
f.close()