# coding=utf-8 # Copyright 2024 HuggingFace Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # 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 unittest import pytest from smolagents.default_tools import PythonInterpreterTool, VisitWebpageTool from smolagents.types import AGENT_TYPE_MAPPING from .test_tools import ToolTesterMixin class DefaultToolTests(unittest.TestCase): def test_visit_webpage(self): arguments = { "url": "https://en.wikipedia.org/wiki/United_States_Secretary_of_Homeland_Security" } result = VisitWebpageTool()(arguments) assert isinstance(result, str) assert ( "* [About Wikipedia](/wiki/Wikipedia:About)" in result ) # Proper wikipedia pages have an About class PythonInterpreterToolTester(unittest.TestCase, ToolTesterMixin): def setUp(self): self.tool = PythonInterpreterTool(authorized_imports=["numpy"]) self.tool.setup() def test_exact_match_arg(self): result = self.tool("(2 / 2) * 4") self.assertEqual(result, "Stdout:\n\nOutput: 4.0") def test_exact_match_kwarg(self): result = self.tool(code="(2 / 2) * 4") self.assertEqual(result, "Stdout:\n\nOutput: 4.0") def test_agent_type_output(self): inputs = ["2 * 2"] output = self.tool(*inputs, sanitize_inputs_outputs=True) output_type = AGENT_TYPE_MAPPING[self.tool.output_type] self.assertTrue(isinstance(output, output_type)) def test_agent_types_inputs(self): inputs = ["2 * 2"] _inputs = [] for _input, expected_input in zip(inputs, self.tool.inputs.values()): input_type = expected_input["type"] if isinstance(input_type, list): _inputs.append( [ AGENT_TYPE_MAPPING[_input_type](_input) for _input_type in input_type ] ) else: _inputs.append(AGENT_TYPE_MAPPING[input_type](_input)) # Should not raise an error output = self.tool(*inputs, sanitize_inputs_outputs=True) output_type = AGENT_TYPE_MAPPING[self.tool.output_type] self.assertTrue(isinstance(output, output_type)) def test_imports_work(self): result = self.tool("import numpy as np") assert "import from numpy is not allowed" not in result.lower() def test_unauthorized_imports_fail(self): with pytest.raises(Exception) as e: self.tool("import sympy as sp") assert "sympy" in str(e).lower()