Understanding RAG in a Simple Way

In the last part, we saw how to prepare our data, break it into small pieces, and store it neatly. Imagine you’ve built your own Bollywood DVD library, where every film is cut into scenes and labeled. Now, when someone asks a question, you don’t need to rewatch all the movies—you can directly pull out the exact scene that has the answer. That’s what we’re learning here.

What’s Happening Behind the Scenes

Think of it like Kaun Banega Crorepati. The host asks you a tough question. If you rely only on your memory, you might give a wrong answer. But if you use the “phone-a-friend” lifeline, suddenly you’re more confident. That lifeline is like giving your system extra context from your own library of documents.

Without this, the system depends only on what it learned long ago. That knowledge may be old, like remembering France as the last World Cup winner, when actually Argentina has already won in 2022. By adding the latest information from your own collection, the answer becomes accurate.

Why Not Do It Manually?

Sure, you could always copy a Wikipedia paragraph yourself and paste it with your question, but can you imagine doing that every time? It’s like preparing for every KBC round by reading 50 books on the spot. Totally impractical.

Instead, your system should automatically find the right notes, attach them to the question, and then pass everything along. This way, the answer is always based on real, up-to-date details instead of guesswork.

The Big Picture

This process is what makes modern AI apps reliable. Without extra context, answers can sound like a filmi side character guessing. With context, the system becomes more like Rancho from 3 Idiots smart, confident, and always up to date.

Retrieving Relevant Documents

A RAG system for an AI app typically follows three core stages:

1) Indexing This stage involves preprocessing the external data source and storing embed‐ dings that represent the data in a vector store where they can be easily retrieved. 2) **Retrieval** This stage involves retrieving the relevant embeddings and data stored in the vector store based on a user’s query. 3) **Generation** This stage involves synthesizing the original prompt with the retrieved relevant documents as one final prompt sent to the model for a prediction.

image.png

1) Let’s run through an example from scratch again, starting with the indexing stage:

//Load the script (documents)
const loader = new TextLoader("./test.txt");
const rawDocs = await loader.load();
//Split the script into smaller scenes (chunks)
const splitter = new RecursiveCharacterTextSplitter({
  chunkSize: 1000,
  chunkOverlap: 200,
});
const docs = await splitter.splitDocuments(rawDocs);
//embeddings
const embeddingsModel = new GoogleGenerativeAIEmbeddings({
  model: "embedding-001",
  apiKey: process.env.GEMINI_API_KEY,
});
//Store in Pinecone
const pinecone = new Pinecone({
  apiKey: process.env.PINECONE_API_KEY,
});

const index = pinecone.index(process.env.PINECONE_INDEX_NAME);

const vectorStore = await PineconeStore.fromDocuments(docs, embeddingsModel, {
  pineconeIndex: index,
});

console.log("✅ Documents successfully indexed in Pinecone!");
//At this stage, your document is fully indexed.

image.png

Step 2: Retrieval (Finding the right scenes)

Now that the producer (you) has indexed all the movie scripts and stored them neatly in Pinecone, it’s time for the assistant director to shine.

When the director (LLM) asks: