-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
briandevvn
wants to merge
1
commit into
huggingface:main
Choose a base branch
from
briandevvn:improve_tool_call
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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): | ||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Catch a concrete exception class, not general |
||
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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can write:
The
not model_message.tool_calls
covers bothNone
and empty list cases.You should
strip
content
to cover the case thatcontent
has some data but only whitespaces.