Skip to content

< all projects26 steps · medium · Rebuild

Build a RAG system

You will have built a RAG system one small function at a time, on a real embedding model and a real chat model: 3 functions that cut documents into chunks, 5 that embed them and find the nearest to a question, 4 that add a keyword ranking and fuse the two, 3 that number the sources and build the prompt, 6 that ask the model, read its citations and abstain when the sources are silent, 3 that check every sentence against the sources, and 2 that put it behind two functions. Each step is a few lines with its own tests, and the earlier functions are provided to the later ones. The models are handed to you: `embedder.embed(texts)` returns one vector per text, `model.chat(messages, system, max_tokens)` returns a reply. Both are real, so the tests for those steps check properties (the right chunk comes first, the fact is in the answer, one call was made) and spend a little of your credits. When every step has passed, the seven files assemble into one system and you can ask it anything about the store, live: it answers with sources, says so when the documents are silent, and flags any sentence it cannot back up.

Start: Split a document into paragraphsSign in to run steps and keep your progress.
  1. Part 01 · 3 steps

    Chunk the documents

    Cut each document into pieces small enough to embed and to quote.

    1. 01Split a document into paragraphsnext
    2. 02Pack paragraphs into chunks
    3. 03Chunk every document
  2. Part 02 · 5 steps

    Embed and search

    Turn chunks into vectors, and find the nearest to a question.

    1. 04Cosine similarity
    2. 05Embed a batch in one call
    3. 06Build the index
    4. 07The nearest chunks
    5. 08Search by meaning
  3. Part 03 · 4 steps

    Keywords too

    A second ranking by exact words, fused with the first.

    1. 09The words of a text
    2. 10Rank by shared words
    3. 11Fuse rankings
    4. 12Hybrid search
  4. Part 04 · 3 steps

    Build the prompt

    Number the sources, fit them to a budget, and tell the model the rules.

    1. 13Number the sources
    2. 14Fit the sources to a budget
    3. 15The prompt
  5. Part 05 · 6 steps

    Answer with the model

    Ask, read the citations, and abstain when the sources are silent.

    1. 16Ask the model
    2. 17Read the citations
    3. 18The chunks an answer cites
    4. 19Answer from the sources
    5. 20Did it abstain?
    6. 21Answer, or say the sources are silent
  6. Part 06 · 3 steps

    Check the answer

    Find the sentences nothing in the sources supports.

    1. 22Split into sentences
    2. 23Is one sentence supported?
    3. 24The unsupported sentences
  7. Part 07 · 2 steps

    The system

    Build once, answer many times.

    1. 25Build the system
    2. 26Answer a question