< 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.
Part 01 · 3 steps
Chunk the documents
Cut each document into pieces small enough to embed and to quote.
Part 02 · 5 steps
Embed and search
Turn chunks into vectors, and find the nearest to a question.
Part 03 · 4 steps
Keywords too
A second ranking by exact words, fused with the first.
Part 04 · 3 steps
Build the prompt
Number the sources, fit them to a budget, and tell the model the rules.
Part 05 · 6 steps
Answer with the model
Ask, read the citations, and abstain when the sources are silent.
Part 06 · 3 steps
Check the answer
Find the sentences nothing in the sources supports.
Part 07 · 2 steps
The system
Build once, answer many times.