MCPcopy Create free account
hub / github.com/ChenWu98/agent-attack / run

Function run

scripts/eval_step_attack.py:255–320  ·  view source on GitHub ↗
(args: argparse.Namespace, dataset, use_caption=True)

Source from the content-addressed store, hash-verified

253
254@beartype
255def run(args: argparse.Namespace, dataset, use_caption=True) -> None:
256 caption_image_fn = None # Don't use captioning for the demo, due to extra resources required to run BLIP-2.
257
258 agent = construct_agent(
259 args,
260 captioning_fn=caption_image_fn if args.observation_type == "accessibility_tree_with_captioner" else None,
261 ) # NOTE: captioning_fn here is used for captioning input images.
262
263 all_actions = {}
264 for ex_id, example in enumerate(dataset):
265 print(f"Running evaluation on Example {example['id']}")
266 try:
267 intent, images, trajectory, meta_data = load(
268 example, attack=args.attack, use_caption=use_caption, args=args
269 )
270 except Exception as e:
271 print(f"Error loading example {example['id']}: {repr(e)}")
272 continue
273 try:
274 # Load task.
275 logger.info(f"[Intent]: {intent}")
276
277 try:
278 print("=" * 30)
279 print("Agent: Thinking...")
280 action = agent.next_action(
281 trajectory, intent, images=images, meta_data=meta_data, output_response=True
282 )
283 except ValueError as e:
284 # get the error message
285 action = create_stop_action(f"ERROR: {str(e)}")
286
287 print("--- Raw prediction ---")
288 print(action["raw_prediction"])
289
290 pattern = r"\```(.+?)```"
291 match = re.search(pattern, action["raw_prediction"], re.DOTALL)
292 if match:
293 content = match.group(1)
294 content = content.strip()
295 else:
296 content = action["raw_prediction"]
297 # print(content)
298 all_actions[example["id"]] = content
299
300 except openai.OpenAIError as e:
301 logger.info(f"[OpenAI Error] {repr(e)}")
302 except Exception as e:
303 logger.info(f"[Unhandled Error] {repr(e)}]")
304 import traceback
305
306 # write to error file
307 with open(Path(args.result_dir) / "error.txt", "a") as f:
308 f.write(f"[Unhandled Error] {repr(e)}\n")
309 f.write(traceback.format_exc()) # write stack trace to file
310
311 if "gemini" in args.model:
312 print("Sleeping for 11 seconds...")

Callers 1

Calls 1

loadFunction · 0.70

Tested by

no test coverage detected