Build a RAG app

Chapter 1 of 1115 min

What is RAG?

In this chapter you will:

  • see why an AI chatbot can't answer questions about your own documents
  • learn what RAG is, and the parts it is made of
  • see what you are going to build, and what you need

Try it first

Open any AI chatbot (ChatGPT, Gemini, Claude) and ask it:

How many days of paid annual leave do employees at Brightpath Studio get?

It can't know. Brightpath Studio is a made-up company, and its staff handbook is not on the internet. The chatbot will either say it doesn't know, or, worse, invent a number that sounds right.

The same thing happens with real documents a chatbot never saw: your company's policies, your class notes, a PDF someone sent you yesterday. A language model only knows what it read while it was being trained.

The fix: give it the right page

You could paste the whole handbook into the chat with your question. For five short files that works. For five thousand pages it doesn't: it's too long for the model to take in, slow, and expensive.

RAG does something smarter. Before asking the model, it finds the few passages that matter for the question, and pastes only those into the prompt. The model then answers from them.

Think of an open-book exam. The model is a clever student who has never seen this book. RAG hands it the right page before it answers.

The name says the three steps:

  • Retrieval: find the passages that match the question.
  • Augmented: add them to the prompt.
  • Generation: the model writes the answer from them.

The parts of a RAG app

A RAG app does some work once, to get the documents ready, and some work for every question.

Once, to prepare:

  1. Load the documents into your program.
  2. Chunk them: cut them into small passages, so you can find exactly the part that answers a question.
  3. Embed every chunk: turn its meaning into a list of numbers, so a computer can tell which passages mean similar things.

For every question:

  1. Search: turn the question into numbers too, and find the chunks whose numbers are closest.
  2. Ask the AI: build a prompt with those chunks and the question, and send it to a language model.
  3. Show the answer in an app, with the file it came from.

You'll build each part yourself, one chapter at a time, in plain Python. No frameworks, so nothing is hidden. The map at the top of each chapter shows which part you're building.

What you will build

Ask My Docs: a chat app that answers questions about the Brightpath Studio handbook. Ask "How many days off do I get a year?" and it answers from the handbook (24 days of paid annual leave) and tells you the answer comes from leave.txt.

By the end it runs in your browser, and you can put it online and send the link to anyone. Then, in the last chapter, you point it at your own documents.

What you need

  • Python basics: variables, lists, dictionaries, for loops, if, and functions. If any of these feel shaky, the Python chapters in Notes cover them.
  • A laptop with Windows, macOS or Linux, and an internet connection.
  • A Google account, for a free Gemini API key in chapter 8. No card needed.

You don't need any AI, maths or machine learning background. Each idea is explained when you first need it.

Quick check

Predict, then check

A RAG app gets the question "Can I work from Bali for a month?". Which part decides which passages of the handbook the AI gets to read?

Show answer

Search (the R in RAG, retrieval). It compares the question with every chunk and picks the closest ones. If search picks the wrong passages, even the best AI gives a wrong answer, which is why we spend three chapters on it.