name: code-interpreter
description: 在沙箱环境中测试和原型代码。用于调试、验证逻辑或安装包。
代码解释器
由AWS Bedrock AgentCore Code Interpreter提供支持的多用途代码执行环境。在安全沙箱中运行代码、执行shell命令和管理文件。
可用工具
- execute_code(code, language, output_filename): 执行Python、JavaScript或TypeScript代码。
- execute_command(command): 执行shell命令。
- file_operations(operation, paths, content): 在沙箱中读取、写入、列出或删除文件。
工具参数
execute_code
| 参数 |
类型 |
必填 |
默认值 |
描述 |
code |
字符串 |
是 |
|
要执行的代码。使用print()进行文本输出。 |
language |
字符串 |
否 |
"python" |
"python"、"javascript"或"typescript" |
output_filename |
字符串 |
否 |
"" |
执行后下载的文件。代码必须保存一个具有此精确名称的文件。自动保存到工作区。 |
execute_command
| 参数 |
类型 |
必填 |
描述 |
command |
字符串 |
是 |
要执行的shell命令(例如,"ls -la"、"pip install requests")。 |
file_operations
| 参数 |
类型 |
必填 |
描述 |
operation |
字符串 |
是 |
"read"、"write"、"list"或"remove" |
paths |
列表 |
对于read/list/remove |
文件路径。read: ["file.txt"],list: ["."],remove: ["old.txt"] |
content |
列表 |
对于write |
包含path和text的条目:[{"path": "out.txt", "text": "hello"}] |
tool_input 示例
execute_code — 文本输出
{
"code": "import pandas as pd
df = pd.DataFrame({'A': [1,2,3], 'B': [4,5,6]})
print(df.describe())",
"language": "python"
}
execute_code — 生成图表
{
"code": "import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
plt.figure(figsize=(10,6))
plt.plot(x, np.sin(x))
plt.title('Sine Wave')
plt.savefig('sine.png', dpi=300, bbox_inches='tight')
print('Done')",
"language": "python",
"output_filename": "sine.png"
}
execute_command — 安装包
{
"command": "pip install yfinance"
}
execute_command — 检查环境
{
"command": "python --version && pip list | head -20"
}
file_operations — 写入文件
{
"operation": "write",
"content": [{"path": "config.json", "text": "{\"key\": \"value\"}"}]
}
file_operations — 列出文件
{
"operation": "list",
"paths": ["."]
}
file_operations — 读取文件
{
"operation": "read",
"paths": ["output.csv"]
}
何时使用此技能
使用code-interpreter作为测试和原型代码的沙箱。
对于生产任务(创建文档、图表、演示文稿),首选专门的技能。
不要用于:
- 格式化或显示代码示例(直接用markdown代码块回复)
- 解释代码或算法(直接用文本回复)
- 可以心算的简单计算(只提供答案)
- 任何不需要实际代码执行的任务
| 任务 |
推荐技能 |
备注 |
| 创建图表/图表 |
visual-design |
首先用于生产图表 |
| 创建Word文档 |
word-documents |
有模板支持和样式 |
| 创建Excel电子表格 |
excel-spreadsheets |
有格式化管道和验证 |
| 创建PowerPoint |
powerpoint-presentations |
有布局系统和设计模式 |
| 测试代码片段 |
code-interpreter |
调试、验证逻辑、检查输出 |
| 原型算法 |
code-interpreter |
在实现前实验 |
| 安装/测试包 |
code-interpreter |
检查兼容性、测试API |
| 调试代码逻辑 |
code-interpreter |
隔离并测试特定函数 |
| 验证计算 |
code-interpreter |
快速数学或数据检查 |
环境
- 语言: Python(推荐,200+库)、JavaScript、TypeScript
- Shell: 通过
execute_command完全shell访问
- 文件系统: 会话内持久;使用
file_operations管理文件
- 会话状态: 变量和文件在同一会话中的多个调用间持久
- 网络: 互联网访问可用(可以使用
requests、urllib、curl)
支持的语言
- Python(推荐)— 200+预装库,覆盖数据科学、ML、可视化、文件处理
- JavaScript — Node.js运行时,用于JSON操作、异步操作
- TypeScript — TypeScript运行时,支持类型检查
预装的Python库
数据分析与可视化
| 库 |
常用用途 |
pandas |
数据框、CSV/Excel I/O、分组、透视 |
numpy |
数组、线性代数、随机、统计 |
matplotlib |
线、条、散点、直方图、子图 |
plotly |
交互式图表、3D图 |
bokeh |
交互式可视化 |
scipy |
优化、插值、信号处理 |
statsmodels |
回归、时间序列、假设检验 |
sympy |
代数、微积分、方程求解 |
机器学习与AI
| 库 |
常用用途 |
scikit-learn |
分类、回归、聚类、管道 |
torch / torchvision / torchaudio |
深度学习、计算机视觉、音频 |
xgboost |
高性能梯度提升 |
spacy / nltk / textblob |
NLP、分词、NER、情感分析 |
scikit-image |
图像处理、滤波器、分割 |
数学与优化
| 库 |
常用用途 |
cvxpy |
凸优化、投资组合优化 |
ortools |
调度、路由、约束编程 |
pulp |
线性规划 |
z3-solver |
SAT求解、形式验证 |
networkx / igraph |
图算法、网络分析 |
文件处理与文档
| 库 |
常用用途 |
openpyxl / xlrd / XlsxWriter |
Excel读写与格式化 |
python-docx |
Word文档创建/修改 |
python-pptx |
PowerPoint创建/修改 |
PyPDF2 / pdfplumber / reportlab |
PDF读写/生成 |
lxml / beautifulsoup4 |
XML/HTML解析 |
markitdown |
将各种格式转换为Markdown |
图像与媒体
| 库 |
常用用途 |
pillow (PIL) |
图像调整大小、裁剪、滤镜、转换 |
opencv-python (cv2) |
计算机视觉、特征检测 |
imageio / moviepy |
图像/视频 I/O 和编辑 |
pydub |
音频操作 |
svgwrite / Wand |
SVG创建、ImageMagick |
数据存储与格式
| 库 |
常用用途 |
duckdb |
在数据框和文件上进行SQL查询 |
SQLAlchemy |
SQL ORM和数据库抽象 |
pyarrow |
Parquet和Arrow格式处理 |
orjson / ujson / PyYAML |
快速JSON/YAML解析 |
网络与API
| 库 |
常用用途 |
requests / httpx |
HTTP请求、API调用 |
beautifulsoup4 |
网页抓取 |
fastapi / Flask / Django |
Web框架 |
工具库
| 库 |
常用用途 |
pydantic |
数据验证、模式定义 |
Faker |
测试数据生成 |
rich |
漂亮打印、表格 |
cryptography |
加密、哈希 |
qrcode |
QR码生成 |
boto3 |
AWS SDK |
要查看完整的200+库及其版本,运行:execute_command(command="pip list")
使用模式
模式1:数据分析
import pandas as pd
import numpy as np
df = pd.DataFrame({
'date': pd.date_range('2024-01-01', periods=100),
'revenue': np.random.normal(1000, 200, 100),
'costs': np.random.normal(700, 150, 100),
})
df['profit'] = df['revenue'] - df['costs']
print("=== Summary Statistics ===")
print(df.describe())
print(f"
Total Profit: ${df['profit'].sum():,.2f}")
print(f"Profit Margin: {df['profit'].mean() / df['revenue'].mean() * 100:.1f}%")
模式2:可视化(带output_filename)
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
fig, axes = plt.subplots(2, 2, figsize=(14, 10))
categories = ['Q1', 'Q2', 'Q3', 'Q4']
values = [120, 150, 180, 210]
axes[0,0].bar(categories, values, color='#2196F3')
axes[0,0].set_title('Quarterly Revenue')
x = np.linspace(0, 10, 50)
axes[0,1].plot(x, np.sin(x), 'b-', linewidth=2)
axes[0,1].set_title('Trend')
sizes = [35, 30, 20, 15]
axes[1,0].pie(sizes, labels=['A','B','C','D'], autopct='%1.1f%%')
axes[1,0].set_title('Market Share')
x = np.random.normal(50, 10, 200)
y = x * 1.5 + np.random.normal(0, 15, 200)
axes[1,1].scatter(x, y, alpha=0.5, c='#FF5722')
axes[1,1].set_title('Correlation')
plt.tight_layout()
plt.savefig('dashboard.png', dpi=300, bbox_inches='tight')
print('Dashboard saved')
模式3:机器学习
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report
from sklearn.datasets import load_iris
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(
iris.data, iris.target, test_size=0.3, random_state=42
)
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
print(classification_report(y_test, y_pred, target_names=iris.target_names))
模式4:使用DuckDB的SQL
import duckdb
import pandas as pd
orders = pd.DataFrame({
'order_id': range(1, 101),
'customer': [f'Customer_{i%20}' for i in range(100)],
'amount': [round(50 + i * 3.5, 2) for i in range(100)],
})
result = duckdb.sql("""
SELECT customer, COUNT(*) as cnt, ROUND(SUM(amount), 2) as total
FROM orders GROUP BY customer
HAVING COUNT(*) >= 3 ORDER BY total DESC LIMIT 10
""").df()
print(result.to_string(index=False))
模式5:从Web获取数据
import requests
import pandas as pd
response = requests.get("https://api.example.com/data")
data = response.json()
df = pd.DataFrame(data)
print(df.head())
模式6:多步骤工作流(会话状态持久)
Call 1: execute_code → 加载和清理数据,存储在变量`df`中
Call 2: execute_code → 分析`df`,生成图表,保存为PNG
Call 3: execute_code → 导出结果到CSV
Call 4: file_operations(operation="read") → 下载CSV
变量(如df)和文件在同一会话中的多个调用间持久。
重要规则
- 在
import matplotlib.pyplot之前使用matplotlib.use('Agg') — 沙箱没有显示。
- 使用
print()进行文本输出 — stdout是结果返回的方式。
output_filename必须精确匹配 — 在plt.savefig()或wb.save()中的文件名必须匹配output_filename参数。
- 对于shell任务使用
execute_command — ls、pip install、curl等。
- 使用
file_operations管理文件 — 显式地读写/列出/删除文件。
- 会话状态持久 — 变量和文件在调用间保持。使用此功能进行多步骤工作流。
常见错误避免
- 在
import matplotlib.pyplot as plt之前忘记matplotlib.use('Agg')
- 使用
plt.show()而不是plt.savefig() — 没有显示
output_filename拼写错误 — 必须与代码保存的文件名完全一致
- 使用
execute_code进行shell任务 — 使用execute_command代替
- 通过
file_operations写入二进制文件 — 使用execute_code生成二进制文件,然后通过output_filename下载