工具

Sandboxes

强大的云端沙箱工具

概述

VeADK 提供多种沙箱工具,为 Agent 的执行与操作提供安全、隔离的运行环境。目前支持以下几种类型:

  • Code sandbox
  • Computer sandbox (TBD)
  • Browser sandbox (TBD)

当前,VeADK 支持两种方式调用 Code Sandbox:

  • (推荐)内建工具 run_code:该方式通过 AgentKit tools 调用,配置过程可自定义代码生成模型。通过设置环境变量 AGENTKIT_TOOL_ID 或者在 config.yaml 中添加配置即可开始使用:
    agentkit:
      tool:
        id: <your_id>
    
  • MCP 工具 code_sandbox:该方式通过 MCP(Model Context Protocol) 调用 Code Sandbox 工具,具体步骤为:
    • 在 VeFaaS 一键部署 Code Sandbox Agent 应用,配置过程中可自定义代码生成模型;
    • 部署成功后,在部署的 Code sanbox 的应用详情标签页获取访问地址,地址形式是:{YOUR_URL}/?token={YOUR_TOKEN};
    • 为 Code sandbox 创建 MCP Server 并配置环境变量:
      SANDBOX_API={YOUR_URL}
      AUTH_TOKEN={YOUR_TOKEN}
      
    • 最后,将上述 Code sanbox 访问地址设置环境变量 TOOL_CODE_SANDBOX_URL 或者在 config.yaml 中添加配置即可开始使用:
      tool:
        code_sandbox:
          url: <your_url>
      

使用

以下示例展示了在 VeADK 中如何通过两种方式调用沙箱工具来执行代码。

示例一:使用内建工具 run_code

agent.py
import asyncio 
from veadk import Agent, Runner
from veadk.tools.builtin_tools.run_code import run_code

agent = Agent(
    instruction="你是一名资深的代码专家。请通过调用 run_code 工具执行代码、调试、并展示运行结果。",
    tools=[run_code]
) 

runner = Runner(agent=agent)

response = asyncio.run(runner.run(messages="请用Python写一个函数,计算斐波那契数列的前10项,并打印出来。"))

print(response)

示例二:使用 MCP 工具 code_sandbox

agent.py
import asyncio 
from veadk import Agent, Runner
from veadk.tools.sandbox.code_sandbox import code_sandbox

agent = Agent(
    instruction="你是一名资深的代码专家。请通过调用 code_sandbox 工具执行代码、调试、并展示运行结果。",
    tools=[code_sandbox]
) 

runner = Runner(agent=agent)

response = asyncio.run(runner.run(messages="请用Python写一个函数,计算斐波那契数列的前10项,并打印出来。"))

print(response)