本页内容
Sommatic 文档
打开控制台

Chunk Text

将大段文本拆分为较小的分块,以便并行处理。

以 Markdown 查看
在以下应用中打开
数据 可用 data.chunk 版本 1.0.0 执行方式: sync

按页面标记、段落或字符将长文本切分为多个片段,相邻片段之间保留重叠,并返回片段及其统计信息。

处理超出 LLM 上下文窗口的文档时必不可少。支持多种拆分策略,并可配置重叠区域,避免在分块边界处丢失信息。

拆分模式:

  • Pages: 在页面标记(--- PAGE N ---)处拆分。将多页合并,使每块不超过分块大小。最适合在 OCR 之后使用。
  • Paragraphs: 在段落边界(连续两个换行)处拆分。将多段合并,使每块不超过分块大小。
  • Characters: 按字符边界拆分,并智能回溯到句子或段落结尾。

工作原理

  1. 解析配置并将其合并到输入 payload 之上;配置中的键优先于 payload 中的同名键。
  2. 从 text 读取文本(先取配置,再取 payload)。它必须是非空字符串,否则节点失败。
  3. 使用默认值:chunk_size 40000、overlap 500、split_mode pages(值为 0 时同样回退到默认值)。
  4. pages:在 --- PAGE 3 --- 这类标记处切分(三个或更多连字符,不区分大小写),并在不超过 chunk_size 的前提下合并整页。只有一页或没有标记时,改用 paragraphs。
  5. paragraphs:在空行处切分,并以同样方式合并整段。两种模式下,每个新片段都以前一片段的最后 overlap 个字符开头。
  6. characters:每 chunk_size 个字符切一次,并把切点回退到其前 200 个字符内最后一个空行、. 或换行处;下一片段从切点前 overlap 个字符开始。
  7. 通过 success 返回 { chunks, chunk_count, total_chars, avg_chunk_size }。

在工作流中

节点在工作流文件(draft_nodes[])中的写法,以及一次成功运行的 envelope。

{
  "node_id": "node-chunk-contract",
  "label": "Chunk contract",
  "operator_slug": "data.chunk",
  "operator_version": "1.0.0",
  "config": {
    "split_mode": "pages",
    "chunk_size": 40000,
    "overlap": 500
  }
}

配置

chunk_size number 可选

每个分块的目标最大字符数(约 10,000 个 token)。由于边界对齐,实际分块可能略小。

默认值: 40000

overlap number 可选

相邻分块之间重复的字符数。防止实体在边界处丢失。

默认值: 500

split_mode string 可选

Pages:在 OCR 产生的页面标记处拆分。Paragraphs:在连续两个换行处拆分。Characters:按字符数拆分。

默认值: pages

可选值: pagesparagraphscharacters

配置中会显示通用执行设置,但该节点目前不执行重试(retryOnFail、maxTries)。所有节点均未实现 continueOnFail。

代码中的行为

除 schema 描述之外,实现代码对这些键的实际处理。

text
未在 schema 中声明,但代码会读取并像其他配置键一样解析。可用它指向 text 以外的字段;它会覆盖 payload 中的 text。
overlap
在 characters 模式下,若 overlap 大于或等于两次切分之间的距离,循环会在第一个片段后停止,其余文本被丢弃。请让它远小于 chunk_size。

输出端口

success 默认

文本已切分。chunks 是去除首尾空白的字符串数组。

数据
{
  "chunks": [
    "--- PAGE 1 ---\nContract between the parties...",
    "...last 500 characters of chunk 1\n--- PAGE 9 ---\nAnnex A..."
  ],
  "chunk_count": 2,
  "total_chars": 61234,
  "avg_chunk_size": 30617
}
error

缺少文本或 split_mode 无效。

数据
{
  "error": "Missing input text to chunk."
}

在表达式中

后续节点可通过 $("Chunk Text").item.json.<路径> 读取此节点输出中的内容(请把 Chunk Text 换成你的节点标签)。Editor 标记工作流编辑器在 Previous Nodes 中提示的路径(输出契约);Worker 标记 worker 实际通过 success 返回的路径,依据“端口”一节所示的输出。

表达式EditorWorker
$("Chunk Text").item.json.chunksarray= […] (2)文本分块数组,可直接用于 flow.split。
$("Chunk Text").item.json.chunk_countnumber= 2
$("Chunk Text").item.json.total_charsnumber= 61234
$("Chunk Text").item.json.avg_chunk_sizenumber编辑器不提示= 30617—

— Worker: worker 会返回它,但编辑器不会提示:请手动输入。对象内部的键可能取决于你的数据或配置(例如每个标签一个键)。

错误

消息 端口 原因与处理
Missing input text to chunk. error 配置和 payload 的 text 中都没有非空字符串。请显式映射文本,例如在配置中写 "text": "{{ $json.ocr_text }}"。
Unknown split mode: <split_mode> error split_mode 不是 pages、paragraphs 或 characters。

输入契约

text string 必填

要拆分为分块的完整文本。

输出契约

chunks array 可选

文本分块数组,可直接用于 flow.split。

chunk_count number 可选
total_chars number 可选

契约(JSON Schema)

与编排器声明完全一致的定义。All 是完整的 *.definition.js;可与上方各节对照,找出文档与 schema 之间的差异。发布前可用它校验节点。

{
  "slug": "data.chunk",
  "name": "Chunk Text",
  "category": "data",
  "version": "1.0.0",
  "description": "Split large text into smaller chunks for parallel processing.\n\n  Essential for processing documents that exceed LLM context windows. Supports multiple splitting strategies with configurable overlap to prevent losing information at chunk boundaries.\n\n  **Split Modes:**\n  - **Pages:** Split at page markers (--- PAGE N ---). Groups pages to stay under chunk size. Best after OCR.\n  - **Paragraphs:** Split at paragraph boundaries (double newline). Groups paragraphs to stay under chunk size.\n  - **Characters:** Split at character boundaries with smart backtracking to sentence/paragraph ends.",
  "capabilities": [
    "text",
    "chunking",
    "split",
    "preprocessing"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "chunk_size": {
        "type": "number",
        "title": "Chunk Size (characters)",
        "default": 40000,
        "description": "Target maximum characters per chunk (~10,000 tokens). Actual chunks may be slightly smaller due to boundary alignment."
      },
      "overlap": {
        "type": "number",
        "title": "Overlap (characters)",
        "default": 500,
        "description": "Number of characters to repeat between adjacent chunks. Prevents entity loss at boundaries."
      },
      "split_mode": {
        "type": "string",
        "title": "Split Mode",
        "enum": [
          "pages",
          "paragraphs",
          "characters"
        ],
        "default": "pages",
        "description": "Pages: split at page markers from OCR. Paragraphs: split at double newlines. Characters: split at character count."
      },
      "execution": {
        "type": "object",
        "title": "Execution Settings",
        "properties": {
          "alwaysOutputData": {
            "type": "boolean",
            "title": "Always Output Data",
            "description": "Always output data, even on error (if configured).",
            "default": false
          },
          "retryOnFail": {
            "type": "boolean",
            "title": "Retry On Fail",
            "description": "Retry the execution if it fails.",
            "default": false
          },
          "maxTries": {
            "type": "integer",
            "title": "Max Retries",
            "description": "Number of retries.",
            "default": 3,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "waitBetweenTries": {
            "type": "integer",
            "title": "Wait Between Retries (ms)",
            "default": 1000,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "continueOnFail": {
            "type": "boolean",
            "title": "Continue On Fail",
            "description": "Do not crash workflow on error.",
            "default": false
          },
          "timeout_ms": {
            "type": "integer",
            "title": "Timeout (ms)",
            "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
            "default": 300000
          }
        }
      }
    }
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "text": {
        "type": "string",
        "description": "Full text to split into chunks."
      }
    },
    "required": [
      "text"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "chunks": {
        "type": "array",
        "description": "Array of text chunks ready for flow.split."
      },
      "chunk_count": {
        "type": "number"
      },
      "total_chars": {
        "type": "number"
      }
    }
  },
  "ports": [
    {
      "port_id": "success",
      "label": "Chunked",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "supports_condition": false
    }
  ],
  "execution_mode": "sync",
  "tags": [
    "data",
    "chunk",
    "split",
    "text",
    "preprocessing"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_sommatic_ai",
    "profile": {
      "name": "Sommatic AI",
      "verified": true,
      "url": "https://me.veripass.com/sommatic",
      "logo_url": null
    }
  }
}

执行

执行方式
sync
有状态
否
重试(retryOnFail)
否
能力
textchunkingsplitpreprocessing
标签
datachunksplittextpreprocessing

说明与限制

  • 在 pages 和 paragraphs 模式下,单个超过 chunk_size 的页面或段落会被完整保留,因此片段可能超过 chunk_size。
  • 大小按字符计算,而非 token。可将 chunks 交给 flow.split(path 设为 chunks)逐个处理。
  • 输入 payload 会被切分结果替换;如需之前的数据,请使用 $("Node")。
  • worker 不设置 ok(示例中显示为 true);引擎只按 port 路由。

已对照编排器代码核实 · 31e874b · 2026-11-26

安装智能体工具

为你的编程智能体接入 Sommatic AI 官方文档:技能包、文档 MCP 服务器与 Markdown 页面,无需再抓取网页。

安装 Sommatic AI CLI,并为检测到的智能体配置合适的插件。

npx @sommatic/cli agent setup
智能体如何读取本文档