Documentation
¶
Overview ¶
Package chunker — adaptive strategy selection.
SelectOptions picks chunk Options tuned for a document's source type and content shape. The selection is fully deterministic (no LLM calls). Future work can replace or augment the heuristics here with the LLM-scored intrinsic-metrics approach described in the Adaptive Chunking paper (Chen et al. 2024, "Evaluating Chunking Strategies for Retrieval").
Intrinsic metrics (paper §3) — future LLM-scored extension points ¶
- RC (Retrieval Coherence): each chunk answers a coherent sub-question.
- BI (Block Integrity): chunks preserve logical block boundaries (paragraphs, sections).
- ICC (Inter-Chunk Coherence): adjacent chunks remain topically linked.
- DCC (Document-Chunk Coherence): every chunk stays faithful to the source document's overall topic.
- SC (Size Compliance): chunk byte-size falls within [TargetSize,MaxSize].
The deterministic rules below approximate these metrics without LLM scoring:
- BI → HeadingAware=true for structured docs; HeadingAware=false for chat.
- SC → smaller TargetSize/MaxSize for short-turn chat sources.
- RC → smaller chunks for high-density memory/agent sources.
Package chunker splits text into overlapping chunks suitable for FTS indexing and (future) embedding. The strategy is:
- (HeadingAware=true) Split into logical sections based on document headings. Each section is processed independently so semantic boundaries are preserved.
- Within each section (or for the whole text when HeadingAware=false): a. Split on paragraph boundaries (\n\n). b. If a paragraph exceeds MaxSize, split further on sentence boundaries (period, exclamation mark, or question mark followed by whitespace). c. Merge consecutive small paragraphs until they approach TargetSize. d. Append an Overlap-byte suffix from chunk N to the start of chunk N+1 so that multi-chunk phrases are findable in at least one chunk. Overlap is applied only within a section; never across section boundaries.
The implementation is intentionally simple: it operates on bytes (UTF-8 compatible because all split points are ASCII), which means byte counts are correct for ASCII and multi-byte characters are never split mid-rune because all split boundaries are ASCII whitespace / punctuation.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Options ¶
type Options struct {
// TargetSize is the preferred chunk size in bytes. The chunker tries to
// keep chunks at or below this size by merging paragraphs.
// Default: 2000.
TargetSize int
// MaxSize is the hard upper bound for a single chunk in bytes. Paragraphs
// larger than this are split at sentence boundaries.
// Default: 4000.
MaxSize int
// Overlap is the number of bytes copied from the end of chunk N to the
// beginning of chunk N+1. This ensures cross-boundary phrases are
// searchable. Overlap must be less than TargetSize.
// Overlap is only applied within a section when HeadingAware is true.
// Default: 100.
Overlap int
// HeadingAware enables section-based splitting. When true, the chunker
// first detects document headings (Markdown, Setext, HTML, numbered
// outline) and splits at those boundaries before applying the paragraph /
// sentence logic within each section. The heading text is prepended to
// every chunk produced from that section so that the heading context is
// preserved in search results.
//
// Default: false (v0.1.9 behaviour unchanged).
HeadingAware bool
}
Options controls chunk size and overlap.
func SelectOptions ¶
SelectOptions returns chunking Options tuned for doc.SourceType and the shape of doc.Content.
Strategy mapping (implemented):
filesystem, notion, github, gdrive → HeadingAware=true, 2000/4000/100 slack, discord, telegram → HeadingAware=false, 900/1500/80 secretary, llm-memory → HeadingAware=false, 1200/2500/100 unknown / default → HeadingAware=true, 2000/4000/100
Content-shape override (BI metric heuristic): If a "long-form" source's content has no heading markers and no paragraph breaks, HeadingAware is downgraded to false to avoid creating single-chunk sections that span the full document.
type Section ¶
type Section struct {
// Level is the heading depth: 0 = implicit (no heading), 1–6 = ATX/Setext/HTML level.
Level int
// Heading is the stripped heading text; empty when Level is 0.
Heading string
// Body is the content below the heading up to (but not including) the next
// same-or-higher-level heading.
Body string
}
Section represents a logical section of a document delimited by a heading.