diff --git a/python/pyproject.toml b/python/pyproject.toml index 5c651e2..53420a3 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "sandboxai-client" -version = "0.0.1" +version = "0.0.2" description = "Secure sandboxes for AI agents and LLMs." requires-python = ">=3.10" diff --git a/python/sandboxai/experimental/crewai.py b/python/sandboxai/experimental/crewai.py new file mode 100644 index 0000000..fa484dd --- /dev/null +++ b/python/sandboxai/experimental/crewai.py @@ -0,0 +1,42 @@ +from crewai.tools import BaseTool +from typing import Type +from pydantic import BaseModel, Field +from sandboxai import Sandbox + + +class SandboxIPythonToolArgs(BaseModel): + code: str = Field(..., description="The code to execute in the ipython cell.") + + +class SandboxIPythonTool(BaseTool): + name: str = "Run Python code" + description: str = "Run python code and shell commands in an ipython cell. Shell commands should be on a new line and start with a '!'." + args_schema: Type[BaseModel] = SandboxIPythonToolArgs + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + # Note that the sandbox only shuts down once the Python program exits. + self._sandbox = Sandbox(embedded=True) + + def _run(self, code: str) -> str: + result = self._sandbox.run_ipython_cell(code=code) + return result.output + + +class SandboxShellToolArgs(BaseModel): + command: str = Field(..., description="The bash commands to execute.") + + +class SandboxShellTool(BaseTool): + name: str = "Run shell command" + description: str = "Run bash shell commands in a sandbox." + args_schema: Type[BaseModel] = SandboxShellToolArgs + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + # Note that the sandbox only shuts down once the Python program exits. + self._sandbox = Sandbox(embedded=True) + + def _run(self, command: str) -> str: + result = self._sandbox.run_shell_command(command=command) + return result.output diff --git a/python/sandboxai/sandbox.py b/python/sandboxai/sandbox.py index 87e8d53..e56290b 100644 --- a/python/sandboxai/sandbox.py +++ b/python/sandboxai/sandbox.py @@ -86,9 +86,11 @@ def delete(self) -> None: self.name = "" self.image = "" - def run_ipython_cell(self, input: str) -> IPythonCellResult: + def run_ipython_cell(self, code: str) -> IPythonCellResult: """ Runs an ipython cell in the sandbox. + + @param code: The code to run in the sandbox. """ if not self.name: self.create() @@ -97,7 +99,7 @@ def run_ipython_cell(self, input: str) -> IPythonCellResult: result = self.client.run_ipython_cell( self.space, self.name, - v1Api.RunIPythonCellRequest(code=input, split_output=False), + v1Api.RunIPythonCellRequest(code=code, split_output=False), ) # type: ignore log.debug(f"IPython cell returned the output: {result.output}") result = IPythonCellResult(output=result.output or "")