🇬🇧 Where should you split text for RAG? A four-line example with Chonkie

Where should you split text for RAG? A four-line example with Chonkie

Can I return a library book at 5 p.m. on Sunday? The hours page tells me the library closes at four. The returns section tells me there’s an outdoor drop box that accepts books after closing. I need both facts to answer confidently.

That’s the example I use to explain chunking: dividing a document into smaller pieces that a search system can return individually. If each section becomes a chunk, the question about a Sunday return becomes a useful test. Can the system find the hours and the returns policy together?

Download the companion PDF guide. It includes the indexing and retrieval diagrams, the code below, four chunkers compared on the same text, the settings that matter, a guide for choosing a technique, and a reference for all 12 techniques with links to runnable Python labs.

Where chunking fits in RAG

In retrieval-augmented generation, or RAG, a retriever finds source material and a language model uses that material to answer. In a basic vector-search setup, I think of the work in two stages.

  1. At indexing time, split the documents into chunks. Turn each chunk into an embedding, a vector that can be compared to others for similarity. Store the vector with the original text, heading, and source ID.
  2. At question time, embed the question, retrieve relevant chunks, and pass their text to the model. The source IDs let me trace a retrieved claim back to the document.

For the library guide, a natural first split is at the headings. One chunk keeps the Sunday hours under Library hours. Another keeps the drop box policy under Book returns. Each works for a simple question. The Sunday return question requires both.

Example 0 in four lines of Python

I start with two short sentences so I can see every boundary. In an active Python virtual environment, install the same Chonkie version used in the guide.

python -m pip install chonkie==1.7.0

Save this as example_0.py and run python example_0.py.

from chonkie import SentenceChunker
text = "Sunday hours end at four. The drop box stays open."
chunker = SentenceChunker(chunk_size=30)
print([chunk.text for chunk in chunker(text)])

The output contains two whole sentences. The first string includes the space after its period.

['Sunday hours end at four. ', 'The drop box stays open.']

Here, the default character tokenizer counts letters, punctuation, and spaces. Change chunk_size to 60 and predict which sentences will share a chunk before you rerun the script. For an application with a model token budget, use the tokenizer appropriate to that model. Chonkie OSS Quick Start · SentenceChunker reference

Test the retrieved evidence

I start with questions whose answers I can check in the source. For this guide, I’d use these three.

QuestionEvidence to retrieve
When does the library close on Sunday?Sunday closing time
How do I return a book after closing?Outdoor drop box policy
Can I return a book at 5 p.m. on Sunday?Sunday closing time and the drop box policy

To compare two chunking settings, build two indexes from the same documents. Use the same questions, retriever settings, and number of returned chunks for each run. Count how many questions have all their required evidence in the returned chunks.

If a run misses the closing time, inspect the retrieved passages and their neighboring text. You might keep short, related sections together or test a small amount of overlap between neighboring chunks. If the evidence is present and the answer is still wrong, inspect how the model used it. Those are two different things to test.

The library is a teaching example. For an application, repeat the comparison on representative documents and include questions that need evidence from several sections. The best setting for your collection depends on what people ask and what the retriever finds.

For a larger example of how retrieval fits into an application, see my medDiscover case study.


© 2024. All rights reserved.