-
Notifications
You must be signed in to change notification settings - Fork 19
/
run.py
263 lines (237 loc) · 8.41 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
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
# Copyright Sierra
import os
import json
import random
import argparse
import traceback
from math import comb
import multiprocessing
from typing import List, Dict, Any
from datetime import datetime
from concurrent.futures import ThreadPoolExecutor
from tau_bench.envs import get_env
from tau_bench.agents.base import Agent
from tau_bench.types import EnvRunResult
from litellm import provider_list
from tau_bench.envs.user import UserStrategy
def run(
args: argparse.Namespace,
ckpt_path: str,
) -> List[EnvRunResult]:
print(f"Loading user with strategy: {args.user_strategy}")
env = get_env(
args.env,
user_strategy=args.user_strategy,
user_model=args.user_model,
user_provider=args.user_model_provider,
task_split=args.task_split,
)
agent = agent_factory(
tools_info=env.tools_info,
wiki=env.wiki,
args=args,
)
end_index = (
len(env.tasks) if args.end_index == -1 else min(args.end_index, len(env.tasks))
)
results: List[EnvRunResult] = []
lock = multiprocessing.Lock()
if args.task_ids and len(args.task_ids) > 0:
print(f"Running tasks {args.task_ids} (checkpoint path: {ckpt_path})")
else:
print(
f"Running tasks {args.start_index} to {end_index} (checkpoint path: {ckpt_path})"
)
for i in range(args.num_trials):
if args.task_ids and len(args.task_ids) > 0:
idxs = args.task_ids
else:
idxs = list(range(args.start_index, end_index))
if args.shuffle:
random.shuffle(idxs)
def _run(idx: int) -> EnvRunResult:
isolated_env = get_env(
args.env,
user_strategy=args.user_strategy,
user_model=args.user_model,
task_split=args.task_split,
user_provider=args.user_model_provider,
task_index=idx,
)
print(f"Running task {idx}")
try:
res = agent.solve(
env=isolated_env,
task_index=idx,
)
result = EnvRunResult(
task_id=idx,
reward=res.reward,
info=res.info,
traj=res.messages,
trial=i,
)
except Exception as e:
result = EnvRunResult(
task_id=idx,
reward=0.0,
info={"error": str(e), "traceback": traceback.format_exc()},
traj=[],
trial=i,
)
print(
"✅" if result.reward == 1 else "❌",
f"task_id={idx}",
result.info,
)
print("-----")
with lock:
data = []
if os.path.exists(ckpt_path):
with open(ckpt_path, "r") as f:
data = json.load(f)
with open(ckpt_path, "w") as f:
json.dump(data + [result.model_dump()], f, indent=2)
return result
with ThreadPoolExecutor(max_workers=args.max_concurrency) as executor:
res = list(executor.map(_run, idxs))
results.extend(res)
return results
def agent_factory(
tools_info: List[Dict[str, Any]], wiki, args: argparse.Namespace
) -> Agent:
if args.agent_strategy == "tool-calling":
# native tool calling
from tau_bench.agents.tool_calling_agent import ToolCallingAgent
return ToolCallingAgent(
tools_info=tools_info,
wiki=wiki,
model=args.model,
provider=args.model_provider,
temperature=args.temperature,
)
elif args.agent_strategy == "act":
# `act` from https://arxiv.org/abs/2210.03629
from tau_bench.agents.chat_react_agent import ChatReActAgent
return ChatReActAgent(
tools_info=tools_info,
wiki=wiki,
model=args.model,
provider=args.model_provider,
use_reasoning=False,
temperature=args.temperature,
)
elif args.agent_strategy == "react":
# `react` from https://arxiv.org/abs/2210.03629
from tau_bench.agents.chat_react_agent import ChatReActAgent
return ChatReActAgent(
tools_info=tools_info,
wiki=wiki,
model=args.model,
provider=args.model_provider,
use_reasoning=True,
temperature=args.temperature,
)
else:
raise ValueError(f"Unknown agent strategy: {args.agent_strategy}")
def display_metrics(results: List[EnvRunResult]) -> None:
def is_successful(reward: float) -> bool:
return (1 - 1e-6) <= reward <= (1 + 1e-6)
num_trials = len(set([r.trial for r in results]))
rewards = [r.reward for r in results]
avg_reward = sum(rewards) / len(rewards)
# c from https://arxiv.org/pdf/2406.12045
c_per_task_id: dict[int, int] = {}
for result in results:
if result.task_id not in c_per_task_id:
c_per_task_id[result.task_id] = 1 if is_successful(result.reward) else 0
else:
c_per_task_id[result.task_id] += 1 if is_successful(result.reward) else 0
pass_hat_ks: dict[int, float] = {}
for k in range(1, num_trials + 1):
sum_task_pass_hat_k = 0
for c in c_per_task_id.values():
sum_task_pass_hat_k += comb(c, k) / comb(num_trials, k)
pass_hat_ks[k] = sum_task_pass_hat_k / len(c_per_task_id)
print(f"🏆 Average reward: {avg_reward}")
print("📈 Pass^k")
for k, pass_hat_k in pass_hat_ks.items():
print(f" k={k}: {pass_hat_k}")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--num-trials", type=int, default=1)
parser.add_argument(
"--env", type=str, choices=["retail", "airline"], default="retail"
)
parser.add_argument(
"--model",
type=str,
help="The model to use for the agent",
)
parser.add_argument(
"--model-provider",
type=str,
choices=provider_list,
help="The model provider for the agent",
)
parser.add_argument(
"--user-model",
type=str,
default="gpt-4o",
help="The model to use for the user simulator",
)
parser.add_argument(
"--user-model-provider",
type=str,
choices=provider_list,
help="The model provider for the user simulator",
)
parser.add_argument(
"--agent-strategy",
type=str,
default="tool-calling",
choices=["tool-calling", "act", "react"],
)
parser.add_argument(
"--temperature",
type=float,
default=0.0,
help="The sampling temperature for the action model",
)
parser.add_argument(
"--task-split",
type=str,
default="test",
choices=["train", "test", "dev"],
help="The split of tasks to run (only applies to the retail domain for now",
)
parser.add_argument("--start-index", type=int, default=0)
parser.add_argument("--end-index", type=int, default=-1, help="Run all tasks if -1")
parser.add_argument("--task-ids", type=int, nargs="+", help="(Optional) run only the tasks with the given IDs")
parser.add_argument("--log-dir", type=str, default="results")
parser.add_argument(
"--max-concurrency",
type=int,
default=1,
help="Number of tasks to run in parallel",
)
parser.add_argument("--seed", type=int, default=10)
parser.add_argument("--shuffle", type=int, default=0)
parser.add_argument("--user-strategy", type=str, default="llm", choices=[item.value for item in UserStrategy])
args = parser.parse_args()
print(args)
random.seed(args.seed)
time_str = datetime.now().strftime("%m%d%H%M%S")
file_str = f"{args.log_dir}/{args.agent_strategy}-{args.model.split('/')[-1]}-{args.temperature}_range_{args.start_index}-{args.end_index}_user-{args.user_model}-{args.user_strategy}_{time_str}.json"
if not os.path.exists(args.log_dir):
os.makedirs(args.log_dir)
results = run(
args=args,
ckpt_path=file_str,
)
display_metrics(results)
with open(file_str, "w") as f:
json.dump([result.model_dump() for result in results], f, indent=2)
print(f"\n📄 Results saved to {file_str}\n")
if __name__ == "__main__":
main()