Generative AIby Dhruv Narang

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.

Manager
"Please analyze these thousand pages of PDFs and give me a report."
The data
1,000 pages
Client detailsPayment amountsDatesEverything in text
How do you think we should proceed?

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.

Idea 1Paste everything into ChatGPT
chatgpt.com
[ all 1,000 pages pasted here ] Summarize every client's payments.

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.
Not good.
Example answer. Token count is a rough estimate (about 500 words a page).
Idea 2Split it and ask in parts
Part 1… Client A, payments 2019 …cut here
Part 2… Client A, payments 2020 …
  • 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.
Not good either.
So which part has the answer?

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.

The question
What was Client A's revenue in 2019?
The word is
revenue
In the PDF
… Client A's income in 2019 was 4.2 lakh …
The word is
income
Keyword search misses it. The words are different, but the meaning is the same. We need search by meaning. That is what embeddings give us.
Example text.

4. 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.

Definition

RAG stands for Retrieval-Augmented Generation. Retrieve only the pieces that matter, augment the prompt with them, then generate the answer.

Retrieve
Find the pieces
Search the PDF for the few pieces that match the question.
Augment
Add them to the prompt
Put those pieces next to the question. This is the context.
Generate
The LLM answers
The LLM writes the answer from the context you gave it.

5. What Is Inside RAG?

Three steps make it work. Reading the data in is a separate part.

Step 1
Chunking
Cut the text into small pieces. We cover the chunking practices.
Lesson: Text Chunking
Step 2
Embeddings
Turn each chunk into numbers, a vector. The theory, and how big an embedding is.
Lesson: Embeddings
Step 3
Similarity search
Find the chunks closest to the question: cosine similarity, dot product, and how a vector database stores them.
Lesson: Vector Databases
A separate part
Document loaders
Getting the data in: PDFs, CSVs, websites, Wikipedia and more.
Lessons: Loading Documents, Web Data & Search

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.5px