Chunk Text
将大段文本拆分为较小的分块,以便并行处理。
按页面标记、段落或字符将长文本切分为多个片段,相邻片段之间保留重叠,并返回片段及其统计信息。
处理超出 LLM 上下文窗口的文档时必不可少。支持多种拆分策略,并可配置重叠区域,避免在分块边界处丢失信息。
拆分模式:
- Pages: 在页面标记(--- PAGE N ---)处拆分。将多页合并,使每块不超过分块大小。最适合在 OCR 之后使用。
- Paragraphs: 在段落边界(连续两个换行)处拆分。将多段合并,使每块不超过分块大小。
- Characters: 按字符边界拆分,并智能回溯到句子或段落结尾。
工作原理
- 解析配置并将其合并到输入 payload 之上;配置中的键优先于 payload 中的同名键。
- 从
text读取文本(先取配置,再取 payload)。它必须是非空字符串,否则节点失败。 - 使用默认值:
chunk_size40000、overlap500、split_modepages(值为 0 时同样回退到默认值)。 pages:在--- PAGE 3 ---这类标记处切分(三个或更多连字符,不区分大小写),并在不超过chunk_size的前提下合并整页。只有一页或没有标记时,改用paragraphs。paragraphs:在空行处切分,并以同样方式合并整段。两种模式下,每个新片段都以前一片段的最后overlap个字符开头。characters:每chunk_size个字符切一次,并把切点回退到其前 200 个字符内最后一个空行、.或换行处;下一片段从切点前overlap个字符开始。- 通过
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
}
}{
"ok": true,
"data": {
"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
},
"port": "success"
}配置
-
chunk_sizenumber 可选 -
每个分块的目标最大字符数(约 10,000 个 token)。由于边界对齐,实际分块可能略小。
-
overlapnumber 可选 -
相邻分块之间重复的字符数。防止实体在边界处丢失。
-
split_modestring 可选 -
Pages:在 OCR 产生的页面标记处拆分。Paragraphs:在连续两个换行处拆分。Characters:按字符数拆分。
配置中会显示通用执行设置,但该节点目前不执行重试(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 返回的路径,依据“端口”一节所示的输出。
| 表达式 | Editor | Worker |
|---|---|---|
$("Chunk Text").item.json.chunks文本分块数组,可直接用于 flow.split。 | ||
$("Chunk Text").item.json.chunk_count | ||
$("Chunk Text").item.json.total_chars | ||
$("Chunk Text").item.json.avg_chunk_size | — |
— 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。 |
输入契约
-
textstring 必填 -
要拆分为分块的完整文本。
输出契约
-
chunksarray 可选 -
文本分块数组,可直接用于 flow.split。
-
chunk_countnumber 可选 -
total_charsnumber 可选
契约(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
}
}
}{
"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
}
}
}
}
}{
"type": "object",
"properties": {
"text": {
"type": "string",
"description": "Full text to split into chunks."
}
},
"required": [
"text"
]
}{
"type": "object",
"properties": {
"chunks": {
"type": "array",
"description": "Array of text chunks ready for flow.split."
},
"chunk_count": {
"type": "number"
},
"total_chars": {
"type": "number"
}
}
}[
{
"port_id": "success",
"label": "Chunked",
"is_default": true,
"supports_condition": true
},
{
"port_id": "error",
"label": "Error",
"supports_condition": false
}
]执行
- 执行方式
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