注册 PPIO 账号

如果您之前没有 PPIO 账号,请通过 https://ppio.com/user/register 先进行注册,详情请参考 新手指引

创建 API 密钥

通过 PPIO API 密钥管理 页面,您可以创建 API 密钥并保存好 API 密钥的值用于后续步骤。

安装 SDK

您可以通过在终端执行以下命令来安装 SDK。
npm i ppio-sandbox dotenv

配置环境变量

在您的项目文件夹新建 .env 文件(如果之前不存在的话),并配置 API 密钥。
通过这种方式,对于 JavaScript 和 TypeScript 项目,需要在项目中引入 dotenv/config 包;对于 Python 项目,需要在项目中引入 dotenv 库,并调用 load_dotenv 方法来加载环境变量。
.env
PPIO_API_KEY=sk_*** # 通过前面步骤获取的 API 密钥
或者您在运行代码的命令行终端中,通过设置环境变量来配置 API 密钥。
Bash
export PPIO_API_KEY=sk_*** # 通过前面步骤获取的 API 密钥

编写代码来启动 Agent 沙箱

下面通过一个简单的实例来展示如何通过 SDK 来创建一个沙箱,并运行指定命令;
// index.ts
import 'dotenv/config'
import { Sandbox } from 'ppio-sandbox/code-interpreter'

// The .env file should be located in the project root directory
// dotenv/config will automatically look for .env in the current working directory
// Or 
// You can set the environment variable in the command line
// export PPIO_API_KEY=sk_***

const sandbox = await Sandbox.create()
const execution = await sandbox.runCode('print("hello world")')
console.log(execution.logs)

const files = await sandbox.files.list('/tmp')
console.log(files)

// 不再使用时,关闭沙箱
await sandbox.kill()
执行以下命令来运行上面代码:
npx tsx ./index.ts