-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.py
289 lines (269 loc) · 9.23 KB
/
main.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# =========== Copyright 2024 @ CAMEL-AI.org. All Rights Reserved. ===========
# Licensed under the Apache License, Version 2.0 (the “License”);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an “AS IS” BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2024 @ CAMEL-AI.org. All Rights Reserved. ===========
import argparse
import logging
import warnings
from pathlib import Path
from typing import Literal
from crab import (
BenchmarkConfig,
Experiment,
MessageType,
TaskGenerator,
create_benchmark,
)
from crab.actions.crab_actions import complete, wait
from crab.actions.visual_prompt_actions import (
get_elements_prompt,
groundingdino_easyocr,
)
from crab.agents.backend_models import BackendModelConfig
from crab.agents.policies import (
MultiAgentByEnvPolicy,
MultiAgentByFuncPolicy,
SingleAgentPolicy,
)
from crab.core.agent_policy import AgentPolicy
from crab.core.benchmark import Benchmark
from .android_env import ANDROID_ENV
from .dataset.android_subtasks import android_subtasks
from .dataset.handmade_tasks import handmade_tasks
from .dataset.ubuntu_subtasks import ubuntu_subtasks
from .ubuntu_env import UBUNTU_ENV
warnings.filterwarnings("ignore")
class CrabBenchmarkV0(Experiment):
def __init__(
self,
benchmark: Benchmark,
task_id: str,
agent_policy: AgentPolicy | Literal["human"],
log_dir: Path | None = None,
) -> None:
super().__init__(benchmark, task_id, agent_policy, log_dir)
def get_prompt(self):
observation, ob_prompt = self.benchmark.observe_with_prompt()
# construct prompt
result_prompt = {}
for env in ob_prompt:
if env == "root":
continue
screenshot = observation[env]["screenshot"]
marked_screenshot, _ = ob_prompt[env]["screenshot"]
result_prompt[env] = [
(f"Here is the current screenshot of {env}:", MessageType.TEXT),
(screenshot, MessageType.IMAGE_JPG_BASE64),
(
f"Here is the screenshot with element labels of {env}:",
MessageType.TEXT,
),
(marked_screenshot, MessageType.IMAGE_JPG_BASE64),
]
return result_prompt
def get_benchmark(env: str, ubuntu_url: str):
ubuntu_env = UBUNTU_ENV.model_copy()
ubuntu_env.remote_url = ubuntu_url
ubuntu_tool = {
"screenshot": groundingdino_easyocr(font_size=16) >> get_elements_prompt
}
android_tool = {
"screenshot": groundingdino_easyocr(font_size=40) >> get_elements_prompt
}
if env == "ubuntu":
prompting_tools = {"ubuntu": ubuntu_tool}
benchmark_config = BenchmarkConfig(
name="ubuntu_benchmark",
tasks=[],
environments=[ubuntu_env],
prompting_tools=prompting_tools,
root_action_space=[complete, wait],
multienv=True,
)
elif env == "android":
prompting_tools = {"android": android_tool}
benchmark_config = BenchmarkConfig(
name="android_benchmark",
tasks=[],
environments=[ANDROID_ENV],
prompting_tools=prompting_tools,
root_action_space=[complete, wait],
multienv=True,
)
elif env == "cross":
prompting_tools = {
"android": android_tool,
"ubuntu": ubuntu_tool,
}
benchmark_config = BenchmarkConfig(
name="ubuntu_android_benchmark",
tasks=[],
environments=[ubuntu_env, ANDROID_ENV],
prompting_tools=prompting_tools,
root_action_space=[complete, wait],
multienv=True,
)
else:
raise ValueError("Env not support")
# Load from json config files by combining sub-tasks
generator = TaskGenerator(subtasks=android_subtasks + ubuntu_subtasks)
dir_path = (Path(__file__).parent / "dataset").resolve()
tasks = []
for task_json_files in dir_path.rglob("*.json"):
task = generator.get_task_from_file(task_json_files)
tasks.append(task)
benchmark_config.tasks.extend(tasks)
# Load from handmade tasks
benchmark_config.tasks.extend(handmade_tasks)
benchmark_config.step_limit = 20
return create_benchmark(benchmark_config)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Script for running benchmark with an agent."
)
parser.add_argument(
"--model",
type=str,
help="gpt4o, gpt4turbo, gemini, claude or human",
default="gpt4o",
)
parser.add_argument(
"--policy",
type=str,
help="single, multi-by-func, or multi-by-env",
default="single",
)
parser.add_argument(
"--ubuntu-url",
type=str,
help="remote url of Ubunutu environment",
default="http://127.0.0.1:8000",
)
parser.add_argument(
"--env",
type=str,
help="ubuntu, android or cross",
default="cross",
)
parser.add_argument("--task-id", type=str, help="task id")
parser.add_argument(
"--model-base-url",
type=str,
help="URL of the model API",
default="http://127.0.0.1:8000/v1",
)
parser.add_argument(
"--model-api-key",
type=str,
help="API key of the model API",
default="EMPTY",
)
parser.add_argument(
"--loglevel",
type=str,
help="logger level, debug, info, warning, or error",
default="warning",
)
parser.add_argument(
"--history-messages-len",
type=int,
help="The number of rounds of chat history to provide to the model",
default=2,
)
args = parser.parse_args()
loglevel = args.loglevel
numeric_level = getattr(logging, loglevel.upper(), None)
if not isinstance(numeric_level, int):
raise ValueError("Invalid log level: %s" % loglevel)
logging.basicConfig(level=numeric_level)
benchmark = get_benchmark(args.env, args.ubuntu_url)
if args.model == "human":
expeirment = CrabBenchmarkV0(
benchmark=benchmark,
task_id=args.task_id,
agent_policy="human",
)
expeirment.start_benchmark()
exit()
if args.model == "gpt4o":
model = BackendModelConfig(
model_class="openai",
model_name="gpt-4o",
history_messages_len=args.history_messages_len,
)
elif args.model == "gpt4turbo":
model = BackendModelConfig(
model_class="openai",
model_name="gpt-4-turbo",
history_messages_len=args.history_messages_len,
)
elif args.model == "gemini":
model = BackendModelConfig(
model_class="gemini",
model_name="gemini-1.5-pro-latest",
history_messages_len=args.history_messages_len,
)
elif args.model == "claude":
model = BackendModelConfig(
model_class="claude",
model_name="claude-3-opus-20240229",
history_messages_len=args.history_messages_len,
)
elif args.model == "pixtral":
model = BackendModelConfig(
model_class="openai",
model_name="mistralai/Pixtral-12B-2409",
json_structre_output=True,
history_messages_len=args.history_messages_len,
base_url=args.model_base_url,
api_key=args.model_api_key,
)
elif args.model == "gpt4o-wofc":
model = BackendModelConfig(
model_class="openai",
model_name="gpt-4o",
json_structre_output=True,
history_messages_len=args.history_messages_len,
)
elif args.model == "llava-ov72b":
model = BackendModelConfig(
model_class="sglang",
model_name="lmms-lab/llava-onevision-qwen2-72b-ov-chat",
json_structre_output=True,
history_messages_len=args.history_messages_len,
base_url=args.model_base_url,
api_key=args.model_api_key,
)
else:
print("Unsupported model: ", args.model)
exit()
if args.policy == "single":
agent_policy = SingleAgentPolicy(model_backend=model)
elif args.policy == "multi-by-func":
agent_policy = MultiAgentByFuncPolicy(
main_agent_model_backend=model, tool_agent_model_backend=model
)
elif args.policy == "multi-by-env":
agent_policy = MultiAgentByEnvPolicy(
main_agent_model_backend=model, env_agent_model_backend=model
)
else:
print("Unsupported policy: ", args.policy)
exit()
log_dir = (Path(__file__).parent / "tianqi_logs").resolve()
expeirment = CrabBenchmarkV0(
benchmark=benchmark,
task_id=args.task_id,
agent_policy=agent_policy,
log_dir=log_dir,
)
expeirment.start_benchmark()