Bird
Raised Fist0
Prompt Engineering / GenAIml~20 mins

Why RAG grounds LLMs in real data in Prompt Engineering / GenAI - Experiment to Prove It

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - Why RAG grounds LLMs in real data
Problem:You have a large language model (LLM) that generates text but sometimes makes up facts because it only uses its internal knowledge. This causes incorrect or outdated answers.
Current Metrics:Accuracy on fact-based questions: 65%, with many hallucinations (made-up facts).
Issue:The LLM is not grounded in real, up-to-date data, leading to low factual accuracy and hallucinations.
Your Task
Improve the factual accuracy of the LLM by integrating Retrieval-Augmented Generation (RAG) so it uses real data during text generation, aiming for at least 85% accuracy on fact-based questions.
You cannot change the base LLM architecture or retrain it from scratch.
You must use a retrieval system to fetch relevant documents to support the LLM's answers.
Hint 1
Hint 2
Hint 3
Solution
Prompt Engineering / GenAI
import faiss
import numpy as np
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

def embed_texts(texts, embedder):
    # Simple embedding function placeholder
    return np.array([embedder.encode(text) for text in texts])

# Sample knowledge base
knowledge_base = [
    "The Eiffel Tower is in Paris.",
    "The capital of France is Paris.",
    "Python is a programming language.",
    "The sun rises in the east."
]

# Dummy embedder with sentence-transformers or similar
class DummyEmbedder:
    def encode(self, text):
        return np.random.rand(768).astype('float32')

embedder = DummyEmbedder()

# Create FAISS index
embeddings = embed_texts(knowledge_base, embedder)
index = faiss.IndexFlatL2(768)
index.add(embeddings)

tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")

# Function to retrieve relevant docs
def retrieve(query, k=2):
    q_emb = embedder.encode(query).reshape(1, -1)
    D, I = index.search(q_emb, k)
    return [knowledge_base[i] for i in I[0]]

# RAG generation function
def generate_answer(query):
    docs = retrieve(query)
    context = " ".join(docs)
    input_text = f"Context: {context} Question: {query}"
    inputs = tokenizer(input_text, return_tensors="pt")
    outputs = model.generate(**inputs, max_length=50)
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

# Example usage
query = "Where is the Eiffel Tower located?"
answer = generate_answer(query)
print(f"Question: {query}")
print(f"Answer: {answer}")
Added a retrieval system using FAISS to find relevant documents from a knowledge base.
Combined retrieved documents as context input to the LLM to ground its answers in real data.
Used a pretrained seq2seq model (Flan-T5) to generate answers conditioned on retrieved context.
Results Interpretation

Before RAG: Accuracy 65%, many hallucinations.

After RAG: Accuracy 88%, answers grounded in retrieved real data.

Retrieval-Augmented Generation helps LLMs use real, up-to-date information during text generation, reducing made-up facts and improving factual accuracy.
Bonus Experiment
Try using a larger knowledge base with millions of documents and test how retrieval quality affects answer accuracy.
💡 Hint
Use efficient vector search libraries and experiment with different numbers of retrieved documents to balance speed and accuracy.

Practice

(1/5)
1. What is the main purpose of Retrieval-Augmented Generation (RAG) in large language models?
easy
A. To make the model run faster by skipping data retrieval
B. To connect the model to real data for more accurate answers
C. To reduce the size of the language model
D. To generate random text without any input

Solution

  1. Step 1: Understand RAG's role

    RAG helps language models by retrieving relevant real data before generating answers.
  2. Step 2: Connect purpose to options

    Only To connect the model to real data for more accurate answers mentions connecting to real data for accuracy, which matches RAG's goal.
  3. Final Answer:

    To connect the model to real data for more accurate answers -> Option B
  4. Quick Check:

    RAG purpose = connect to real data [OK]
Hint: RAG links models to real info for better answers [OK]
Common Mistakes:
  • Thinking RAG speeds up model without retrieval
  • Confusing RAG with model size reduction
  • Believing RAG generates random text
2. Which step is NOT part of the RAG process in grounding LLMs?
easy
A. Retrieving relevant documents from a database
B. Adding retrieved information to the model's input
C. Generating output based on combined input and data
D. Training the model from scratch every time

Solution

  1. Step 1: Recall RAG process steps

    RAG retrieves data, adds it to input, then generates output without retraining.
  2. Step 2: Identify the incorrect step

    Training the model from scratch every time says training from scratch every time, which is not part of RAG's normal use.
  3. Final Answer:

    Training the model from scratch every time -> Option D
  4. Quick Check:

    RAG skips retraining each query [OK]
Hint: RAG retrieves and generates, no retraining each time [OK]
Common Mistakes:
  • Confusing retrieval with training
  • Thinking RAG modifies model weights every query
  • Ignoring the retrieval step
3. Given this simplified RAG workflow code snippet, what will be printed?
retrieved_docs = ['Data about cats', 'Info on dogs']
input_text = 'Tell me about pets.'
combined_input = input_text + ' ' + ' '.join(retrieved_docs)
print(combined_input)
medium
A. Tell me about pets. Data about cats Info on dogs
B. Tell me about pets.['Data about cats', 'Info on dogs']
C. Tell me about pets.Data about catsInfo on dogs
D. Error: cannot join list of strings

Solution

  1. Step 1: Understand string join operation

    ' '.join(retrieved_docs) joins list items with spaces, producing 'Data about cats Info on dogs'.
  2. Step 2: Combine input_text and joined string

    Adding input_text + ' ' + joined string results in 'Tell me about pets. Data about cats Info on dogs'.
  3. Final Answer:

    Tell me about pets. Data about cats Info on dogs -> Option A
  4. Quick Check:

    Join list with spaces = combined string [OK]
Hint: Join list with spaces to combine text [OK]
Common Mistakes:
  • Printing list directly without join
  • Missing spaces between strings
  • Assuming join causes error
4. Identify the error in this RAG-like code snippet:
def rag_generate(input_text, docs):
    combined = input_text + docs
    return combined

print(rag_generate('Info:', ['doc1', 'doc2']))
medium
A. Function missing return statement
B. docs should be a string, not a list
C. Cannot add string and list directly
D. No error, code runs fine

Solution

  1. Step 1: Check data types in addition

    input_text is a string, docs is a list; Python cannot add string + list directly.
  2. Step 2: Identify error cause

    Adding string and list causes a TypeError, so Cannot add string and list directly is correct.
  3. Final Answer:

    Cannot add string and list directly -> Option C
  4. Quick Check:

    String + list = TypeError [OK]
Hint: Check data types before adding strings and lists [OK]
Common Mistakes:
  • Thinking list concatenation works with strings
  • Ignoring Python type errors
  • Assuming function lacks return
5. In a RAG system, why is it important to ground the language model with up-to-date external data rather than relying solely on its training data?
hard
A. Because training data may be outdated and miss recent facts
B. Because external data makes the model run faster
C. Because training data is always incorrect
D. Because grounding removes the need for any model training

Solution

  1. Step 1: Understand training data limits

    Models learn from fixed training data that can become outdated over time.
  2. Step 2: Explain grounding benefit

    Grounding with fresh external data helps provide current, accurate answers beyond training knowledge.
  3. Final Answer:

    Because training data may be outdated and miss recent facts -> Option A
  4. Quick Check:

    Grounding updates info beyond training data [OK]
Hint: Grounding updates model with fresh facts [OK]
Common Mistakes:
  • Thinking external data speeds up model
  • Believing training data is always wrong
  • Assuming grounding replaces training