Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 67

RAG System Architecture Explained

Recommended resources

In our last two lessons, you built the core components of our AI Tutor's knowledge system. First, you created an ingestion pipeline to process and store SQL documentation as embeddings. Then, you implemented a retrieval mechanism to perform semantic searches on that knowledge base. You've essentially built the tutor's memory and a way to recall information from it.

Now, it's time to zoom out and see how these pieces fit together. Today's lesson will focus on our learning outcome: to explain the architecture of a Retrieval-Augmented Generation (RAG) system. Understanding this architecture is crucial, as it's the blueprint for the AI Tutor you are building. We'll connect the ingestion and retrieval steps you've already completed to the final generation step, giving you a complete picture of the end-to-end workflow.

1. Why RAG? Solving the Limitations of LLMs

Large Language Models (LLMs) like GPT are incredibly powerful, but they have inherent limitations. They are trained on a static snapshot of data, which means:

  • Knowledge Cutoff: They don't know about events or information created after their training date.
  • Lack of Private Data: They have no knowledge of your specific, private documents—like our SQL knowledge base for the AI Tutor.
  • Hallucination: When they don't know an answer, they might invent plausible but incorrect information.

To overcome these issues, we use Retrieval-Augmented Generation (RAG). RAG gives an LLM access to an external, up-to-date knowledge base at the time it needs to answer a question. This grounds the model's response in factual, relevant data, significantly reducing hallucinations and allowing it to use information it wasn't trained on.

For a clear explanation of these problems and how RAG offers a solution, the following video by Krish Naik is an excellent starting point.

Introduction To Undertsanding RAG(Retrieval-Augmented Generation)

Watch this introductory section from "Introduction To Undertsanding RAG(Retrieval-Augmented Generation)" by Krish Naik.

Focus on the discussion from the beginning up to the end. Pay close attention to the two main disadvantages of using LLMs alone: hallucination and the inability to use private, up-to-date data. The video also briefly contrasts RAG with fine-tuning, another method for customizing LLMs.


2. The Complete RAG Architecture

A RAG system can be understood as two primary pipelines: an "offline" data preparation pipeline and an "online" query pipeline.

  1. Data Preparation (Indexing/Ingestion): This is the process you built in the first lesson of this module. It involves taking your raw data, preparing it, and storing it in a way that's optimized for retrieval.
  2. RAG Query Process (Retrieval & Generation): This is the real-time process that happens when a user asks a question. It uses the retrieval mechanism you built in the last lesson and combines it with an LLM to generate an answer.

The diagram below provides a high-level overview of this entire process.

This diagram shows the two main phases of a RAG system. The **Data Preparation** phase (top) is an offline process that indexes your knowledge base. The **RAG query** phase (bottom) is the online process that handles a user's question, retrieves relevant context, and generates a response.

To make this architecture intuitive, the next video uses a fantastic analogy of building a high-tech library. It breaks down the entire pipeline into six clear steps that cover both the preparation and query phases.

Retrieval-augmented generation (RAG), Clearly Explained (Why it Matters)

Watch this segment from "Retrieval-augmented generation (RAG), Clearly Explained" by Builders Central.

This is the most important part of the lesson. Watch the section from how RAG works. The video explains the six key steps of a RAG pipeline using a library analogy: Data Intake (Books arrive) Chunking (Breaking books into index cards) Embedding (Giving each card GPS coordinates) Vector Storage (Organizing the high-tech shelves) Retrieval (Librarian finds the exact cards) Synthesis (Librarian writes the perfect answer) As you watch, connect these steps to the code you've already written. Steps 1-4 correspond to your ingest.py script, and step 5 corresponds to your query_data.py script.


3. A Production-Oriented View of the RAG Pipeline

The "high-tech library" provides a strong conceptual model. For a more detailed, production-focused perspective, we can break down the architecture into a series of engineering steps. The following article provides a comprehensive guide that aligns with building a robust, real-world RAG application.

Step-by-step Guide to Building a RAG (Retrieval- ...

This guide from Nimbleway offers a practical, step-by-step framework for building a production-grade RAG pipeline.

Read the main section titled "How to Build a RAG (Retrieval-Augmented Generation) Pipeline in 8 Steps". This section expands on the concepts from the video, covering: Data Collection and Ingestion Data Cleaning and Transformation Chunking and Embedding Indexing into a Vector Store Building the Retrieval and Query Engine Prompt Engineering and LLM Integration Orchestration, Evaluation, and Monitoring Continuous Improvement and Refreshing Focus on understanding how each step contributes to the overall system's reliability and performance. This gives you a forward-looking view of what's required to move your project from a prototype to a production system.


4. Key Functional Components: Retriever and Generator

At its core, the RAG architecture is composed of two main functional units, as shown in the diagram below.

This diagram illustrates the two main workflows. The **Document Ingestion** flow (top) is the offline preparation. The **User Query, Retrieval, and Response Generation** flow (bottom) shows the interaction between the user, the retriever, and the generator (LLM) at query time.

Let's briefly examine these components.

The Retriever

The retriever's job is to find the most relevant information from your knowledge base in response to a query. This is exactly what you implemented in the previous lesson using semantic search.

  • It takes a user's query.
  • It converts the query to an embedding.
  • It searches the vector database for the document chunks with the highest similarity (e.g., using cosine similarity).
  • It returns these top-k relevant chunks.

This process is often called Dense Passage Retrieval (DPR), where "dense" refers to the fact that we are using dense vector embeddings to represent the meaning of the text passages.

The Generator

The generator is the LLM (e.g., GPT-3.5, GPT-4) that creates the final, human-readable answer. Its key function is to synthesize a response based on two sources of information:

  1. Its own pre-trained knowledge.
  2. The fresh, contextual information provided by the retriever.

The process of combining the user's query and the retrieved context into a single input for the LLM is the "Augmentation" step in RAG. We do this through carefully designed prompts, which guide the LLM on how to use the provided context to answer the question accurately.


Conclusion

In this lesson, you've pieced together the full architecture of a Retrieval-Augmented Generation system. You now understand how the ingestion and retrieval scripts you've built serve as critical components in a larger workflow that enables an LLM to answer questions using a custom knowledge base.

Key Takeaways:

  • RAG Solves Key LLM Problems: It reduces hallucinations and allows LLMs to use private, up-to-date information by grounding them in external data at query time.
  • Two-Pipeline Architecture: RAG consists of an offline ingestion pipeline (load, chunk, embed, index) and an online query pipeline (embed query, retrieve, augment, generate).
  • Core Components: The system is driven by a Retriever (which performs semantic search) and a Generator (the LLM that synthesizes the final answer).
  • Augmentation is Key: The magic happens when the retrieved context is "augmented" into the LLM's prompt, guiding it to generate a factually grounded response.

You've explored the "R" (Retrieval) and the "A" (Augmentation) in RAG from an architectural standpoint. The final step is the "G" (Generation). In our next lesson, we will dive deep into the art of prompt engineering. You'll learn how to craft effective prompts that instruct the LLM to act as a helpful AI tutor, using the retrieved context to provide clear, step-by-step explanations.