Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 16.9 — Retrieval-Augmented Generation from Scratch
Python · Example 1 of 3

Chunk text with overlap

RAG answers questions from your own documents: split them into chunks, embed them, retrieve the chunks closest to the question, put them in the prompt as numbered sources, and have the model answer and cite them. It is a few dozen lines of Python, and most failures come from bad chunks and missing context.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write chunk_words(text, size, overlap) returning overlapping chunks of words.

02

Write build_prompt(question, sources) that numbers sources from 1.

03

Write valid_citations(answer, source_count) that checks that every [n] in the answer points at a real source and that at least one citation exists.

Explain it without notes

01

Walk through the indexing and answering phases of RAG.

02

How do chunk size and overlap affect retrieval quality?

03

How do you reduce wrong or ungrounded answers in a RAG system?

04

Users say your RAG assistant gives wrong answers. How do you find out whether the retriever or the generator is at fault, and what do you try first?

Chunk text with overlap
Sign in to run this example in your browser.

Expected output

1 | Docker images are built from layers. Each layer
2 | layers. Each layer is read-only. A container adds
3 | A container adds a thin writable layer on
4 | writable layer on top.