Ragex.Embeddings.Chunker (Ragex v0.20.0)

View Source

Splits large source texts into overlapping windows for fine-grained embedding.

A single embedding for a 500-line module averages over too much semantic territory. This module produces multiple chunk records per entity, each with its own embedding, all pointing back to the parent entity.

Chunk record format

Each chunk is stored in the embedding table under node type :chunk with a compound key {parent_type, parent_id, chunk_index}, and text that retains the parent's identifier header for context anchoring.

Strategy

  • Splits on line boundaries to avoid breaking mid-token.
  • Uses a sliding window with overlap so context around split points is preserved in adjacent chunks.
  • Entities shorter than min_lines are stored as a single embedding using the normal (non-chunk) path.

Summary

Functions

Build a chunk key suitable for Store.store_embedding/4.

True when the node key looks like a chunk key (3-tuple with integer last element).

Default chunking options as a keyword list. Useful for tests and documentation.

Return the parent {type, id} for a chunk key, or nil for non-chunk keys.

Split text into overlapping line-based windows.

Types

chunk_key()

@type chunk_key() :: {parent_type(), parent_id(), non_neg_integer()}

parent_id()

@type parent_id() :: term()

parent_type()

@type parent_type() :: :module | :function | atom()

Functions

chunk_key(parent_type, parent_id, index)

@spec chunk_key(parent_type(), parent_id(), non_neg_integer()) :: chunk_key()

Build a chunk key suitable for Store.store_embedding/4.

The key encodes the parent identity and chunk index so all chunks for an entity can be found by scanning the embeddings table.

chunk_key?(arg1)

@spec chunk_key?({parent_type(), parent_id(), non_neg_integer()} | term()) ::
  boolean()

True when the node key looks like a chunk key (3-tuple with integer last element).

defaults()

@spec defaults() :: keyword()

Default chunking options as a keyword list. Useful for tests and documentation.

parent_of(arg1)

@spec parent_of(chunk_key()) :: {parent_type(), parent_id()} | nil

Return the parent {type, id} for a chunk key, or nil for non-chunk keys.

split(text, opts \\ [])

@spec split(
  String.t(),
  keyword()
) :: [{non_neg_integer(), String.t()}]

Split text into overlapping line-based windows.

Returns a list of {chunk_index, chunk_text} pairs. Always returns at least one chunk. Returns a single-element list when the text is shorter than min_lines — callers can detect this and skip chunk storage.

Options

  • :chunk_lines - lines per chunk window (default: 60)
  • :overlap_lines - lines shared between adjacent windows (default: 15)
  • :min_lines - minimum lines before chunking is applied (default: 30)
  • :header - prepended to every chunk for context anchoring (default: "")