Skip to content

What is RAG? Retrieval Augmented Generation Explained

DodaTech 1 min read

In this tutorial, you'll learn about What is RAG? Retrieval Augmented Generation Explained. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

What You'll Learn

Understand Retrieval Augmented Generation (RAG) — the most popular architecture for building LLM applications that use your own data.

Why It Matters

LLMs only know what they were trained on. RAG lets them answer questions about your private documents, codebase, or latest data without retraining.

Real-World Use

Customer support bots that read your knowledge base, code assistants that understand your internal APIs, and research tools that search your document library.

The Problem with LLMs Alone

LLMs have two major limitations:

  1. Training cutoff — GPT-4 doesn't know events after its training date
  2. No private knowledge — It can't access your company's internal documents

How RAG Works

User Question
     ↓
[Embedding Model] → Convert question to vector
     ↓
[Vector Database] → Find similar documents
     ↓
[LLM] → Generate answer using question + retrieved docs
     ↓
   Answer

Step 1: Convert your documents into embeddings (numerical vectors) Step 2: Store them in a vector database (Pinecone, Chroma, Weaviate) Step 3: When a user asks a question, convert it to a vector and find similar documents Step 4: Feed the question + retrieved documents to an LLM Step 5: Get an answer grounded in your data

Simple RAG with Python

from openai import OpenAI
import chromadb

client = OpenAI()
chroma = chromadb.Client()
collection = chroma.create_collection("docs")

# Add documents
collection.add(
    documents=["Our API rate limit is 100 requests per minute.",
               "Authentication requires a Bearer token.",
               "Refunds are processed within 5 business days."],
    ids=["1", "2", "3"]
)

# Query
question = "What's the rate limit?"
results = collection.query(query_texts=[question], n_results=1)

context = results["documents"][0][0]
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": f"Answer using this context: {context}"},
        {"role": "user", "content": question}
    ]
)
print(response.choices[0].message.content)

RAG vs Fine-Tuning

Aspect RAG Fine-Tuning
Updates Change the documents Retrain the model
Accuracy High (uses source docs) May hallucinate
Cost Low (no training) High (training compute)
Best for Facts, docs, knowledge base Tone, style, format

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro