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]

Improve function calling error template

Kilian Lieretbc85bf540e4d
diff --git a/sweagent/agent/models.py b/sweagent/agent/models.py
index 9829dec8..52081cf4 100644
--- a/sweagent/agent/models.py
+++ b/sweagent/agent/models.py
@@ -2,6 +2,7 @@ from __future__ import annotations
import json
import random
+import shlex
import time
from abc import ABC, abstractmethod
from pathlib import Path
@@ -21,6 +22,7 @@ from tenacity import (
)
from sweagent import REPO_ROOT
+from sweagent.tools.parsing import FunctionCallingFormatError
from sweagent.tools.tools import ToolConfig
from sweagent.types import History, HistoryItem
from sweagent.utils.log import get_logger
@@ -194,6 +196,13 @@ def _handle_raise_commands(action: str) -> None:
raise CostLimitExceededError()
elif action == "raise_context":
raise ContextWindowExceededError()
+ elif action.startswith("raise_function_calling"):
+ parts = shlex.split(action)
+ error_code = parts[1]
+ if len(parts) == 3:
+ error_message = parts[2]
+ assert len(parts) < 4
+ raise FunctionCallingFormatError(error_message, error_code) # type: ignore
class HumanModel(AbstractModel):
diff --git a/sweagent/tools/parsing.py b/sweagent/tools/parsing.py
index 778207c3..de06fd34 100644
--- a/sweagent/tools/parsing.py
+++ b/sweagent/tools/parsing.py
@@ -29,7 +29,8 @@ class FunctionCallingFormatError(FormatError):
],
**extra_info: Any,
):
- super().__init__(message + f"[error_code={error_code}]")
+ super().__init__(message + f" [error_code={error_code}]")
+ self.message = message
self.extra_info = {"error_code": error_code, **extra_info}
@@ -240,13 +241,23 @@ class Identity(AbstractParseFunction, BaseModel):
class FunctionCallingParser(AbstractParseFunction, BaseModel):
"""Expects the model response to be a LiteLLM tool call."""
- error_message: str = """\
- Your action could not be parsed properly.
+ error_message: str = """{% if error_code == "missing" %}
+ Your last output did not use any tool calls!
+ Please make sure your output includes exactly _ONE_ function call!
+ You must invoke the function directly using the function call format.
+ You cannot invoke commands using with ```, you have to use the function call format.
+ If you think you have already resolved the issue, please submit your changes by running the `submit` command.
+ If you think you cannot solve the problem, please run `exit_forfeit` (if available).
+ Else, please continue with a new tool call!
+ {% elif error_code == "multiple" %}
+ Your last output included multiple tool calls!
Please make sure your output includes a thought and exactly _ONE_ function call.
-
+ {% elif error_code == "unexpected_arg" %}
+ Your action could not be parsed properly: {{exception_message}}.
Make sure your function call doesn't include any extra arguments that are not in the allowed arguments, and only use the allowed commands.
-
- You must invoke the function directly using the function call format. You cannot invoke commands using with ```, you have to use the function call format.
+ {% else %}
+ Your action could not be parsed properly: {{exception_message}}.
+ {% endif %}
"""
type: Literal["function_calling"] = "function_calling"
diff --git a/sweagent/tools/tools.py b/sweagent/tools/tools.py
index 7e67a117..05a6fb8a 100644
--- a/sweagent/tools/tools.py
+++ b/sweagent/tools/tools.py
@@ -156,7 +156,6 @@ class ToolConfig(BaseModel):
)
if self.format_error_template is None:
self.format_error_template = self.parse_function.format_error_template
- self.format_error_template = self.format_error_template.format(**self.__dict__)
for command in commands:
if command.name == self.submit_command:
self.submit_command_end_name = command.end_name
diff --git a/tests/test_parsing.py b/tests/test_parsing.py
index 9ddf4328..e9b6579a 100644
--- a/tests/test_parsing.py
+++ b/tests/test_parsing.py
@@ -1,12 +1,14 @@
from __future__ import annotations
import pytest
+from jinja2 import Template
from sweagent.tools.commands import Command
from sweagent.tools.parsing import (
ActionParser,
EditFormat,
FormatError,
+ FunctionCallingFormatError,
FunctionCallingParser,
Identity,
JsonParser,
@@ -122,3 +124,9 @@ def test_function_calling_parser():
}
with pytest.raises(FormatError):
parser(invalid_json, [command])
+
+
+def test_function_calling_parser_error_message():
+ template = Template(FunctionCallingParser().error_message)
+ exc1 = FunctionCallingFormatError("test", "missing")
+ assert "did not use any tool calls" in template.render(**exc1.extra_info, exception_message=exc1.message)