Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
46 changes: 46 additions & 0 deletions python/sandboxai/integrations/crewai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from crewai.tools import BaseTool
from typing import Type
from pydantic import BaseModel, Field
from sandboxai import Sandbox


class RunIPythonCellArgs(BaseModel):
code: str = Field(..., description="The code to execute in the ipython cell.")


class RunIPythonCell(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] = RunIPythonCellArgs

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._sandbox = Sandbox(embedded=True)

def __del__(self):
self._sandbox.delete()

def _run(self, code: str) -> str:
result = self._sandbox.run_ipython_cell(code=code)
return result.output


class RunShellCommandArgs(BaseModel):
command: str = Field(..., description="The bash commands to execute.")


class RunShellCommand(BaseTool):
name: str = "Run shell command"
description: str = "Run bash shell commands in a sandbox."
args_schema: Type[BaseModel] = RunShellCommandArgs

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._sandbox = Sandbox(embedded=True)

def __del__(self):
self._sandbox.delete()

def _run(self, command: str) -> str:
result = self._sandbox.run_shell_command(command=command)
return result.output
6 changes: 4 additions & 2 deletions python/sandboxai/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 "")
Expand Down
Loading