> ## Documentation Index
> Fetch the complete documentation index at: https://ikun.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# E2B

> 启用你的 Agent 在远程、安全沙箱中运行代码。

**E2BTools** 使 Agent 能够在一个安全的沙箱环境中执行代码，并支持 Python、文件操作和 Web 服务器功能。

## 前提条件

E2B 工具需要 `e2b_code_interpreter` Python 包和一个 E2B API 密钥。

```shell theme={null}
pip install e2b_code_interpreter
```

```shell theme={null}
export E2B_API_KEY=your_api_key
```

## 示例

以下示例演示了如何创建一个能够在安全沙箱中运行 Python 代码的 Agent：

```python cookbook/tools/e2b_tools.py theme={null}
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.e2b import E2BTools

e2b_tools = E2BTools(
    timeout=600,  # 10 分钟超时（以秒为单位）
)

agent = Agent(
    name="代码执行沙箱",
    agent_id="e2b-sandbox",
    model=OpenAIChat(id="gpt-4o"),
    tools=[e2b_tools],
    markdown=True,
    show_tool_calls=True,
    instructions=[
        "你是使用安全 E2B 沙箱环境编写和验证 Python 代码的专家。",
        "你的主要目的是：",
        "1. 根据用户请求编写清晰、高效的 Python 代码",
        "2. 在 E2B 沙箱中执行和验证代码",
        "3. 与用户共享完整的代码，因为这是主要用例",
        "4. 详细解释代码的工作原理",
        "",
        "你可以使用这些工具：",
        "1. 运行 Python 代码 (run_python_code)",
        "2. 上传文件到沙箱 (upload_file)",
        "3. 从沙箱下载文件 (download_file_from_sandbox)",
        "4. 生成并添加可视化图表作为 ImageArtifact (download_png_result)",
        "5. 列出沙箱中的文件 (list_files)",
        "6. 读取和写入文件内容 (read_file_content, write_file_content)",
        "7. 启动 Web 服务器并获取公共 URL (run_server, get_public_url)",
        "8. 管理沙箱生命周期 (set_sandbox_timeout, get_sandbox_status, shutdown_sandbox)",
    ],
)

# 示例：生成斐波那契数列
agent.print_response(
    "编写 Python 代码生成前 10 个斐波那契数并计算它们的和与平均值"
)

```

## Toolkit 参数

| 参数                   | 类型     | 默认值     | 描述                                       |
| -------------------- | ------ | ------- | ---------------------------------------- |
| `api_key`            | `str`  | `None`  | E2B API 密钥。如果未提供，则使用 E2B\_API\_KEY 环境变量。 |
| `run_code`           | `bool` | `True`  | 是否注册 `run_code` 函数                       |
| `upload_file`        | `bool` | `True`  | 是否注册 `upload_file` 函数                    |
| `download_result`    | `bool` | `True`  | 是否注册 `download_result` 函数                |
| `filesystem`         | `bool` | `False` | 是否注册文件系统操作                               |
| `internet_access`    | `bool` | `False` | 是否注册互联网访问函数                              |
| `sandbox_management` | `bool` | `False` | 是否注册沙箱管理函数                               |
| `timeout`            | `int`  | `300`   | 沙箱的超时时间（秒）（默认：5 分钟）                      |
| `sandbox_options`    | `dict` | `None`  | 要传递给 Sandbox 构造函数的附加选项                   |
| `command_execution`  | `bool` | `False` | 是否注册命令执行函数                               |

## Toolkit 函数

### 代码执行

| 函数                | 描述                      |
| ----------------- | ----------------------- |
| `run_python_code` | 在 E2B 沙箱环境中运行 Python 代码 |

### 文件操作

| 函数                           | 描述                                  |
| ---------------------------- | ----------------------------------- |
| `upload_file`                | 将文件上传到沙箱                            |
| `download_png_result`        | 将 PNG 图片结果添加为 ImageArtifact 到 Agent |
| `download_chart_data`        | 从结果中的交互式图表中提取图表数据                   |
| `download_file_from_sandbox` | 将文件从沙箱下载到本地系统                       |

### 文件系统操作

| 函数                   | 描述               |
| -------------------- | ---------------- |
| `list_files`         | 列出沙箱中某个路径下的文件和目录 |
| `read_file_content`  | 从沙箱读取文件内容        |
| `write_file_content` | 将文本内容写入沙箱中的文件    |
| `watch_directory`    | 监控指定目录在指定时长内的变化  |

### 命令执行

| 函数                        | 描述                  |
| ------------------------- | ------------------- |
| `run_command`             | 在沙箱环境中运行 shell 命令   |
| `stream_command`          | 运行 shell 命令并流式传输其输出 |
| `run_background_command`  | 在后台运行 shell 命令      |
| `kill_background_command` | 终止后台命令              |

### 互联网访问

| 函数               | 描述                  |
| ---------------- | ------------------- |
| `get_public_url` | 获取沙箱中正在运行的服务的公共 URL |
| `run_server`     | 在沙箱中启动服务器并返回其公共 URL |

### 沙箱管理

| 函数                       | 描述          |
| ------------------------ | ----------- |
| `set_sandbox_timeout`    | 更新沙箱的超时时间   |
| `get_sandbox_status`     | 获取沙箱的当前状态   |
| `shutdown_sandbox`       | 立即关闭沙箱      |
| `list_running_sandboxes` | 列出所有正在运行的沙箱 |

## 开发者资源

* 查看 [Tools](https://github.com/agno-agi/agno/blob/main/libs/agno/agno/tools/e2b.py)
* 查看 [Cookbook](https://github.com/agno-agi/agno/blob/main/cookbook/tools/e2b_tools.py)
