Chunking¶
Chunking splits a source into chunks: the pieces that will be indexed and retrieved. Choosing the pieces is a retrieval decision. Keeping the whole text as one chunk is valid; so are fixed-size pieces, overlapping pieces, or pieces that follow sections.
The library provides two simple chunkers: FixedSize cuts the
text every n characters, and WholeText keeps the whole text as one chunk.
Example¶
from triplum.datatype import Collection, Source
from triplum.steps.chunking import FixedSize
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.",
)
chunker = FixedSize(20)
for chunk in chunker(source):
print(chunk.ordinal, chunk.start, repr(chunk.text))
assert source.text[chunk.start : chunk.start + len(chunk.text)] == chunk.text
# 0 0 'Returns are accepted'
# 1 20 ' within 30 days. Ref'
# 2 40 'unds take 5 days.'
FixedSize(20)configures the chunker: the size is set once, in__init__, not per call. A size below 1 raisesValueError.- Calling it with one source returns a list of chunks. The last chunk keeps the remainder.
- Every chunk has the source's
collection_idandorigin, and itsstartandtextare an exact slice of the source text, which is what the assertion checks.ordinalnumbers the chunks in the order returned.
FixedSize ignores words, sentences and structure, which is why it is only a baseline.
Whole text¶
WholeText returns one chunk holding the whole source text,
or no chunks for an empty source. Use it when the sources already are the retrieval units, such
as a benchmark's native passages: passage-level recall assumes that each chunk is
one whole passage.
from triplum.datatype import Collection, Source
from triplum.steps.chunking import WholeText
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.",
)
for chunk in WholeText()(source):
print(chunk.ordinal, chunk.start, repr(chunk.text))
# 0 0 'Returns are accepted within 30 days. Refunds take 5 days.'
WholeText takes no settings.
The contract¶
Chunker takes one Source and returns list[Chunk].
- Input: one
Source. - Output: zero or more
Chunks with the source'scollection_idandorigin, each satisfyingsource.text[chunk.start : chunk.start + len(chunk.text)] == chunk.text. Chunk text is never rewritten. - Each chunk's
ordinalis its position in the returned list, starting at 0. Chunk IDs derive from it, so returning the same chunks in the same order gives the same IDs. Ordinals stop at 65,535: a chunker can return at most 65,536 chunks per source, and constructing a chunk beyond that fails validation. WholeTextreturns the whole source text as one chunk atstart0, or no chunks for an empty source.FixedSizeadditionally guarantees that its chunks do not overlap, concatenate back to the source text, and that an empty source produces no chunks. Other chunkers may overlap or skip text.
Writing your own¶
Subclass the protocol and implement __call__. This chunker makes one chunk per paragraph:
import re
from triplum.datatype import Chunk, Collection, Source
from triplum.steps.chunking import Chunker
class Paragraphs(Chunker):
"""One chunk per paragraph; blank lines separate paragraphs."""
def __call__(self, source: Source, /) -> list[Chunk]:
matches = re.finditer(r"[^\n]+(?:\n[^\n]+)*", source.text)
return [
Chunk(
collection_id=source.collection_id,
source_id=source.id,
origin=source.origin,
ordinal=ordinal,
start=match.start(),
text=match.group(),
)
for ordinal, match in enumerate(matches)
]
collection = Collection(name="Policies")
source = Source(
collection_id=collection.id,
origin="notes/policy.md",
text="Returns: 30 days.\n\nRefunds: 5 days.",
)
for chunk in Paragraphs()(source):
print(chunk.start, repr(chunk.text))
# 0 'Returns: 30 days.'
# 19 'Refunds: 5 days.'
Taking start from the match keeps each chunk an exact slice, and enumerate supplies the
ordinals. Chunker only asks for a call, so a plain function with the same signature fits it as
well, without subclassing. This one behaves like WholeText (continuing the example above):
def whole_text(source: Source, /) -> list[Chunk]:
if not source.text:
return []
return [
Chunk(
collection_id=source.collection_id,
source_id=source.id,
origin=source.origin,
ordinal=0,
start=0,
text=source.text,
)
]
chunker: Chunker = whole_text # accepted by the type checker
Open questions¶
Hierarchical chunking (sections containing passages) and how containment is represented are still open.
Next: Chunk preprocessing.