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.pyindex 9829dec8..52081cf4 100644--- a/sweagent/agent/models.py+++ b/sweagent/agent/models.py@@ -2,6 +2,7 @@ from __future__ import annotationsimport jsonimport random+import shleximport timefrom abc import ABC, abstractmethodfrom pathlib import Path@@ -21,6 +22,7 @@ from tenacity import ()from sweagent import REPO_ROOT+from sweagent.tools.parsing import FunctionCallingFormatErrorfrom sweagent.tools.tools import ToolConfigfrom sweagent.types import History, HistoryItemfrom 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: ignoreclass HumanModel(AbstractModel):diff --git a/sweagent/tools/parsing.py b/sweagent/tools/parsing.pyindex 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 = messageself.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.pyindex 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_namediff --git a/tests/test_parsing.py b/tests/test_parsing.pyindex 9ddf4328..e9b6579a 100644--- a/tests/test_parsing.py+++ b/tests/test_parsing.py@@ -1,12 +1,14 @@from __future__ import annotationsimport pytest+from jinja2 import Templatefrom sweagent.tools.commands import Commandfrom 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)