e2b-dev/code-interpreter
简介
E2B Code Interpreter 是一个 Python & JS/TS SDK,用于在 AI 应用中运行 AI 生成的代码(代码解释器)。底层依托 E2B 的开源云沙盒基础设施,在安全隔离的云端容器中执行代码。
- ⭐ Stars:2371
- 🍴 Forks:221
- 📄 License:Apache 2.0
- 🏠 官网:https://e2b.dev
核心能力
- 在安全隔离的云端沙盒中运行任意代码(底层基于 Jupyter)
- 支持 Python 和 JavaScript / TypeScript 两套 SDK
- 适用于 AI 数据分析、LLM 工具调用、Code Interpreter 场景
- 兼容 OpenAI、Anthropic、Cohere、Gemini 等主流 LLM
- 支持自定义沙盒模板(安装额外依赖、更换运行时)
快速上手
安装
# JavaScript / TypeScript
npm i @e2b/code-interpreter
# Python
pip install e2b-code-interpreter
获取 API Key
- 在 e2b.dev 注册账号
- 前往 Dashboard → Keys 获取 API Key
- 设置环境变量:
E2B_API_KEY=e2b_***
示例代码
TypeScript
import { Sandbox } from '@e2b/code-interpreter'
const sbx = await Sandbox.create()
await sbx.runCode('x = 1')
const execution = await sbx.runCode('x+=1; x')
console.log(execution.text) // 输出 2
Python
from e2b_code_interpreter import Sandbox
with Sandbox.create() as sandbox:
sandbox.run_code("x = 1")
execution = sandbox.run_code("x+=1; x")
print(execution.text) # 输出 2
适用场景
- AI 数据分析 Agent(让 LLM 直接生成并运行数据处理代码)
- Code Interpreter 功能复刻(类似 ChatGPT Advanced Data Analysis)
- LLM 工具调用中的代码执行节点
- 自动化报告生成、图表绘制
相关资源
- 📖 官方文档
- 🍳 E2B Cookbook(示例集)
- 📦 npm 包
- 🐍 PyPI 包