AI Hints
Home›RAG Tutorial›Chunking Practice
RAG · Lesson 23 of 120

Practice: Chunk the Help Centre

All fourteen files.

⏱ 2 min read▶ Video at 22:40🎓 Free
Watch this lesson in the RAG courseWatch on YouTube ↗Full RAG course

Let's put it together, and chunk the whole help centre. We'll split by headings first, so every chunk knows its title and section, then split long sections by size.

Practice: Chunk the Help Centre
From the video: Practice: Chunk the Help Centre
  • All fourteen pages
  • Headings first
  • Then by size

The full chunking step

For each page, the header splitter makes sections, with title and section metadata. We add the source file. Then the recursive splitter breaks any long section into pieces under three hundred characters. Run it. We get a few dozen chunks, and each one knows its file, title, and section.

chunk_all.pypython
from pathlib import Path
from langchain_text_splitters import MarkdownHeaderTextSplitter, RecursiveCharacterTextSplitter

by_heading = MarkdownHeaderTextSplitter([("#", "title"), ("##", "section")])
by_size = RecursiveCharacterTextSplitter(chunk_size=300, chunk_overlap=30)

chunks = []
for path in sorted(Path("data/rag/northwind").glob("*.md")):
    sections = by_heading.split_text(path.read_text())
    for s in sections:
        s.metadata["source"] = path.name
    chunks += by_size.split_documents(sections)

print(len(chunks), "chunks")
print(chunks[5].metadata)
print(chunks[5].page_content)
Output
35 chunks
{'title': 'E-bike display error codes', 'source': 'ebike_errors.md'}
E03: the speed sensor is not reading. Check the magnet on the rear wheel is lined up
with the sensor.
E05: the motor is overheating. Stop riding for 15 minutes, then continue in a lower
assist mode.

What we have now

We now have a clean set of chunks, each small, complete, and labelled. This is the input to the next section, where we turn every chunk into an embedding, so we can search by meaning.

keyvalue
Chunksa few dozen
Eachone idea
Labelsfile, title, section
Nextembeddings

Summary

We chunked the whole help centre, by headings, then by size, with full metadata. Next, the chunking quiz.

Key takeaways
  • Headings first
  • Then size
  • Metadata everywhere
Next up: Chunking Quiz — test yourself