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]

Add function_calling support for run-replay

carlosejimenez3ad17ef0e1e1
diff --git a/sweagent/agent/models.py b/sweagent/agent/models.py
index edb8421d..79543464 100644
--- a/sweagent/agent/models.py
+++ b/sweagent/agent/models.py
@@ -284,6 +284,8 @@ class ReplayModel(AbstractModel):
]
self._replay_idx = 0
self._action_idx = 0
+ self.use_function_calling = tools.use_function_calling
+ self.submit_command = tools.submit_command
def _next_replay(self) -> None:
"""Called after last action"""
@@ -297,20 +299,35 @@ class ReplayModel(AbstractModel):
try:
action = actions[self._action_idx]
except IndexError:
- msg = (
- "This seems to be an incomplete trajectory. "
- "We reached the end of it, but `submit` was not called. "
- "Calling it now."
- )
- logger.warning(msg)
- action = "```\nsubmit\n```"
+ # log error
+ logger.error("Reached end of replay trajectory without submitting. Submitting now.")
+ if self.use_function_calling:
+ action = {
+ "message": f"Calling `{self.submit_command}` to submit.",
+ "tool_calls": [
+ {
+ "type": "function",
+ "id": "call_submit",
+ "function": {
+ "name": self.submit_command,
+ "arguments": "{}",
+ },
+ }
+ ],
+ }
+ else:
+ action = f"```\n{self.submit_command}\n```"
self._action_idx += 1
# Assuming `submit` is always last action of replay trajectory
- if action == "submit":
+ if isinstance(action, str) and action == "submit":
self._next_replay()
+ return {"message": action}
+ # Handle both dict and string actions
+ if isinstance(action, dict):
+ return action
return {"message": action}
diff --git a/sweagent/run/run_replay.py b/sweagent/run/run_replay.py
index 58e2a769..9fc99767 100644
--- a/sweagent/run/run_replay.py
+++ b/sweagent/run/run_replay.py
@@ -3,7 +3,7 @@
import json
import sys
import tempfile
-from getpass import getpass
+from getpass import getuser
from pathlib import Path
from typing import Any
@@ -37,7 +37,7 @@ class RunReplayConfig(BaseSettings):
def model_post_init(self, __context: Any) -> None:
if self.output_dir == Path("DEFAULT"):
- user_id = getpass.getuser()
+ user_id = getuser()
self.output_dir = (
Path.cwd() / "trajectories" / user_id / f"{self.config_path.name}__replay___{self.traj_path.stem}"
)
@@ -85,7 +85,39 @@ class RunReplay:
def _create_actions_file(self) -> None:
traj_path = Path(self.traj_path)
traj_data = json.loads(traj_path.read_text())
- actions = [x["content"] for x in traj_data["history"] if x["role"] == "assistant"]
+
+ # Verify config compatibility with tool calls
+ has_tool_calls = any(
+ "tool_calls" in item and item["tool_calls"] is not None
+ for item in traj_data["history"]
+ if item["role"] == "assistant"
+ )
+ agent_config = yaml.safe_load(Path(self.config_path).read_text())["agent"]
+ parse_function = agent_config.get("tools", {}).get("parse_function", {}).get("type")
+ use_function_calling = parse_function == "function_calling"
+
+ if has_tool_calls and not use_function_calling:
+ msg = (
+ "Trajectory contains tool calls but config is not set up for function calling. "
+ "Check that the config you want to use has agent.tools.parse_function.type set to 'function_calling'."
+ )
+ raise ValueError(msg)
+ actions = []
+ for ix, item in enumerate(traj_data["history"]):
+ if item["role"] != "assistant":
+ continue
+ action = {"message": item["content"]}
+ if use_function_calling:
+ assert "tool_calls" in item and item["tool_calls"] is not None, (
+ f"Config is set to use `function_calling` but trajectory item {ix} is missing a tool call "
+ f"or has tool_calls set to None"
+ )
+ action["tool_calls"] = item["tool_calls"]
+ actions.append(action)
+ if len(actions) == 0:
+ msg = "No actions found in trajectory"
+ raise ValueError(msg)
+
self._replay_action_trajs_path.write_text(json.dumps({self.instance_id: actions}))
def _get_env(self) -> SWEEnv: