Skip to content

Commit 9ebf3aa

Browse files
authored
docs(CodeInterpreterTool): update docs (#2675)
1 parent 2e4c976 commit 9ebf3aa

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

docs/tools/codeinterpretertool.mdx

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,29 @@ icon: code-simple
88

99
## Description
1010

11-
The `CodeInterpreterTool` enables CrewAI agents to execute Python 3 code that they generate autonomously. The code is run in a secure, isolated Docker container, ensuring safety regardless of the content. This functionality is particularly valuable as it allows agents to create code, execute it, obtain the results, and utilize that information to inform subsequent decisions and actions.
11+
The `CodeInterpreterTool` enables CrewAI agents to execute Python 3 code that they generate autonomously. This functionality is particularly valuable as it allows agents to create code, execute it, obtain the results, and utilize that information to inform subsequent decisions and actions.
1212

13-
## Requirements
13+
There are several ways to use this tool:
14+
15+
### Docker Container (Recommended)
16+
17+
This is the primary option. The code runs in a secure, isolated Docker container, ensuring safety regardless of its content.
18+
Make sure Docker is installed and running on your system. If you don’t have it, you can install it from [here](https://docs.docker.com/get-docker/).
19+
20+
### Sandbox environment
21+
22+
If Docker is unavailable — either not installed or not accessible for any reason — the code will be executed in a restricted Python environment - called sandbox.
23+
This environment is very limited, with strict restrictions on many modules and built-in functions.
24+
25+
### Unsafe Execution
26+
27+
**NOT RECOMMENDED FOR PRODUCTION**
28+
This mode allows execution of any Python code, including dangerous calls to `sys, os..` and similar modules. [Check out](/tools/codeinterpretertool#enabling-unsafe-mode) how to enable this mode
29+
30+
## Logging
31+
32+
The `CodeInterpreterTool` logs the selected execution strategy to STDOUT
1433

15-
- Docker must be installed and running on your system. If you don't have it, you can install it from [here](https://docs.docker.com/get-docker/).
1634

1735
## Installation
1836

@@ -74,18 +92,32 @@ programmer_agent = Agent(
7492
)
7593
```
7694

95+
### Enabling `unsafe_mode`
96+
97+
```python Code
98+
from crewai_tools import CodeInterpreterTool
99+
100+
code = """
101+
import os
102+
os.system("ls -la")
103+
"""
104+
105+
CodeInterpreterTool(unsafe_mode=True).run(code=code)
106+
```
107+
77108
## Parameters
78109

79110
The `CodeInterpreterTool` accepts the following parameters during initialization:
80111

81112
- **user_dockerfile_path**: Optional. Path to a custom Dockerfile to use for the code interpreter container.
82113
- **user_docker_base_url**: Optional. URL to the Docker daemon to use for running the container.
83-
- **unsafe_mode**: Optional. Whether to run code directly on the host machine instead of in a Docker container. Default is `False`. Use with caution!
114+
- **unsafe_mode**: Optional. Whether to run code directly on the host machine instead of in a Docker container or sandbox. Default is `False`. Use with caution!
115+
- **default_image_tag**: Optional. Default Docker image tag. Default is `code-interpreter:latest`
84116

85117
When using the tool with an agent, the agent will need to provide:
86118

87119
- **code**: Required. The Python 3 code to execute.
88-
- **libraries_used**: Required. A list of libraries used in the code that need to be installed.
120+
- **libraries_used**: Optional. A list of libraries used in the code that need to be installed. Default is `[]`
89121

90122
## Agent Integration Example
91123

@@ -152,7 +184,7 @@ class CodeInterpreterTool(BaseTool):
152184
if self.unsafe_mode:
153185
return self.run_code_unsafe(code, libraries_used)
154186
else:
155-
return self.run_code_in_docker(code, libraries_used)
187+
return self.run_code_safety(code, libraries_used)
156188
```
157189

158190
The tool performs the following steps:
@@ -168,8 +200,9 @@ The tool performs the following steps:
168200
By default, the `CodeInterpreterTool` runs code in an isolated Docker container, which provides a layer of security. However, there are still some security considerations to keep in mind:
169201

170202
1. The Docker container has access to the current working directory, so sensitive files could potentially be accessed.
171-
2. The `unsafe_mode` parameter allows code to be executed directly on the host machine, which should only be used in trusted environments.
172-
3. Be cautious when allowing agents to install arbitrary libraries, as they could potentially include malicious code.
203+
2. If the Docker container is unavailable and the code needs to run safely, it will be executed in a sandbox environment. For security reasons, installing arbitrary libraries is not allowed
204+
3. The `unsafe_mode` parameter allows code to be executed directly on the host machine, which should only be used in trusted environments.
205+
4. Be cautious when allowing agents to install arbitrary libraries, as they could potentially include malicious code.
173206

174207
## Conclusion
175208

0 commit comments

Comments
 (0)