xberg-chunking
它能做什么
这项技能说明如何把长文本切分为适合 AI 模型上下文窗口或搜索数据库的片段。适合构建 AI 搜索或文档问答工具的人。
适用场景
- ✓当你需要把一份长文档输入到有大小限制的 AI 工具时。
- ✓当你要从文档构建搜索索引或 RAG 系统时。
- ✓当你已经有文本,并希望将其切分为设定大小的片段时。
技能链接
https://github.com/xberg-io/xberg/blob/main/plugin/.ai-rulez/skills/xberg-chunking/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请把这份 80 页的报告切分为大约 1000 个字符的片段,并保留少量重叠,以便加载到我的 AI 搜索中。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会在提取过程中开启切分功能,或对你已有的文本使用单独的切分命令。它会设置片段大小和重叠量,并检查重叠量是否小于片段大小。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Chunking
Use this when feeding documents into an LLM context window or a vector
store. Xberg chunks two ways: inline during extraction (chunks land on
each document's chunks field), or standalone via the chunk command for text you
already have. Sizing is character-based by default, or token-based when a
tokenizer model is supplied.
Inline during extraction
Turn on chunking with --chunk and the chunks appear on the structured
result under chunks:
# 1000-char chunks, 200-char overlap (defaults when --chunk is on)
xberg extract report.pdf --chunk --format json | jq '.result.chunks | length'
# Explicit size + overlap
xberg extract report.pdf --chunk --chunk-size 1500 --chunk-overlap 300 --format json
Overlap must be smaller than chunk size — the CLI rejects
--chunk-overlap >= --chunk-size. When you set only --chunk-overlap
against an existing config, an overlap that exceeds the size is clamped to
chunk_size / 4.
Standalone chunk command
Chunk text you already have, from --text or stdin. Output defaults to
JSON:
# From a flag
xberg chunk --text "long document text ..." --chunk-size 800 --chunk-overlap 100
# From stdin (pipe extracted content straight in)
xberg extract notes.md | xberg chunk --chunk-size 500 --format json
JSON output carries chunks (array of strings), chunk_count, the
resolved config (max_characters, overlap, chunker_type), and
input_size_bytes. Use --format text for a human-readable dump with
--- chunk N --- separators.
Note: in the JSON output,
chunker_typeis rendered capitalized ("Text","Markdown","Yaml","Semantic") because it is emitted via Rust's Debug formatting, whereas the--chunker-typeinput flag is lowercase (text,markdown,yaml,semantic). Lowercase the value before comparing if you parse it back.
Chunker types
--chunker-type selects the splitting strategy (standalone chunk
command):
| Type | Behavior |
|---|---|
text |
Default. Plain character-window splitting with overlap. |
markdown |
Markdown-aware — splits on structure (headings, blocks) where possible. |
yaml |
YAML-aware splitting for structured config/data documents. |
semantic |
Topic-boundary splitting driven by --topic-threshold (0.0–1.0, default 0.75). |
# Markdown-aware chunking keeps headings and blocks intact
xberg chunk --text "$(cat README.md)" --chunker-type markdown
# Semantic chunking — lower threshold = more, smaller topic chunks
xberg chunk --text "$(cat transcript.txt)" --chunker-type semantic --topic-threshold 0.6
Token-based sizing
By default --chunk-size counts characters. To size chunks by tokens for
a specific model, pass --chunking-tokenizer with a HuggingFace tokenizer
id. On the extract command this implicitly enables chunking. Requires the
chunking-tokenizers feature (present in the default CLI build).
# Size chunks by GPT-4o tokens during extraction
xberg extract report.pdf --chunking-tokenizer Xenova/gpt-4o --format json
# Or on the standalone command
xberg chunk --text "$(cat doc.txt)" --chunking-tokenizer Xenova/gpt-4o --chunk-size 512
With a tokenizer set, --chunk-size is interpreted in tokens, not
characters.
Config file alternative
Field names in config files are snake_case under [chunking]:
[chunking]
max_characters = 1000
overlap = 200
chunker_type = "markdown"
xberg extract report.pdf --config xberg.toml --format json
CLI flags map to config fields as
--chunk-size→max_charactersand--chunk-overlap→overlap. In config files use the snake_case names.
Programmatic access
From Python, enable chunking on the config and read the chunks off the
document in the result envelope (result.results[0].chunks):
from xberg import ExtractInput, extract, ExtractionConfig, ChunkingConfig
config = ExtractionConfig(
chunking=ChunkingConfig(max_characters=1000, overlap=200),
)
result = await extract(ExtractInput(uri="report.pdf"), config)
for chunk in result.results[0].chunks or []:
print(len(chunk.content))
The public Python
ChunkingConfig(a dataclass) uses constructor kwargsmax_characters/overlap; the Rust core struct fields are alsomax_characters/overlap. TOML/JSON conf
常见问题
重叠是做什么用的?
重叠会在相邻片段之间重复一小段文字,使在边界处被截断的句子仍能完整出现在其中一个片段里。
可以按词元(token)计算片段大小吗?
可以,前提是提供了分词器模型。否则,大小按字符计算。
如果重叠量太大怎么办?
如果重叠量等于或大于片段大小,命令会拒绝执行。
详情
作者的原始描述
Use when splitting extracted text into chunks for LLM context windows or RAG ingestion. Covers chunk size, overlap, markdown/yaml/semantic chunkers, tokenizer-based sizing, and the standalone `chunk` command.
repo: xberg-io/xberg
热度
- GitHub 星标
- 9.5k
- 排名
- #12269
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
xberg-batch-extraction
9.5k帮你的智能体使用 Xberg 一次处理大量文档,共用一组选项,限制并行工作量,并让失败的文件不会中断整个任务。
xberg-extracting-keywords
9.5k帮你的智能体使用 Xberg 的关键词设置,从文档中提取主要关键词或关键短语,并检测文档语言。
xberg-extracting-tables
9.5k帮你的智能体从 PDF、电子表格和图片中提取表格,并以 Markdown 或结构化数据的形式保持行和列的完整。
xberg-extracting-with-ocr
9.5k帮你的智能体使用 OCR 从没有文字层的扫描 PDF、照片和图片中读取文字,并可选择语言包。