> ## Documentation Index
> Fetch the complete documentation index at: https://ppio.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 安全的代码运行环境

> 了解 PPIO Code Interpreter 的归档使用说明，认识该版本在沙箱中运行代码的方式，为维护既有代码执行应用提供参考。

在 AI Agent 开发场景中，大语言模型（LLM）生成的代码执行已成为提升 Agent 自主性的关键技术。通过允许 Agent 在操作系统环境中执行代码并调用工具，可以显著增强其功能完整性和应用落地能力。然而，这种能力也带来了严峻的安全挑战。

当 Agent 在本地环境（即 Agent 程序运行的环境）中直接执行 LLM 生成的代码时，存在以下安全风险：

* **恶意代码注入**：LLM 可能生成包含恶意逻辑的代码，导致系统资源被滥用或数据泄露；
* **环境破坏**：执行的代码可能修改或删除重要文件，破坏运行环境稳定性；
* **跨用户影响**：在多租户环境中，一个用户的恶意代码可能影响其他用户的正常使用；

为了解决这些安全挑战，业界普遍采用云端安全隔离环境作为解决方案。该方案通过在隔离的容器或虚拟机中运行代码，确保即使出现恶意行为，也不会对其他用户或底层基础设施造成影响。

PPIO Agent 沙箱服务基于这一理念，为开发者提供了一个安全、可控的代码执行环境，有效平衡了功能性和安全性需求。

通过使用 PPIO Agent 沙箱服务提供官方模板 `code-interpreter-v1`，您可以运行以下编程语言的代码：

* [Python](#执行-python-代码)
* [JavaScript & TypeScript](#执行-javascript-%26-typescript-代码)
* [R](#执行-r-代码)
* [Java](#执行-java-代码)
* [Bash](#执行-bash-代码)

如果您需要运行其他编程语言的代码，您可以通过 [创建自定义模板](/docs/sandbox/archive/sandbox-template) 来支持。

## 执行 Python 代码

```js JavaScript & TypeScript icon="js" theme={null}
import { Sandbox } from 'ppio-sandbox/code-interpreter';

const sandbox = await Sandbox.create();

const result = await sandbox.runCode(
    `
    import requests
    response = requests.get("https://sandbox.ppio.cn/health")
    print(response.text)
    `,
    { language: "python" }
);
console.log(result);

await sandbox.kill();
```

```python Python icon="python" theme={null}
from ppio_sandbox.code_interpreter import Sandbox

sandbox = Sandbox.create()

result = sandbox.run_code("""
    import requests
    response = requests.get("https://sandbox.ppio.cn/health")
    print(response.text)""", language="python")
print(result)

sandbox.kill()
```

## 执行 JavaScript & TypeScript 代码

```js JavaScript & TypeScript icon="js" theme={null}
import { Sandbox } from 'ppio-sandbox/code-interpreter';

// Create a new sandbox
const sandbox = await Sandbox.create();

await sandbox.commands.run('npm install axios');

// Run the code
const execution = await sandbox.runCode(`
  import axios from "axios";

  const url: string = "https://sandbox.ppio.cn/health";
  const response = await axios.get(url);
  console.log(response.data);
`,
  { language: "ts" }
);

console.log(execution);

await sandbox.kill();
```

```python Python icon="python" theme={null}
from ppio_sandbox.code_interpreter import Sandbox

# Create a new sandbox
sandbox = Sandbox.create()

# Install the axios package
sandbox.commands.run('npm install axios')

# Run the code
execution = sandbox.run_code("""
  import axios from "axios";

  const url: string = "https://sandbox.ppio.cn/health";
  const response = await axios.get(url);
  console.log(response.data);""", language="ts")

print(execution)

sandbox.kill()
```

## 执行 R 代码

```js JavaScript & TypeScript icon="js" theme={null}
import { Sandbox } from 'ppio-sandbox/code-interpreter';

const sandbox = await Sandbox.create();

const result = await sandbox.runCode(
  `
  url <- "https://sandbox.ppio.cn/health"
  content <- readLines(url, warn = FALSE)
  print(paste(content, collapse = "\\n"))
  `,
  { language: "r" }
);
console.log(result);

await sandbox.kill();
```

```python Python icon="python" theme={null}
from ppio_sandbox.code_interpreter import Sandbox

sandbox = Sandbox.create()

result = sandbox.run_code("""
    url <- "https://sandbox.ppio.cn/health"
    content <- readLines(url, warn = FALSE)
    print(paste(content, collapse = "\\n"))""", language="r")

print(result)

sandbox.kill()
```

## 执行 Java 代码

```js JavaScript & TypeScript icon="js" theme={null}
import { Sandbox } from 'ppio-sandbox/code-interpreter';

const sandbox = await Sandbox.create();
const execution = await sandbox.runCode(`
import java.net.*;
import java.io.*;

URL url = new URL("https://sandbox.ppio.cn/health");HttpURLConnection con = (HttpURLConnection) url.openConnection();con.setRequestMethod("GET");

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
    content.append(inputLine);
}
in.close();
con.disconnect();
        
System.out.println(content.toString());
`, { language: 'java' });
console.log(execution);

await sandbox.kill();
```

```python Python icon="python" theme={null}
from ppio_sandbox.code_interpreter import Sandbox

sandbox = Sandbox.create()
execution = sandbox.run_code("""
import java.net.*;
import java.io.*;

URL url = new URL("https://sandbox.ppio.cn/health");HttpURLConnection con = (HttpURLConnection) url.openConnection();con.setRequestMethod("GET");

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
    content.append(inputLine);
}
in.close();
con.disconnect();
        
System.out.println(content.toString());""", language="java")
print(execution)

sandbox.kill()
```

## 执行 Bash 代码

```js JavaScript & TypeScript icon="js" theme={null}
import { Sandbox } from 'ppio-sandbox/code-interpreter';

const sandbox = await Sandbox.create();
const execution = await sandbox.runCode(
    'curl -s https://sandbox.ppio.cn/health', 
    { language: 'bash' },
);

console.log(execution);

await sandbox.kill();
```

```python Python icon="python" theme={null}
from ppio_sandbox.code_interpreter import Sandbox

sandbox = Sandbox.create()
execution = sandbox.run_code(
    'curl -s https://sandbox.ppio.cn/health', 
    language='bash'
)

print(execution)

sandbox.kill()
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.