Skip to content

Improve tool call by re-trying tool call with the model response content #1052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/smolagents/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from .agent_types import AgentAudio, AgentImage, AgentType, handle_agent_output_types
from .default_tools import TOOL_MAPPING, FinalAnswerTool
from .local_python_executor import BASE_BUILTIN_MODULES, LocalPythonExecutor, PythonExecutor, fix_final_answer_code
from .memory import ActionStep, AgentMemory, PlanningStep, SystemPromptStep, TaskStep, ToolCall
from .memory import ActionStep, AgentMemory, PlanningStep, SystemPromptStep, TaskStep, ToolCall, Message
from .models import (
ChatMessage,
MessageRole,
Expand Down Expand Up @@ -1019,6 +1019,44 @@ def step(self, memory_step: ActionStep) -> Union[None, Any]:
level=LogLevel.DEBUG,
)

# Re-try if the model did not call any tools
if (model_message.content != '') and (model_message.tool_calls is None or len(model_message.tool_calls) == 0):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can write:

if (model_message.content.strip() != '') and not model_message.tool_calls:
    ...

The not model_message.tool_calls covers both None and empty list cases.

You should strip content to cover the case that content has some data but only whitespaces.

self.logger.log_markdown(
content=model_message.content,
title="Model did not call any tools. Retrying with the model response content...",
level=LogLevel.DEBUG,
)

retry_messages = [
Message(
role=MessageRole.SYSTEM,
content=[
{
"type": "text",
"text": "You are an expert assistant who finds the tools in the text provided using tool calls. You must use the tools provided to you to solve the task otherwise you will fail."
}
]
),
Message(
role=MessageRole.USER,
content=[
{
"type": "text",
"text": f"{model_message.content}"
}
]
),
]
try:
model_message: ChatMessage = self.model(
retry_messages,
tools_to_call_from=list(self.tools.values()),
stop_sequences=["Observation:", "Calling tools:"],
)
memory_step.model_output_message = model_message
except Exception as e:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catch a concrete exception class, not general Exception class.

raise AgentGenerationError(f"Error in generating tool call with model:\n{e}", self.logger) from e

if model_message.tool_calls is None or len(model_message.tool_calls) == 0:
raise AgentParsingError(
"Model did not call any tools. Call `final_answer` tool to return a final answer.", self.logger
Expand Down