diff --git a/pyproject.toml b/pyproject.toml index 3f49945..cf39b35 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -99,6 +99,9 @@ lint.select = ["E", "F", "I", "W"] known-first-party = ["smolagents"] lines-after-imports = 2 +[tool.setuptools.package-data] +"smolagents.prompts" = ["*.yaml"] + [project.scripts] smolagent = "smolagents.cli:main" webagent = "smolagents.vision_web_browser:main" \ No newline at end of file diff --git a/src/smolagents/__init__.py b/src/smolagents/__init__.py index 37a545c..a2aa137 100644 --- a/src/smolagents/__init__.py +++ b/src/smolagents/__init__.py @@ -25,7 +25,6 @@ from .local_python_executor import * from .memory import * from .models import * from .monitoring import * -from .prompts import * from .tools import * from .utils import * from .cli import * diff --git a/src/smolagents/agents.py b/src/smolagents/agents.py index 6fbbcc7..9366950 100644 --- a/src/smolagents/agents.py +++ b/src/smolagents/agents.py @@ -14,8 +14,8 @@ # 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 importlib.resources import inspect -import os import re import textwrap import time @@ -646,9 +646,9 @@ class ToolCallingAgent(MultiStepAgent): planning_interval: Optional[int] = None, **kwargs, ): - yaml_path = os.path.join(os.path.dirname(__file__), "prompts", "toolcalling_agent.yaml") - with open(yaml_path, "r") as f: - self.prompt_templates = yaml.safe_load(f) + self.prompt_templates = yaml.safe_load( + importlib.resources.read_text("smolagents.prompts", "toolcalling_agent.yaml") + ) super().__init__( tools=tools, model=model, @@ -779,9 +779,7 @@ class CodeAgent(MultiStepAgent): ): self.additional_authorized_imports = additional_authorized_imports if additional_authorized_imports else [] self.authorized_imports = list(set(BASE_BUILTIN_MODULES) | set(self.additional_authorized_imports)) - yaml_path = os.path.join(os.path.dirname(__file__), "prompts", "code_agent.yaml") - with open(yaml_path, "r") as f: - self.prompt_templates = yaml.safe_load(f) + self.prompt_templates = yaml.safe_load(importlib.resources.read_text("smolagents.prompts", "code_agent.yaml")) super().__init__( tools=tools, model=model, diff --git a/tests/test_import.py b/tests/test_import.py index 3253b6d..aaa284d 100644 --- a/tests/test_import.py +++ b/tests/test_import.py @@ -4,7 +4,7 @@ import subprocess def test_import_smolagents_without_extras(): # Run the import statement in an isolated virtual environment result = subprocess.run( - ["uv", "run", "--isolated", "-"], input="import smolagents", text=True, capture_output=True + ["uv", "run", "--isolated", "--no-editable", "-"], input="import smolagents", text=True, capture_output=True ) # Check if the import was successful assert result.returncode == 0, (