|
| 1 | +import re |
| 2 | +import argparse |
| 3 | +import logging |
| 4 | +from pathlib import Path |
| 5 | +from dotenv import load_dotenv |
| 6 | + |
| 7 | +from agent_as_a_judge.agent import JudgeAgent |
| 8 | +from agent_as_a_judge.config import AgentConfig |
| 9 | + |
| 10 | + |
| 11 | +def main(agent_config: AgentConfig, logger: logging.Logger): |
| 12 | + |
| 13 | + def extract_number_from_filename(filename: str) -> int: |
| 14 | + match = re.search(r"(\d+)", filename) |
| 15 | + return int(match.group(1)) if match else float("inf") |
| 16 | + |
| 17 | + instance_files = sorted( |
| 18 | + list(agent_config.instance_dir.glob("*.json")), |
| 19 | + key=lambda f: extract_number_from_filename(f.stem), |
| 20 | + ) |
| 21 | + |
| 22 | + logger.info(f"Total instances found: {len(instance_files)}") |
| 23 | + |
| 24 | + for instance_file in instance_files: |
| 25 | + instance_name = instance_file.stem |
| 26 | + |
| 27 | + trajectory_file = None |
| 28 | + if agent_config.trajectory_file: |
| 29 | + trajectory_file = agent_config.trajectory_file / f"{instance_name}.json" |
| 30 | + |
| 31 | + judgment_file = agent_config.judge_dir / instance_file.name |
| 32 | + |
| 33 | + if judgment_file.exists(): |
| 34 | + logger.info( |
| 35 | + f"Judgment for instance '{instance_name}' already exists. Skipping..." |
| 36 | + ) |
| 37 | + continue |
| 38 | + |
| 39 | + if trajectory_file and trajectory_file.exists(): |
| 40 | + logger.info( |
| 41 | + f"Processing instance: {instance_file} with trajectory: {trajectory_file}" |
| 42 | + ) |
| 43 | + else: |
| 44 | + logger.warning( |
| 45 | + f"Trajectory file not found for instance: {instance_file}, processing without it" |
| 46 | + ) |
| 47 | + trajectory_file = None |
| 48 | + |
| 49 | + workspace = agent_config.workspace_dir / instance_name |
| 50 | + |
| 51 | + judge_agent = JudgeAgent( |
| 52 | + workspace=workspace, |
| 53 | + instance=instance_file, |
| 54 | + judge_dir=agent_config.judge_dir, |
| 55 | + trajectory_file=trajectory_file, |
| 56 | + config=agent_config, |
| 57 | + ) |
| 58 | + judge_agent.judge_anything() |
| 59 | + |
| 60 | + |
| 61 | +def parse_arguments(): |
| 62 | + parser = argparse.ArgumentParser() |
| 63 | + |
| 64 | + parser.add_argument( |
| 65 | + "--developer_agent", type=str, required=True, help="Name of the developer agent" |
| 66 | + ) |
| 67 | + parser.add_argument( |
| 68 | + "--setting", |
| 69 | + type=str, |
| 70 | + required=True, |
| 71 | + help="Setting for the JudgeAgent (e.g., gray_box, black_box)", |
| 72 | + ) |
| 73 | + parser.add_argument( |
| 74 | + "--planning", |
| 75 | + type=str, |
| 76 | + required=True, |
| 77 | + choices=["planning", "comprehensive (no planning)", "efficient (no planning)"], |
| 78 | + help="Module to run", |
| 79 | + ) |
| 80 | + parser.add_argument( |
| 81 | + "--benchmark_dir", |
| 82 | + type=str, |
| 83 | + required=True, |
| 84 | + help="Base directory for the DevAI benchmark", |
| 85 | + ) |
| 86 | + parser.add_argument( |
| 87 | + "--include_dirs", |
| 88 | + nargs="+", |
| 89 | + default=["src", "results", "models", "data"], |
| 90 | + help="Directories to include in search", |
| 91 | + ) |
| 92 | + parser.add_argument( |
| 93 | + "--exclude_dirs", |
| 94 | + nargs="+", |
| 95 | + default=[ |
| 96 | + "__pycache__", |
| 97 | + "env", |
| 98 | + ".git", |
| 99 | + "venv", |
| 100 | + "logs", |
| 101 | + "output", |
| 102 | + "tmp", |
| 103 | + "temp", |
| 104 | + "cache", |
| 105 | + "data", |
| 106 | + ], |
| 107 | + help="Directories to exclude in search", |
| 108 | + ) |
| 109 | + parser.add_argument( |
| 110 | + "--exclude_files", |
| 111 | + nargs="+", |
| 112 | + default=[".DS_Store"], |
| 113 | + help="Files to exclude in search", |
| 114 | + ) |
| 115 | + parser.add_argument( |
| 116 | + "--trajectory_file", |
| 117 | + type=str, |
| 118 | + help="Path to the trajectory directory, if available", |
| 119 | + ) |
| 120 | + |
| 121 | + return parser.parse_args() |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + load_dotenv() |
| 126 | + |
| 127 | + logger = logging.getLogger(__name__) |
| 128 | + logging.basicConfig(level=logging.INFO) |
| 129 | + args = parse_arguments() |
| 130 | + |
| 131 | + benchmark_dir = Path(args.benchmark_dir) |
| 132 | + instance_dir = benchmark_dir / "devai/instances" |
| 133 | + workspace_dir = benchmark_dir / f"workspaces/{args.developer_agent}" |
| 134 | + judge_dir = ( |
| 135 | + benchmark_dir |
| 136 | + / f"judgment/{args.developer_agent}/agent_as_a_judge/{args.setting}" |
| 137 | + ) |
| 138 | + trajectory_file = benchmark_dir / f"trajectories/{args.developer_agent}" |
| 139 | + |
| 140 | + agent_config = AgentConfig( |
| 141 | + include_dirs=args.include_dirs, |
| 142 | + exclude_dirs=args.exclude_dirs, |
| 143 | + exclude_files=args.exclude_files, |
| 144 | + setting=args.setting, |
| 145 | + planning=args.planning, |
| 146 | + judge_dir=judge_dir, |
| 147 | + workspace_dir=workspace_dir, |
| 148 | + instance_dir=instance_dir, |
| 149 | + trajectory_file=trajectory_file, |
| 150 | + ) |
| 151 | + |
| 152 | + main( |
| 153 | + agent_config=agent_config, |
| 154 | + logger=logger, |
| 155 | + ) |
0 commit comments