Skip to content
Streak:0Loading your progress…Sign in

Module 10 · Lesson 2 of 3

Chunking & indexing

How you split documents matters more than which vector DB you pick. ~30 min

Lesson notes

Chunking trade-off: small chunks are precise but lose context; large chunks keep context but dilute relevance and cost more tokens. Common defaults: 300–800 tokens with 10–20% overlap, splitting on structure (headings, paragraphs) before falling back to size. Store metadata (source, section, page, updated_at) with every chunk — you'll need it for citations, filtering and access control.

Practice

  1. 1. Chunk with overlap

    medium25 XP

    Write chunk_words(text, size, overlap) splitting text into chunks of up to size whitespace-separated words, where consecutive chunks share overlap words. Return a list of strings. Raise ValueError unless 0 <= overlap < size. Don't emit a final chunk that's entirely contained in the previous one.

    Ctrl/⌘ + Enter runs the tests · Esc then Tab leaves the editor

  2. 2. A tiny vector index

    hard50 XP

    Build class VectorIndex(embed) where embed(list[str]) -> np.ndarray (n, dim). Methods: add(docs: list[dict]) where each doc has id, text, and optional acl (set of group names; missing = public); search(query, k, groups=frozenset()) returning up to k {"id", "score"} dicts by cosine similarity, only including docs that are public or share a group with groups. Embed in one batch per add call; store normalised vectors.

    Ctrl/⌘ + Enter runs the tests · Esc then Tab leaves the editor