Skip to content

SWE-agent takes a GitHub issue and tries to automatically fix it, using your LM of choice. It can also be employed for offensive cybersecurity or competitive coding challenges. [NeurIPS 2024]

Feat: Add CL util to merge preds files

Kilian Lieret81d6beaa1774
diff --git a/sweagent/run/merge_predictions.py b/sweagent/run/merge_predictions.py
new file mode 100644
index 00000000..73869113
--- /dev/null
+++ b/sweagent/run/merge_predictions.py
@@ -0,0 +1,52 @@
+import argparse
+import json
+from pathlib import Path
+
+from sweagent.utils.log import get_logger
+
+"""Merge multiple predictions into a single file."""
+
+
+logger = get_logger("merge", emoji="➕")
+
+
+def merge_predictions(directory: Path, output: Path | None = None) -> None:
+ """Merge predictions found in `directory` into a single JSON file.
+
+ Args:
+ directory: Directory containing predictions.
+ output: Output file. If not provided, the merged predictions will be
+ written to `directory/preds.json`.
+ """
+ preds = list(directory.glob("*.pred"))
+ logger.info("Found %d predictions", len(preds))
+ if not preds:
+ logger.warning("No predictions found in %s", directory)
+ return
+ if output is None:
+ output = directory / "preds.json"
+ data = {}
+ for pred in preds:
+ _data = json.loads(pred.read_text())
+ instance_id = _data["instance_id"]
+ data[instance_id] = _data
+ output.parent.mkdir(parents=True, exist_ok=True)
+ output.write_text(json.dumps(data))
+ logger.info("Wrote merged predictions to %s", output)
+
+
+def get_cli_parser() -> argparse.ArgumentParser:
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument("directory", type=Path, help="Directory containing predictions")
+ parser.add_argument("--output", type=Path, help="Output file")
+ return parser
+
+
+def run_from_cli(args: list[str] | None = None) -> None:
+ cli_parser = get_cli_parser()
+ cli_args = cli_parser.parse_args(args)
+ merge_predictions(cli_args.directory, cli_args.output)
+
+
+if __name__ == "__main__":
+ run_from_cli()
diff --git a/sweagent/run/run.py b/sweagent/run/run.py
index 1b3f2c5a..ac79f883 100644
--- a/sweagent/run/run.py
+++ b/sweagent/run/run.py
@@ -8,7 +8,9 @@ import sys
def get_cli():
parser = argparse.ArgumentParser(add_help=False)
- parser.add_argument("command", choices=["run", "run-batch", "run-replay", "traj-to-demo", "run-api"], nargs="?")
+ parser.add_argument(
+ "command", choices=["run", "run-batch", "run-replay", "traj-to-demo", "run-api", "merge-preds"], nargs="?"
+ )
parser.add_argument("-h", "--help", action="store_true", help="Show this help message and exit")
return parser
@@ -52,6 +54,10 @@ def main(args: list[str] | None = None):
from sweagent.api.server import run_from_cli as run_api_main
run_api_main(remaining_args)
+ elif command == "merge-preds":
+ from sweagent.run.merge_predictions import run_from_cli as merge_predictions_main
+
+ merge_predictions_main(remaining_args)
else:
msg = f"Unknown command: {command}"
raise ValueError(msg)
diff --git a/sweagent/run/run_batch.py b/sweagent/run/run_batch.py
index 4d1ab867..4d4ecc14 100644
--- a/sweagent/run/run_batch.py
+++ b/sweagent/run/run_batch.py
@@ -28,6 +28,7 @@ from sweagent.run.batch_instances import BatchInstance, BatchInstanceSourceConfi
from sweagent.run.common import BasicCLI, save_predictions
from sweagent.run.hooks.abstract import CombinedRunHooks, RunHook
from sweagent.run.hooks.apply_patch import SaveApplyPatchHook
+from sweagent.run.merge_predictions import merge_predictions
from sweagent.run.run_single import RunSingleConfig
from sweagent.types import AgentRunResult
from sweagent.utils.config import load_environment_variables
@@ -160,6 +161,8 @@ class RunBatch:
else:
self.main_multi_worker()
+ merge_predictions(self.output_dir)
+
self._chooks.on_end()
def main_single_worker(self) -> None: