DevTools Intermediate Python

E2B - AI 代码执行沙箱

开源 AI 代码执行沙箱基础设施,在云端安全隔离环境中运行 AI 生成代码,支持 JavaScript/TypeScript 和 Python

aisandboxsecurity

什么是 E2B?

E2B 是一个开源基础设施,允许在云端安全的隔离沙箱中运行 AI 生成的代码。提供 JavaScript/TypeScript SDK 和 Python SDK,可以启动和控制沙箱环境。

安装

npm install e2b           # JavaScript/TypeScript
pip install e2b            # Python

快速开始

JavaScript/TypeScript

import Sandbox from 'e2b'

const sandbox = await Sandbox.create()
const result = await sandbox.commands.run('echo "Hello from E2B!"')
console.log(result.stdout) // Hello from E2B!

Python

from e2b import Sandbox

with Sandbox.create() as sandbox:
    result = sandbox.commands.run('echo "Hello from E2B!"')
    print(result.stdout)  # Hello from E2B!

代码解释器

E2B 还提供代码解释器 SDK,适合 AI 执行动态生成的代码:

import { Sandbox } from '@e2b/code-interpreter'

const sandbox = await Sandbox.create()
const execution = await sandbox.runCode('x = 1; x += 1; x')
console.log(execution.text) // outputs 2

核心特点

特点说明
安全隔离云端沙箱,AI 代码不会影响真实系统
真实工具包含文件系统、网络等真实工具
多语言支持 JavaScript/TypeScript 和 Python
可自托管可以在自己的基础设施上部署

支持的云平台

  • ✅ AWS
  • ✅ Google Cloud (GCP)
  • 🔄 Azure
  • 🔄 General Linux machine

自托管

E2B 支持完全自托管,使用 Terraform 部署:

参考 self-hosting guide

使用场景

  1. AI Agent 工具:给 AI Agent 提供执行代码的安全环境
  2. 代码解释器:用户输入代码,AI 解释执行返回结果
  3. 自动化测试:在隔离环境中运行 AI 生成的测试代码
  4. 数据处理:AI 生成并执行数据处理脚本

参考