运行代码
更新时间:2025-11-06
在 AI Agent 研发领域,让大模型直接生成并执行代码,已成为提升 Agent 自主性与落地能力的核心手段。然而,代码一旦在本地运行,便等同于“敞开了大门”——恶意注入、环境破坏、跨租户攻击等风险随之而来。 业界通行的破局思路是“上云+隔离”:把代码关进容器或虚拟机,即使“爆炸”也只在沙盒内。百度 Agent 沙箱正是基于此理念,为开发者提供一套安全、可控、开箱即用的远端执行环境,让 Agent 既能“随心所欲”地调用工具,又无需为安全“埋单”。
代码沙箱支持的编程语言
代码沙箱已支持如下主流编程语言,您可直接使用。
- Python
- JavaScript and TypeScript
- R
- Java
- Bash
Python SDK使用 run_code方法,JavaScript SDK使用runCode方法,language参数传递沙箱中代码运行需要使用的编程语言即可,下面是一些示例。
前提条件
- 请按照文档配置环境变量。
- 目前仅支持官方代码沙箱,即template名为 code-interpreter-v1。
运行 Python 代码
1from e2b_code_interpreter import Sandbox
2
3sbx = Sandbox.create()
4execution = sbx.run_code('print("Hello, world!")')
5print(execution)
1import { Sandbox } from '@e2b/code-interpreter'
2
3const sbx = await Sandbox.create()
4const execution = await sbx.runCode('print("Hello, world!")')
5console.log(execution)
运行 JavaScript and TypeScript 代码
1from e2b_code_interpreter import Sandbox
2
3# 创建一个沙箱
4sbx = Sandbox.create()
5
6# 安装 axios 库,注意安装需要有外网权限
7sbx.commands.run("npm install axios")
8
9# 运行代码
10execution = sbx.run_code("""
11 import axios from "axios";
12
13 const url: string = "https://api.github.com/status";
14 const response = await axios.get(url);
15 response.data;
16""",
17 language="ts",
18)
19
20print(execution)
1import { Sandbox } from "@e2b/code-interpreter";
2
3// 创建一个沙箱
4const sbx = await Sandbox.create();
5
6// 安装 axios 库,注意安装需要有外网权限
7await sbx.commands.run("npm install axios");
8
9// 运行代码
10const execution = await sbx.runCode(`
11 import axios from "axios";
12
13 const url: string = "https://api.github.com/status";
14 const response = await axios.get(url);
15 response.data;
16`,
17 { language: "ts" }
18);
19
20console.log(execution);
运行 R 代码
1from e2b_code_interpreter import Sandbox
2
3sbx = Sandbox.create()
4execution = sbx.run_code('print("Hello, world!")', language="r")
5print(execution)
1import { Sandbox } from '@e2b/code-interpreter'
2
3const sbx = await Sandbox.create()
4const execution = await sbx.runCode('print("Hello, world!")', { language: 'r' })
5console.log(execution)
运行 Java 代码
1from e2b_code_interpreter import Sandbox
2
3sbx = Sandbox.create()
4execution = sbx.run_code('System.out.println("Hello, world!");', language="java")
5print(execution)
1import { Sandbox } from '@e2b/code-interpreter'
2
3const sbx = await Sandbox.create()
4const execution = await sbx.runCode('System.out.println("Hello, world!");', { language: 'java' })
5console.log(execution)
运行 Bash 代码
1from e2b_code_interpreter import Sandbox
2
3sbx = Sandbox.create()
4execution = sbx.run_code("echo 'Hello, world!'", language="bash")
5print(execution)
1import { Sandbox } from '@e2b/code-interpreter'
2
3const sbx = await Sandbox.create()
4const execution = await sbx.runCode('echo "Hello, world!"', { language: 'bash' })
5console.log(execution)
代码沙箱已预装Python库列表
为了支持Python 数据处理,代码沙箱已预装如下库,您可直接引用。
- aiohttp (v3.9.3)
- beautifulsoup4 (v4.12.3)
- bokeh (v3.3.4)
- gensim (v4.3.2)
- imageio (v2.34.0)
- joblib (v1.3.2)
- librosa (v0.10.1)
- matplotlib (v3.8.3)
- nltk (v3.8.1)
- numpy (v1.26.4)
- opencv-python (v4.9.0.80)
- openpyxl (v3.1.2)
- pandas (v1.5.3)
- plotly (v5.19.0)
- pytest (v8.1.0)
- python-docx (v1.1.0)
- pytz (v2024.1)
- requests (v2.26.0)
- scikit-image (v0.22.0)
- scikit-learn (v1.4.1.post1)
- scipy (v1.12.0)
- seaborn (v0.13.2)
- soundfile (v0.12.1)
- spacy (v3.7.4)
- textblob (v0.18.0)
- tornado (v6.4)
- urllib3 (v1.26.7)
- xarray (v2024.2.0)
- xlrd (v2.0.1)
- sympy (v1.12)
