In this lesson
The assistant reads the right passage.
A chatbot that answers questions about your documents does not learn what is in them. It searches for the right passage and reads that passage at the moment you ask. The hard part is the search, not the conversation.
- Explain what RAG is in plain words
- Understand how texts become numbers and get compared
- Find out whether the error is in the search or in the answer
What RAG is
RAG stands for retrieval-augmented generation. Think of an open-book exam: the student does not memorize the book. They look for the right page, read it and answer. The system does the same. For each question, it looks for the few passages that matter and puts them in the message sent to the model.
This keeps the knowledge up to date, because all you have to do is swap the document, and it lets the assistant cite the page the answer came from. The limit is clear: if the right passage does not show up in the search, the answer is wrong, no matter how good the model is.
The steps
- Extract: read the PDF, page by page.
- Chunk: split it into passages of 600 to 1,000 characters.
- Embed: turn each passage into a list of numbers, the vector.
- Index: store vectors, texts and source in a database, such as Chroma.
- Search: find the passages closest to the question.
- Answer: the model writes, citing the passages.
An embedding model turns text into numbers, so that texts with similar meaning end up with similar numbers. The most important rule: the model that converts the documents must be the same one that converts the questions. If you switch models, create a new collection and reindex everything.
Chroma, when you choose nothing, uses a model whose own model card says it is for English only. For Portuguese, choose a multilingual model.
What the pros know
Why not put the whole document in the prompt? Because the model reads the middle of a long text poorly. The Lost in the Middle study measured this: performance was better when the right information was at the beginning or the end of the context, and it dropped significantly when the information was in the middle. Even models with huge windows suffer from this. That is why professionals put their effort into selecting a few good passages, not into dumping everything in the prompt.
When search by meaning fails
If the question contains a code, as in "What is the price of the XK-2040?", search by meaning fails on its own, because a code has no meaning. Combine the search with an exact text filter.
r = colecao.query(
query_texts=["What is the price of the XK-2040?"],
n_results=5,
where_document={"$contains": "XK-2040"},
)
Careful
A scanned PDF has no text to extract. The page comes out empty, the file looks successfully indexed and no question about it gets a right answer. Count the characters on each page and send the empty ones to an image reader, OCR.
Evaluate in two stages
The assistant has two parts that fail in different ways, so measure each one. First, the search: does the expected page show up among the top five passages? Only once the search gets it right most of the time does it make sense to evaluate the answer. If the search rarely gets it right, tweaking the prompt is a waste of time.
Measure refusal too: when the answer is not in the documents, does the assistant say it did not find it? A system that makes things up is worse than one that admits it does not know.
Example: an electrical supplies distributor built the assistant with twelve PDFs, wrote twenty questions and found that the search got only 60 percent right. The fault was the embedding model, not the chat model.
Practice · 5 min
- Pick five PDFs of your own and write ten questions, each with the page where the answer is.
- See whether the expected page shows up among the top five passages of the search.
- Count the hits and note the cause of each miss: large passage, code in the question or PDF with no text.
Summary
- RAG means finding the right passage and only then answering.
- Documents and questions use the same embedding model.
- Codes and exact names call for a text filter, not just meaning.
- Measure the search before the answer.
Sources
Read on October 30, 2026.
- Liu, N. et al. Lost in the Middle: How Language Models Use Long Contexts (2023).
- Chroma. Embedding Functions: the default model.
- Lewis, P. et al. Retrieval-Augmented Generation (2020).
Lesson quiz
Three situations to apply what you learned.
Isaque Victor free courses