Retrieval-Augmented Generation (RAG)

Learn with Mevi Technologies

Master one of the most important technologies powering modern AI systems. Understand Chunking, Embeddings, Vector Databases, LangChain, ChromaDB, Groq, Llama 3, and Real World Projects.

Start Learning

Introduction to RAG

RAG stands for Retrieval-Augmented Generation.

It is one of the most important AI architectures used today.

Instead of relying only on the knowledge already present inside a Large Language Model, RAG allows the model to search external information sources before generating answers.

Think of RAG as an Open Book Exam.

Instead of answering purely from memory, the AI first searches documents, retrieves relevant information, and then generates an answer.

Why Do We Need RAG?

Hallucinations

LLMs sometimes generate incorrect information.

Outdated Knowledge

Models may not know recent information.

Private Data

Company documents are not included in training.

Expensive Retraining

Retraining LLMs is expensive and slow.

Complete RAG Architecture

Understanding the entire workflow before diving into individual concepts.

RAG Architecture

Two Major Phases

RAG Step By Step

1. Load Documents
2. Chunk Documents
3. Generate Embeddings
4. Store in Vector Database
5. User Asks Question
6. Generate Query Embedding
7. Similarity Search
8. Retrieve Top Chunks
9. Augment Prompt
10. LLM Generates Answer

Understanding Retrieval, Augmentation & Generation

Retrieval

Search relevant information from knowledge sources.

Augmentation

Add retrieved information into the prompt.

Generation

Generate final answer using the LLM.

Input vs Output Example


INPUT

What are symptoms
of diabetes?

--------------------------------

RAG SYSTEM

Searches Medical Documents

Retrieves Relevant Chunks

Adds Context To Prompt

--------------------------------

OUTPUT

Common symptoms include:

• Excessive thirst

• Frequent urination

• Fatigue

• Blurred vision

• Slow wound healing

What is Chunking?

Chunking is the process of breaking large documents into smaller pieces before storing them inside a Vector Database.

One of the biggest limitations of Large Language Models is the Context Window.

An LLM cannot read unlimited information at once.

If we upload a 500-page PDF directly, the model cannot process everything efficiently.

Therefore, we split the document into smaller parts.

These smaller parts are called Chunks.

Why Do We Need Chunking?

Faster Retrieval

Searching small chunks is much faster than searching an entire document.

Higher Accuracy

Relevant information can be found more accurately.

Lower Token Usage

Reduces prompt size and API cost.

Better Responses

Only useful information reaches the LLM.

Chunking Example


INPUT

100 Page Medical PDF

--------------------------------

Page 1

Page 2

Page 3

...

Page 100

--------------------------------

OUTPUT

Chunk 1

Chunk 2

Chunk 3

Chunk 4

Chunk 5

Chunk 6

...

Chunk N

Types of Chunking

Method Description Usage
Fixed Size Chunking Split every fixed number of characters Simple projects
Token Chunking Split based on token count Production RAG
Sentence Chunking Split using sentence boundaries QA Systems
Paragraph Chunking Split by paragraphs Articles & Blogs
Recursive Chunking Most popular LangChain approach General Purpose
Semantic Chunking Split by meaning/context Advanced Systems

What is Chunk Overlap?

Sometimes important information lies between two chunks.

To avoid losing context, we intentionally repeat some content.


Chunk 1

AI is transforming healthcare.
Doctors use AI for diagnosis.

--------------------------------

Chunk 2

Doctors use AI for diagnosis.
AI improves patient outcomes.

Notice how one sentence appears in both chunks. This is called Chunk Overlap.

What Are Embeddings?

Computers do not understand words.

Computers understand numbers.

Embeddings convert human language into mathematical vectors.

These vectors capture the meaning of words and sentences.

Embedding Example


INPUT

Dog

↓

Embedding Model

↓

OUTPUT

[0.23, 0.81, 0.55, 0.72...]

--------------------------------

INPUT

Cat

↓

Embedding Model

↓

OUTPUT

[0.24, 0.80, 0.57, 0.71...]

Notice something interesting.

Dog and Cat produce similar vectors.

This happens because both have similar meanings.

Why Embeddings Matter

Semantic Search
Similarity Search
Document Retrieval
Recommendation Systems
Vector Databases
RAG Applications

Popular Embedding Models

Model Company Use Case
text-embedding-3-small OpenAI General RAG
text-embedding-3-large OpenAI Advanced Retrieval
BGE BAAI Open Source RAG
E5 Microsoft Search Systems
Sentence Transformers Hugging Face Local RAG

What is a Vector Database?

After generating embeddings, we need a place to store them.

Traditional databases store text.

Vector databases store embeddings.

These embeddings are used for similarity search.

Traditional Database vs Vector Database

Traditional DB Vector DB
Stores Text Stores Vectors
Keyword Search Semantic Search
Exact Matching Meaning Matching
SQL Queries Similarity Search

Popular Vector Databases

Database Description Best For
ChromaDB Easy local vector database Learning Projects
FAISS Facebook vector search engine Fast Search
Pinecone Managed cloud solution Production Apps
Qdrant Open source vector DB Enterprise RAG
Weaviate AI Native Database Large Scale Systems

What is Similarity Search?

Similarity Search is the heart of RAG.

Instead of searching exact words, it searches meaning.


QUESTION

What are symptoms of diabetes?

↓

Embedding Generated

↓

Search Vector Database

↓

Find Similar Medical Chunks

↓

Retrieve Top Results

Keyword Search

Requires exact words.

Similarity Search

Understands meaning.

What is Fine Tuning?

Fine Tuning is the process of taking an already trained Large Language Model and training it again on a custom dataset.

Instead of teaching the model from scratch, we improve an existing model for a specific task.

Think of it like a graduate student.

The student already knows mathematics, science, history, and languages.

Now we give specialized training to become a doctor.

The student doesn't start learning from zero.

Similarly, Fine Tuning teaches a pretrained model a specialized skill.

Fine Tuning Example


BASE MODEL

General Knowledge

↓

Fine Tuning Dataset

Medical Reports

↓

TRAINING

↓

MEDICAL MODEL

Understands
Medical Terminology Better

When Should We Use Fine Tuning?

Medical AI
Legal AI
Coding Assistants
Customer Support Bots
Financial Systems
Company Specific AI

RAG vs Fine Tuning

Many beginners confuse RAG and Fine Tuning.

Both improve AI systems but solve different problems.

Fine Tuning changes the model.

RAG keeps the model unchanged and provides external knowledge.

Feature RAG Fine Tuning
Cost Low High
Training Required No Yes
Latest Information Easy Difficult
Private Documents Excellent Good
Setup Time Fast Slow
Maintenance Easy Complex
Best Use Case Document Retrieval Behavior Learning

RAG vs Traditional Search

Traditional Search RAG
Keyword Matching Semantic Search
Returns Documents Returns Answers
User Reads Results AI Reads Results
No Reasoning LLM Reasoning
Static Search Dynamic Search

What is LangChain?

LangChain is one of the most popular frameworks for building AI applications.

It helps developers connect:

Most modern RAG applications are built using LangChain.

LangChain RAG Architecture

PDF File
PDF Loader
Text Splitter
Embedding Model
ChromaDB
Retriever
Prompt Template
Groq Llama 3
Final Answer

Complete RAG Pipeline


DOCUMENTS

↓

LOADER

↓

CHUNKING

↓

EMBEDDINGS

↓

VECTOR DATABASE

↓

USER QUESTION

↓

QUERY EMBEDDING

↓

SIMILARITY SEARCH

↓

TOP MATCHING CHUNKS

↓

PROMPT AUGMENTATION

↓

LLM

↓

FINAL RESPONSE

Real World Applications of RAG

PDF Chatbots

Ask questions from PDFs.

Healthcare Systems

Analyze reports and medical documents.

Research Assistants

Search scientific papers.

Legal Assistants

Search laws and contracts.

Education Platforms

Study assistants and tutors.

Enterprise Search

Search internal company documents.

Advantages of RAG

Limitations of RAG

RAG Interview Questions

1. What is RAG?

Retrieval-Augmented Generation is a technique that combines information retrieval with LLMs.


2. Why is RAG needed?

To reduce hallucinations and use external knowledge.


3. What is Chunking?

Splitting large documents into smaller pieces.


4. What are Embeddings?

Numerical representations of text.


5. What is a Vector Database?

A database that stores embeddings.


6. Difference between RAG and Fine Tuning?

RAG retrieves knowledge. Fine Tuning modifies the model.

Mini Project: Blood Report RAG Assistant

Now that we understand RAG, let's build a real-world application.

In this project, users upload a Blood Report PDF and ask questions about the report.

The RAG system retrieves relevant medical information from the report and provides intelligent explanations.

Project Architecture

User Uploads PDF
PDF Loader
Chunking
Embeddings
ChromaDB
User Question
Retriever
Groq Llama 3
Answer

Input vs Output Example


INPUT PDF

Blood Report.pdf

--------------------------------

USER QUESTION

Is my hemoglobin normal?

--------------------------------

RAG SYSTEM

Retrieves Relevant Chunk

Hemoglobin = 10.5 g/dL

Normal Range = 12-16 g/dL

--------------------------------

OUTPUT

Your hemoglobin appears lower
than the normal range.

This may indicate anemia.
Consult a healthcare professional
for proper evaluation.

Create Groq API Key

  1. Visit: https://console.groq.com
  2. Create Account
  3. Verify Email
  4. Login to Console
  5. Open API Keys Section
  6. Click Create API Key
  7. Copy Your Key
  8. Save It Securely

Google Colab Setup

  1. Open Google Colab
  2. Create New Notebook
  3. Rename Notebook
  4. Install Libraries
  5. Paste Code
  6. Run Application

Install Required Libraries


pip install langchain

pip install chromadb

pip install sentence-transformers

pip install groq

pip install pdfplumber

pip install gradio

pip install langchain-community

pip install langchain-text-splitters

Technologies Used

Technology Purpose
Python Programming Language
LangChain RAG Framework
ChromaDB Vector Database
Sentence Transformers Embeddings
Groq LLM API
Llama 3 Language Model
Gradio User Interface
PDFPlumber PDF Processing

Complete Project Workflow


PDF Upload

↓

Extract Text

↓

Chunking

↓

Embeddings

↓

Store in ChromaDB

↓

User Question

↓

Question Embedding

↓

Similarity Search

↓

Top Chunks Retrieved

↓

Groq Llama 3

↓

Final Answer

Core Implementation Logic


1. Load PDF

2. Extract Text

3. Create Chunks

4. Generate Embeddings

5. Store In ChromaDB

6. Accept User Question

7. Retrieve Similar Chunks

8. Send Context To Groq

9. Generate Answer

10. Display Result

Why Use RAG Here?

Uses Uploaded Reports
No Retraining Needed
Private Data Supported
Accurate Retrieval
Lower Cost
Real-Time Analysis

Future Enhancements

Frequently Asked Questions

What is RAG?

RAG combines retrieval and generation.


Why not directly use LLM?

LLMs may hallucinate or lack recent information.


Why ChromaDB?

It is simple and beginner friendly.


Why Embeddings?

They help computers understand meaning.


Can RAG work with private documents?

Yes. That is one of its biggest advantages.

Learning Outcomes

Learn with Mevi Technologies

Explore AI, Machine Learning, Deep Learning, Generative AI, Large Language Models, Agentic AI, RAG, Python, Data Science and Real World Projects.

Continue Learning Through The Mevi Notes Portal.

Visit Mevi Notes Portal

Congratulations

You have successfully completed the Retrieval-Augmented Generation learning journey.

You now understand: