Skip to content
RAG Repo

Lesson 1 of 7

What you will build

The plan for the course, the RAG pipeline at a glance, and the tools you need installed.

By the end of this course you will have a small Python script that answers questions about your own documents. Ask it something, and instead of guessing, it finds the most relevant passages in your files and uses them to write a grounded answer. That is Retrieval-Augmented Generation (RAG), and you are going to build one from scratch.

We will not use a framework. Frameworks like LangChain and LlamaIndex are useful, but they hide the moving parts, and the moving parts are exactly what you want to understand. Once you have built RAG by hand, any framework will make sense in minutes.

The pipeline at a glance

A RAG system has two halves.

Indexing (done once, up front):

  1. Load your documents.
  2. Chunk them into small passages.
  3. Embed each passage, turning it into a list of numbers that captures its meaning.
  4. Store those vectors so you can search them.

Answering (done for every question):

  1. Embed the question with the same model.
  2. Retrieve the handful of passages whose vectors are closest to the question.
  3. Generate an answer by giving those passages to a language model as context.

Each lesson builds one of these steps, and by the end they join into a single script.

Why bother

A language model on its own has two weaknesses: it only knows what was in its training data, so it goes stale, and it will happily make things up when it does not know. RAG fixes both by fetching real, current passages from your documents and asking the model to answer from those. The model stops guessing and starts citing.

What you need

  • Python 3.10 or newer.
  • A terminal, and about an hour.
  • A language model. We will use the Anthropic API in the final lesson, but any provider works.

Set up a project and install the three libraries we will use:

mkdir diy-rag && cd diy-rag
python -m venv .venv && source .venv/bin/activate
pip install sentence-transformers numpy anthropic
  • sentence-transformers gives us a free, local embedding model, so the indexing side costs nothing.
  • numpy is all we need for the vector store: we will build nearest-neighbour search by hand, so you can see there is no magic in it.
  • anthropic is the client for the language model in the last step.

Next steps

In the next lesson we will get a small set of documents to work with. If you already have a folder of notes, Markdown files, or text, you are ready to go.