agrag.chunking.turns.TurnWindowChunker
Bases: Chunker
Packs whole chat turns into windows of at most chunk_size tokens.
The chunker reads Document.turns. A window holds one or more whole turns, and
a chunk boundary never falls inside a turn. Tokens are counted for each turn
alone, so the separators between turns are not part of the count. Text before
the first turn joins the first window, and text after a turn joins the window
that holds that turn.
A turn above the budget is split by fallback, and its chunks have the
chunker name turn-window:<fallback strategy>. A document without turns is
split by fallback as a whole and its chunks have the same name.
Attributes:
- chunk_size (
int) – The most tokens in a window, counted withtokenizer. - turn_overlap (
int) – The number of turns that a window repeats from the window before it. A window always moves on by at least one turn. - tokenizer (
str) – The tokenizer that counts size."character"counts characters. - fallback (
SerializeAsAny[SpanChunker]) – The chunker for a turn above the budget and for a document without turns.
Functions:
- chunk – Split a document into chunks.
- fingerprint – Return the hash of
settings(), 16 hex characters. - model_copy – Copy the chunker, validating any changed setting.
- model_post_init – Load the tokenizer once, so a bad name fails at construction.
- settings – Return the strategy name and every setting as JSON-safe data.
chunk
chunk(document:Document) -> list[Chunk]
Split a document into chunks.
Every chunk has non-empty text, indexes run from 0 without gaps, and a chunk
with text provenance has text equal to document.text at its offsets.
Parameters:
- document (
Document) – The document to split.
Returns:
list[Chunk]– The chunks, in document order, each withchunkerandchunker_hashset. A strategy that setschunkeritself keeps its value.
Raises:
ChunkingError– The strategy returned chunks that break the contract.
chunk_size
chunk_size: int = Field(default=256, gt=0)
fallback
fallback: SerializeAsAny[SpanChunker] = Field(default_factory=RecursiveChunker)
fingerprint
fingerprint() -> str
Return the hash of settings(), 16 hex characters.
model_config
model_config = ConfigDict(frozen=True, extra='forbid')
model_copy
model_copy(*, update:Mapping[str, Any] | None = None, deep:bool = False) -> Self
Copy the chunker, validating any changed setting.
A plain copy keeps the fingerprint and the splitter of the original. A copy with changes is built again from its configuration.
model_post_init
model_post_init(context:Any) -> None
Load the tokenizer once, so a bad name fails at construction.
settings
settings() -> dict[str, Any]
Return the strategy name and every setting as JSON-safe data.
A setting that is itself a chunker appears as that chunker's settings.
strategy
strategy: str
The strategy name, "turn-window".
tokenizer
tokenizer: str = DEFAULT_TOKENIZER
turn_overlap
turn_overlap: int = Field(default=0, ge=0)