• 简体中文
  • Agent 调用沙箱

    假设 agent 收到一个小任务:先把数据写入沙箱,再用命令和 Python 处理,最后读回结果。整个过程只需四次调用,所有步骤共享同一个文件系统。

    要求

    GET /v2/sandboxcapabilities 中应包含 filesexeccode_interpreter

    GET /v1/capabilities 的返回结果中应包含 filesexeccode_interpreter

    设置了 AIO_API_KEY 时,将 key 作为 bearer token 发送。

    调用循环

    每一步是一次调用。命令和代码都能读到第 1 步写入的文件,因为所有 plane 共享同一个文件系统:

    每次调用都经过共享的 Aio helper(见 约定),它会把返回结构拆到 data

    Python
    TypeScript
    BASE_URL = "http://127.0.0.1:18091"
    sb = Aio(BASE_URL)
    
    def run() -> None:
        # 1. Write the input file.
        sb.post("/v2/fs/write", path="/tmp/agent-data.txt", content="3\n7\n12\n5\n")
    
        # 2. Run a command on it.
        cmd = sb.post("/v2/commands", command="wc -l /tmp/agent-data.txt")
        print("bash:", cmd["output"].strip(), "exit", cmd["exit_code"])
        # bash: 4 /tmp/agent-data.txt exit 0
    
        # 3. Execute Python that reads the file and writes a result.
        code = (
            "nums = [int(l) for l in open('/tmp/agent-data.txt').read().split()]\n"
            "open('/tmp/agent-result.txt', 'w').write(str(sum(nums)))\n"
            "print('sum written')"
        )
        ran = sb.post("/v2/code/execute", language="python", code=code)
        print("code:", ran["stdout"].strip(), "status", ran["status"])
        # code: sum written status ok
    
        # 4. Read the result back.
        result = sb.get("/v2/fs/read", path="/tmp/agent-result.txt")
        print("result:", result["content"])
        # result: 27
    
    if __name__ == "__main__":
        run()

    四次调用 1.x SDK 都覆盖;结果放在 data 下,TypeScript 里是 body.data

    Python
    TypeScript
    from agent_sandbox import Sandbox
    
    BASE_URL = "http://127.0.0.1:18091"
    client = Sandbox(base_url=BASE_URL)
    # headers={"x-api-key": "<key>"} when AIO_API_KEY is set
    
    def run() -> None:
        # 1. Write the input file.
        client.file.write_file(file="/tmp/agent-data.txt", content="3\n7\n12\n5\n")
    
        # 2. Run a command on it.
        cmd = client.bash.exec(command="wc -l /tmp/agent-data.txt").data
        print("bash:", cmd.output.strip(), "exit", cmd.exit_code)
        # bash: 4 /tmp/agent-data.txt exit 0
    
        # 3. Execute Python that reads the file and writes a result.
        code = (
            "nums = [int(l) for l in open('/tmp/agent-data.txt').read().split()]\n"
            "open('/tmp/agent-result.txt', 'w').write(str(sum(nums)))\n"
            "print('sum written')"
        )
        ran = client.code.execute_code(language="python", code=code).data
        print("code:", ran.stdout.strip(), "status", ran.status)
        # code: sum written status ok
    
        # 4. Read the result back.
        result = client.file.read_file(file="/tmp/agent-result.txt").data
        print("result:", result.content)
        # result: 27
    
    if __name__ == "__main__":
        run()

    两种写法打印同样三行,留下同样两个文件。wc -l 会给计数补空格,因此示例输出中的 4 /tmp/agent-data.txt 是用 strip() 去掉前导空格后的结果。

    相关页面