1. A Practical Problem
So far we built chatbots with history. Now let us build something practical. You work at a company that keeps data for many clients, in PDFs, Excel sheets and other files. Your manager says: "Analyze these thousand pages and give me a report." Every page has payment amounts, dates and details, all as text.
2. How Would You Do It?
Think first. The first idea is usually: paste everything into ChatGPT. The second idea: split it into parts and ask each part.
Your message is too long. Please submit something shorter.
- Too big. 1,000 pages is roughly 650,000 tokens, far over a 128K context window.
- Even if it fits (a 1M-token model): slow and expensive on every question, and less accurate on very long text.
- Does not scale to 10,000 pages, or to data that keeps changing.
- Still too long. 100 pages, or even 50, is a lot.
- Tedious. Many separate questions to ask.
- Not exact. Each part gives its own answer.
- Parts are connected. Client A is in part 1 and part 2. Splitting blindly breaks that.
3. Which Part Has the Answer?
Even with parts, we must search the 1,000 pages for the right piece. But plain keyword search is not enough.
revenueincome4. We Need a Reliable Way to Query the PDF
We need a way to find the pieces that match the meaning of the question, and ask about only those. That is RAG.
DefinitionRAG stands for Retrieval-Augmented Generation. Retrieve only the pieces that matter, augment the prompt with them, then generate the answer.
5. What Is Inside RAG?
Three steps make it work. Reading the data in is a separate part.
6. From Question to Answer
For every question we search the vector database, take the top-k chunks, merge them, and pass them with the prompt to the LLM as context. That is the whole picture.
flowchart TB
subgraph IDX["1 - Prepare your data (once)"]
direction LR
P["100 pages<br/>your documents"] -- "Chunking" --> C["Chunks<br/>chunk 1, chunk 2<br/>chunk 3 ..."]
C -- "Embeddings" --> V[("Vector database<br/>one vector per chunk")]
end
subgraph FIND["2 - Find the context"]
direction LR
Q(["User prompt"]) -- "Embedding" --> QV["Question vector"]
QV -- "Similarity search" --> TK["Top K = 3<br/>closest chunks"]
V2[("Vector database<br/>from part 1")] --> TK
TK --> CTX["Whole context<br/>the 3 chunks together"]
end
subgraph WRITE["3 - Write the answer"]
direction LR
Q2(["User prompt"]) --> PC["Prompt + context"]
CTX2["Whole context<br/>from part 2"] --> PC
PC --> L["LLM<br/>OpenAI, Gemini ..."]
L --> R(["Response<br/>from the context"])
end
IDX ~~~ FIND
FIND ~~~ WRITE
classDef term fill:#0e3b3a,stroke:#0e3b3a,color:#ffffff,font-weight:bold
classDef gate fill:#fdf1ec,stroke:#c9583a,color:#7a2e1a
classDef store fill:#f5f5f4,stroke:#57534e,color:#292524
classDef c0 fill:#d4ecea,stroke:#2a7a79,color:#0e3b3a
classDef c1 fill:#fbe5dc,stroke:#c9583a,color:#7a2e1a
classDef c2 fill:#e3f5ee,stroke:#4aa58a,color:#14463a
classDef c3 fill:#f6f4f0,stroke:#a7a29a,color:#3f3b35
classDef c4 fill:#eef7f7,stroke:#1a5c5b,color:#0e3b3a
class IDX c0
class P c1
class C c2
class V store
class FIND c3
class Q term
class QV c4
class TK c0
class V2 store
class CTX c1
class WRITE c2
class Q2 term
class PC c3
class CTX2 c4
class L c0
class R term
style IDX fill:#f4fafa,stroke:#9fcfce,stroke-width:1.5px
style FIND fill:#fdf6f3,stroke:#ecc0ae,stroke-width:1.5px
style WRITE fill:#f3faf7,stroke:#9fe0c7,stroke-width:1.5pxRAG pipeline (diagram)