Chunk¶
A Chunk is a contiguous excerpt of a source's text. Chunks are the units that
get indexed and retrieved, so each one carries enough information to find it again in its source:
the source's origin and the character offset start.
Example¶
from triplum.datatype import Chunk, Collection, Source
collection = Collection(name="Research notes")
source = Source(
collection_id=collection.id,
origin="notes/returns.md",
text="Returns are accepted within 30 days. Refunds take 5 days.",
)
chunk = Chunk(
collection_id=source.collection_id,
source_id=source.id,
origin=source.origin,
ordinal=1,
start=37,
text="Refunds take 5 days.",
)
assert source.text[chunk.start : chunk.start + len(chunk.text)] == chunk.text
- Build a
Sourceas before. - Create a
Chunkpointing at its source:source_idis the source'sid, andcollection_idandoriginare copied from it. ordinalis the chunk's position in the list its chunker returned for that source, counting from 0. Here the chunk is the second of two.startis the offset of the excerpt insource.text, andtextis the excerpt, verbatim.- The assertion is the relationship every chunk has to its source: slicing the source text at
startgives back the chunk text.
What it guarantees¶
startis a Python character offset: it counts code points, not bytes, so it matches Python string slicing directly. It must be non-negative; a negative value fails validation.textis copied verbatim from the source text, never rewritten. Text prepared for embedding (descriptions, questions, context) is a separate concern; see chunk preprocessing.ordinalis between 0 and 65,535, so one source yields at most 65,536 chunks.- Unknown field names are rejected, as for sources: a stray
startt=3passed next tostart=0raises a validation error of typeextra_forbidden. - Chunks are immutable, like sources. The
idis a deterministic UUIDv8 derived fromcollection_id,source_id,ordinal,startandtext. Running the same chunker on the same source again gives the same IDs. The chunker itself does not enter theid: two chunkers that produce the same chunks produce the same IDs. A suppliedidthat does not match fails validation. fingerprint()hashes the same five fields as theid, recomputing on each call. See Fingerprints for storage and cache behavior.Chunkcannot check the slicing relationship itself, because it does not hold the source; the code that creates chunks is responsible for it.
Reference¶
Chunk in the API reference.
Next: Datasets and the DataLoader, where sources come from.