Skip to content

Commit 208fe92

Browse files
authored
add crewai tool example (#10)
* renames input to cell in the python SDK. * bump python version to 0.0.2.
1 parent 93699ab commit 208fe92

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

python/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "sandboxai-client"
7-
version = "0.0.1"
7+
version = "0.0.2"
88
description = "Secure sandboxes for AI agents and LLMs."
99

1010
requires-python = ">=3.10"
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from crewai.tools import BaseTool
2+
from typing import Type
3+
from pydantic import BaseModel, Field
4+
from sandboxai import Sandbox
5+
6+
7+
class SandboxIPythonToolArgs(BaseModel):
8+
code: str = Field(..., description="The code to execute in the ipython cell.")
9+
10+
11+
class SandboxIPythonTool(BaseTool):
12+
name: str = "Run Python code"
13+
description: str = "Run python code and shell commands in an ipython cell. Shell commands should be on a new line and start with a '!'."
14+
args_schema: Type[BaseModel] = SandboxIPythonToolArgs
15+
16+
def __init__(self, *args, **kwargs):
17+
super().__init__(*args, **kwargs)
18+
# Note that the sandbox only shuts down once the Python program exits.
19+
self._sandbox = Sandbox(embedded=True)
20+
21+
def _run(self, code: str) -> str:
22+
result = self._sandbox.run_ipython_cell(code=code)
23+
return result.output
24+
25+
26+
class SandboxShellToolArgs(BaseModel):
27+
command: str = Field(..., description="The bash commands to execute.")
28+
29+
30+
class SandboxShellTool(BaseTool):
31+
name: str = "Run shell command"
32+
description: str = "Run bash shell commands in a sandbox."
33+
args_schema: Type[BaseModel] = SandboxShellToolArgs
34+
35+
def __init__(self, *args, **kwargs):
36+
super().__init__(*args, **kwargs)
37+
# Note that the sandbox only shuts down once the Python program exits.
38+
self._sandbox = Sandbox(embedded=True)
39+
40+
def _run(self, command: str) -> str:
41+
result = self._sandbox.run_shell_command(command=command)
42+
return result.output

python/sandboxai/sandbox.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ def delete(self) -> None:
8686
self.name = ""
8787
self.image = ""
8888

89-
def run_ipython_cell(self, input: str) -> IPythonCellResult:
89+
def run_ipython_cell(self, code: str) -> IPythonCellResult:
9090
"""
9191
Runs an ipython cell in the sandbox.
92+
93+
@param code: The code to run in the sandbox.
9294
"""
9395
if not self.name:
9496
self.create()
@@ -97,7 +99,7 @@ def run_ipython_cell(self, input: str) -> IPythonCellResult:
9799
result = self.client.run_ipython_cell(
98100
self.space,
99101
self.name,
100-
v1Api.RunIPythonCellRequest(code=input, split_output=False),
102+
v1Api.RunIPythonCellRequest(code=code, split_output=False),
101103
) # type: ignore
102104
log.debug(f"IPython cell returned the output: {result.output}")
103105
result = IPythonCellResult(output=result.output or "")

0 commit comments

Comments
 (0)