前提条件
E2B 工具需要e2b_code_interpreter
Python 包和一个 E2B API 密钥。
示例
以下示例演示了如何创建一个能够在安全沙箱中运行 Python 代码的 Agent:cookbook/tools/e2b_tools.py
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 | 列出所有正在运行的沙箱 |