Skip to main content
Version: DEV

代码组件

一个允许用户将 Python 或 JavaScript 代码集成到其 Agent 中用于动态数据处理的组件。


使用场景

当您需要将复杂的代码逻辑(Python 或 JavaScript)集成到您的 Agent 中进行动态数据处理时,代码组件是必不可少的。

前置条件

1. 确保 gVisor 已正确安装

我们使用 gVisor 来隔离代码执行与主机系统。请按照官方安装指南安装 gVisor,确保您的操作系统兼容后再继续。

2. 确保 Sandbox 已正确安装

RAGFlow Sandbox 是一个安全、可插拔的代码执行后端。它作为代码组件的代码执行器。请按照此处的说明安装 RAGFlow Sandbox。

注意

如果您的 RAGFlow Sandbox 无法正常工作,请务必查阅本文档中的故障排除部分。我们保证它能解决 99.99% 的问题!

3. (可选)安装必要的依赖项

如果您需要将自己的 Python 或 JavaScript 包导入到 Sandbox 中,请按照如何将自己的 Python 或 JavaScript 包导入到 Sandbox 中?部分提供的命令安装额外的依赖项。

4. 在 RAGFlow 中启用 Sandbox 特定设置

确保在 ragflow/docker/.env 中启用了所有 Sandbox 特定设置。

5. 更改后重启服务

对配置或环境的任何更改都需要完全重启服务才能生效。

配置

输入

您可以为代码组件指定多个输入源。在输入变量部分点击**+ 添加变量**以包含所需的输入变量。

代码

此字段允许您输入和编辑源代码。

Python 代码示例

    def main(arg1: str, arg2: str) -> dict:
return {
"result": arg1 + arg2,
}

JavaScript 代码示例


const axios = require('axios');
async function main(args) {
try {
const response = await axios.get('https://github.com/infiniflow/ragflow');
console.log('Body:', response.data);
} catch (error) {
console.error('Error:', error.message);
}
}

返回值

您在此处定义代码组件的输出变量。

输出

定义的输出变量将自动填充在此处。

故障排除

HTTPConnectionPool(host='sandbox-executor-manager', port=9385): Read timed out.

根本原因

  • 您没有正确安装 gVisor,runsc 未被识别为有效的 Docker 运行时。
  • 您没有拉取运行器所需的基镜像,没有启动运行器。

解决方案

对于 gVisor 问题:

  1. 安装 gVisor

  2. 重启 Docker。

  3. 运行以下命令进行双重检查:

    docker run --rm --runtime=runsc hello-world

对于基镜像问题,拉取所需的基镜像:

docker pull infiniflow/sandbox-base-nodejs:latest
docker pull infiniflow/sandbox-base-python:latest

HTTPConnectionPool(host='none', port=9385): Max retries exceeded.

根本原因

sandbox-executor-manager 未在 /etc/hosts 中映射。

解决方案

/etc/hosts 中添加新条目:

127.0.0.1 es01 infinity mysql minio redis sandbox-executor-manager

Container pool is busy

根本原因

所有运行器当前都在使用中,正在执行任务。

解决方案

请稍后重试或在配置中增加池大小以提高可用性并减少等待时间。

常见问题

如何将自己的 Python 或 JavaScript 包导入到 Sandbox 中?

要导入您的 Python 包,请更新 sandbox_base_image/python/requirements.txt 以安装所需的依赖项。例如,要添加 openpyxl 包,请按照以下命令行操作:

(ragflow) ➜ ragflow/sandbox main ✓ pwd # 确保您在正确的目录中
/home/infiniflow/workspace/ragflow/sandbox

(ragflow) ➜ ragflow/sandbox main ✓ echo "openpyxl" >> sandbox_base_image/python/requirements.txt # 将包添加到 requirements.txt 文件

(ragflow) ➜ ragflow/sandbox main ✗ cat sandbox_base_image/python/requirements.txt # 确保包已添加
numpy
pandas
requests
openpyxl # 在这里

(ragflow) ➜ ragflow/sandbox main ✗ make # 重建 docker 镜像,此命令将重建镜像并立即启动服务。如果只想构建镜像,请使用 `make build`。

(ragflow) ➜ ragflow/sandbox main ✗ docker exec -it sandbox_python_0 /bin/bash # 进入容器检查包是否已安装


# 在容器中
nobody@ffd8a7dd19da:/workspace$ python # 启动 python shell
Python 3.11.13 (main, Aug 12 2025, 22:46:03) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import openpyxl # 导入包以验证安装
>>>
# 没问题!

要导入您的 JavaScript 包,请导航到 sandbox_base_image/nodejs 并使用 npm 安装所需的包。例如,要添加 lodash 包,请运行以下命令:

(ragflow) ➜ ragflow/sandbox main ✓ pwd
/home/infiniflow/workspace/ragflow/sandbox

(ragflow) ➜ ragflow/sandbox main ✓ cd sandbox_base_image/nodejs

(ragflow) ➜ ragflow/sandbox/sandbox_base_image/nodejs main ✓ npm install lodash

(ragflow) ➜ ragflow/sandbox/sandbox_base_image/nodejs main ✓ cd ../.. # 返回 sandbox 根目录

(ragflow) ➜ ragflow/sandbox main ✗ make # 重建 docker 镜像,此命令将重建镜像并立即启动服务。如果只想构建镜像,请使用 `make build`。

(ragflow) ➜ ragflow/sandbox main ✗ docker exec -it sandbox_nodejs_0 /bin/bash # 进入容器检查包是否已安装

# 在容器中
nobody@dd4bbcabef63:/workspace$ npm list lodash # 通过 npm list 验证
/workspace
`-- lodash@4.17.21 extraneous

nobody@dd4bbcabef63:/workspace$ ls node_modules | grep lodash # 或通过列出 node_modules 验证
lodash

# 没问题!