跳到主要内容

智能体框架与 SDK

CrewAI

为 CrewAI 智能体提供隔离的沙箱来运行 Python。原生的 E2BPythonTool 在 E2B 中运行智能体编写的代码,而不是在您的机器上。

维护方
CrewAI
集成方式
内置于 crewai-tools
软件包
crewai-tools[e2b]
语言
Python

配置

快速开始

将提示词粘贴到您的编程智能体中,或按以下步骤自行配置。

粘贴到您的编程智能体中

为此 CrewAI 项目添加 E2B 沙箱。

  1. 1.使用项目的包管理器安装带 e2b 扩展的 crewai-tools("crewai-tools[e2b]")。
  2. 2.该工具需要 E2B_API_KEY 环境变量。如果尚未设置,请向我索要 API 密钥(在 https://console.e2b.dev/?tab=keys 获取)。切勿将其硬编码。
  3. 3.按照 https://docs.e2b.dev/agents/crewai 上的 CrewAI 指南,为我的智能体提供 E2BPythonTool,让它们编写的 Python 在 E2B 沙箱中运行。在 finally 块中关闭该工具。
  4. 4.运行一个任务,并展示来自沙箱的输出。

或自行配置

  1. 1. 安装 E2B 工具

    pip install "crewai-tools[e2b]" python-dotenv
  2. 2. 设置 API 密钥

    export E2B_API_KEY="e2b_..."
    export OPENAI_API_KEY="..."
  3. 3. 将工具交给智能体

    from crewai import Agent, Crew, Task
    from crewai_tools import E2BPythonTool
    
    python_tool = E2BPythonTool(persistent=True, sandbox_timeout=600)
    
    analyst = Agent(
        role="Data Analyst",
        goal="Use sandboxed Python to produce accurate analysis",
        backstory="You verify every answer by running code in an E2B sandbox.",
        tools=[python_tool],
    )
  4. 4. 运行您的 crew

    task = Task(
        description="Generate 10,000 random integers and return their mean and p95.",
        expected_output="A JSON object with the statistics.",
        agent=analyst,
    )
    
    try:
        print(Crew(agents=[analyst], tasks=[task]).kickoff())
    finally:
        python_tool.close()

安全

沙箱中运行的内容

智能体编写的代码在隔离的沙箱中运行,与运行 CrewAI 的机器隔离。

  • 代码智能体编写的 Python,由 E2BPythonTool 在 E2B Code Interpreter 沙箱中执行。
  • 状态默认每次工具调用使用一个新沙箱。persistent=True 可在多次调用之间保留导入和变量。
  • 生命周期sandbox_timeout 限定沙箱的存活时长。在 finally 中调用 close() 将其关闭。
  • 密钥除非您将 API 密钥传入沙箱,否则它们始终与 CrewAI 一起留在您的机器上。

已启动 30亿+ 个沙箱

为每个智能体 配一台机器