跳到主要内容

训练与评估

DSPy RLMs

在 E2B 沙箱中运行 DSPy 递归语言模型编写的 Python。您可以使用带 SQLite 的完整 CPython,无限循环的代码会被终止。

维护方
E2B
集成方式
通过 interpreter_factory 使用 E2BInterpreter
软件包
dspy 3.4.0
语言
Python

配置

快速开始

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

粘贴到您的编程智能体中

为这个 DSPy 项目添加 E2B 沙箱。

  1. 1.使用项目的包管理器,将 E2B cookbook 中的 dspy-rlms-python 示例安装到项目中。E2BInterpreter 来自这个本地项目,而不是 PyPI。
  2. 2.解释器需要 E2B_API_KEY 环境变量。如果尚未设置,请向我索要 API 密钥(在 https://console.e2b.dev/?tab=keys 获取)。切勿将其硬编码。
  3. 3.按照 https://docs.e2b.dev/agents/dspy 上的 DSPy RLMs 指南,向 dspy.RLM 传入一个返回新 E2BInterpreter 的 interpreter_factory,让模型编写的 Python 在 E2B 沙箱中运行。
  4. 4.运行一次 RLM,并展示来自沙箱的输出。

或自行配置

  1. 1. 获取示例

    git clone https://github.com/e2b-dev/e2b-cookbook.git
    cd e2b-cookbook/examples/dspy-rlms-python
    uv sync --frozen
    cp .env.example .env
  2. 2. 在 .env 中设置密钥

    E2B_API_KEY=e2b_...
    OPENAI_API_KEY=...
    MODEL=openai/gpt-5.6-luna
  3. 3. 为 RLM 提供 E2B 解释器

    import dspy
    from dspy_e2b import E2BInterpreter
    
    
    class IncidentAnalysis(dspy.Signature):
        records: list[dict] = dspy.InputField()
        query: str = dspy.InputField()
        candidate_ids: list[str] = dspy.OutputField(desc="Exact SQL-filtered IDs, sorted")
        queue_incident_ids: list[str] = dspy.OutputField(desc="IDs classified as queue buildup, sorted")
        counts_by_region: dict[str, int] = dspy.OutputField(desc="SQL counts for queue_incident_ids")
        summary: str = dspy.OutputField(desc="Short explanation grounded in the selected records")
    
    
    def interpreter_factory():
        return E2BInterpreter(execution_timeout=120, sandbox_timeout=600)
    
    
    interpreter_factory.execution_instructions = E2BInterpreter.execution_instructions
    
    rlm = dspy.RLM(
        IncidentAnalysis,
        interpreter_factory=interpreter_factory,
        max_iters=6,
        max_llm_calls=4,
        max_output_chars=4000,
    )
  4. 4. 运行事故分析

    uv run main.py

安全

沙箱中运行的内容

RLM 编写的 Python 在禁用网络的 E2B 沙箱中运行。模型调用和密钥留在您的主机上。

  • 代码生成的 Python 在远程 Linux 上的完整 CPython 中运行,可使用 sqlite3 和 subprocess,没有出站网络。
  • 状态在一次调用中,导入、变量和 SQLite 连接会在各步骤之间保留。每次调用都有自己的沙箱。
  • 生命周期execution_timeout 限制每一步,sandbox_timeout 限制整个会话。DSPy 完成后,shutdown() 会删除沙箱。
  • 密钥提供商密钥留在主机上。沙箱中的 llm_query 调用会发回主机,由主机调用模型。

已启动 30亿+ 个沙箱

为每个智能体 配一台机器