Skip to content
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

Fix final_answer issue in e2b_executor #319

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions src/smolagents/e2b_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
import base64
import pickle
import textwrap
Expand Down Expand Up @@ -45,6 +46,8 @@ def __init__(self, additional_imports: List[str], tools: List[Tool], logger):
)

self.custom_tools = {}
self.final_answer = False
self.final_answer_pattern = re.compile(r'^final_answer\((.*)\)$')
self.sbx = Sandbox() # "qywp2ctmu2q7jzprcf4j")
# TODO: validate installing agents package or not
# print("Installing agents package on remote executor...")
Expand Down Expand Up @@ -85,6 +88,8 @@ def forward(self, *args, **kwargs):
self.logger.log(tool_definition_execution.logs)

def run_code_raise_errors(self, code: str):
if self.final_answer_pattern.match(code):
self.final_answer = True
execution = self.sbx.run_code(
code,
)
Expand Down Expand Up @@ -122,7 +127,7 @@ def __call__(self, code_action: str, additional_args: dict) -> Tuple[Any, Any]:
execution = self.run_code_raise_errors(code_action)
execution_logs = "\n".join([str(log) for log in execution.logs.stdout])
if not execution.results:
return None, execution_logs
return None, execution_logs, self.final_answer
else:
for result in execution.results:
if result.is_main_result:
Expand All @@ -144,7 +149,7 @@ def __call__(self, code_action: str, additional_args: dict) -> Tuple[Any, Any]:
"text",
]:
if getattr(result, attribute_name) is not None:
return getattr(result, attribute_name), execution_logs
return getattr(result, attribute_name), execution_logs, self.final_answer
raise ValueError("No main result returned by executor!")


Expand Down
4 changes: 4 additions & 0 deletions src/smolagents/tool_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def __init__(self, class_attributes: Set[str], check_imports: bool = True):
self.class_attributes = class_attributes
self.errors = []
self.check_imports = check_imports
self.typing_names = {
'Any'
}

def visit_arguments(self, node):
"""Collect function arguments"""
Expand Down Expand Up @@ -97,6 +100,7 @@ def visit_Name(self, node):
or node.id in self.imports
or node.id in self.from_imports
or node.id in self.assigned_names
or node.id in self.typing_names
Copy link
Collaborator

Choose a reason for hiding this comment

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

@keetrap is this fixing any failing tool initialization? Normally, having Any in tool arguments is supported and tested in tests/test_tools.py::test_tool_supports_any_none, tell me if you have a counterexample!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aymeric-roucher
Yes, this fix the FinalanswerTool without this code the error is:

ValueError: Tool validation failed:
- forward: Name 'Any' is undefined.

This error only occurs when use_e2b_executor=True

Copy link
Collaborator

Choose a reason for hiding this comment

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

Then this is due to calling method Tool.save! Could you add this line in test_tool_supports_any_none (in tests/test_tools.py):
get_weather.save()
This may force you to add None as well to self.typing_names

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added the line and ran the tests. The error was: TypeError: Tool.save() missing 1 required positional argument: 'output_dir'. After providing the output_dir, the tests passed without adding None to self.typing_names.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am still confused as to why there is an error, and this only occurs when use_e2b_executor=True. It is not occurring in the local Python executor because we are not validating the tool attributes there.

Thanks for merging this PR.

):
self.errors.append(f"Name '{node.id}' is undefined.")

Expand Down
Loading