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

Function run

scripts/eval_step.py:198–257  ·  view source on GitHub ↗
(args: argparse.Namespace, dataset, use_caption=True)

Source from the content-addressed store, hash-verified

196
197@beartype
198def run(args: argparse.Namespace, dataset, use_caption=True) -> None:
199 caption_image_fn = None # Don't use captioning for the demo, due to extra resources required to run BLIP-2.
200
201 agent = construct_agent(
202 args,
203 captioning_fn=caption_image_fn if args.observation_type == "accessibility_tree_with_captioner" else None,
204 ) # NOTE: captioning_fn here is used for captioning input images.
205
206 all_actions = {}
207 for ex_id, example in enumerate(dataset):
208 print(f"Running evaluation on Example {example['id']}")
209 intent, images, trajectory, meta_data = load(example, use_caption)
210 try:
211 # Load task.
212 logger.info(f"[Intent]: {intent}")
213
214 try:
215 print("=" * 30)
216 print("Agent: Thinking...")
217 action = agent.next_action(
218 trajectory, intent, images=images, meta_data=meta_data, output_response=True
219 )
220 except ValueError as e:
221 # get the error message
222 action = create_stop_action(f"ERROR: {str(e)}")
223
224 print("--- Raw prediction ---")
225 print(action["raw_prediction"])
226
227 pattern = r"\```(.+?)```"
228 match = re.search(pattern, action["raw_prediction"], re.DOTALL)
229 if match:
230 content = match.group(1)
231 content = content.strip()
232 else:
233 content = action["raw_prediction"]
234 # print(content)
235 all_actions[example["id"]] = content
236
237 except openai.OpenAIError as e:
238 logger.info(f"[OpenAI Error] {repr(e)}")
239 except Exception as e:
240 logger.info(f"[Unhandled Error] {repr(e)}]")
241 import traceback
242
243 # write to error file
244 with open(Path(args.result_dir) / "error.txt", "a") as f:
245 f.write(f"[Unhandled Error] {repr(e)}\n")
246 f.write(traceback.format_exc()) # write stack trace to file
247
248 if "gemini" in args.model:
249 print("Sleeping for 11 seconds...")
250 time.sleep(11)
251 elif "claude" in args.model:
252 print("Sleeping for 5 seconds...")
253 time.sleep(5)
254
255 result_path = Path(args.result_dir) / f"actions_{'caption' if use_caption else 'no_caption'}.json"

Callers 1

eval_step.pyFile · 0.70

Calls 1

loadFunction · 0.70

Tested by

no test coverage detected