Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† AI and Automation Level Two ยท Lesson 6 of 9

Module Five

๐Ÿ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
6
Module 5 ยท AI and Automation Level Two

Module 5: Retrieval-Augmented Generation (RAG)

Module Introduction

Welcome to Module 5 of "AI and Automation Level Two"! In Module 1, we reviewed the basics. In Module 2, we built advanced automation workflows. In Module 3, we learned advanced prompt engineering. In Module 4, we explored AI Agents. Now, we are going to learn about a powerful technique called Retrieval-Augmented Generation, or RAG for short.

Have you ever asked an AI a question, and it gave a good answer but not a perfect one? That happens because the AI only knows what it was trained on. It does not know your specific information. RAG solves this problem. It lets the AI look up information from your own documents or databases before answering. It is like giving the AI a library to search through.

Think of RAG like a student who is taking a test. A normal AI is like a student who can only use their memory. A RAG AI is like a student who can also look at their notes and textbooks. The student with the notes will usually get better answers. RAG makes AI smarter by giving it access to knowledge.

In this module, we will learn what RAG is, how it works, and how to build it. We will learn about embeddings, vector databases, and retrieval. By the end of this module, you will be able to build a document-based assistant that can answer questions using your own documents. Let us get started!


Learning Objectives

By the end of this module, you will be able to:

  • Explain what Retrieval-Augmented Generation (RAG) is in simple words.
  • Understand why RAG is important for getting accurate answers.
  • Understand the role of embeddings and vector databases.
  • Describe the RAG pipeline โ€“ indexing and retrieval.
  • Understand chunking and why it matters.
  • Build a simple document-based assistant.
  • Evaluate RAG performance and accuracy.

Warm-up Story: The Smart Library

In a small town in Enugu, there was a library that was very special. It had a librarian named Ada. Ada was not just any librarian. She had a superpower. She could remember every single book in the library. If you asked her a question, she could recall the exact book and page with the answer.

One day, a new librarian came to help. His name was Chidi. Chidi did not have a superpower. But he was very smart. He created a system to help him answer questions. He scanned every book and stored the text on a computer. He used AI to understand the text. Now, when someone asked a question, the AI would search through all the text and find the best answer.

People were amazed. They could ask any question, and the system would find the answer. It was like having a superpower, but it was technology.

That is exactly what RAG does. It takes all your documents, stores them in a smart way, and lets the AI search through them to find answers. It is like having a super-smart librarian for your own information.

What can we learn from this story? RAG is like a super-smart librarian. It takes your documents and lets AI search through them to answer questions accurately.


Main Lessons

Lesson 1: What is RAG?

RAG stands for Retrieval-Augmented Generation. It is a way to make AI smarter by giving it access to external knowledge.

Definition: RAG is a technique that combines retrieval (searching) with generation (creating answers).

Why it is important: RAG helps AI give accurate, up-to-date answers based on your own documents.

Simple explanation: Imagine you have a test. You can only use your memory. That is like a normal AI. RAG is like having a textbook you can look at during the test.

Real-life example: A customer service AI that can search through company policies to answer customer questions.

School example: An AI that can search through your textbooks to help you with homework.

Home example: An AI that can search through your recipes to suggest a meal.

Nigerian example: An AI that helps farmers by searching through agricultural manuals.

Illustration (ASCII):

   +-------------------+
   |   RAG             |
   +-------------------+
   | Retrieval (Search)|
   | +                 |
   | Generation (AI)   |
   | = Better answers  |
   +-------------------+

Mini summary: RAG combines retrieval and generation. It gives AI access to external knowledge for better answers.


Lesson 2: Why Do We Need RAG?

Normal AI models are trained on a fixed set of data. They do not know new things. They do not know your personal documents. RAG solves this problem.

Definition: RAG gives AI access to up-to-date and specific information.

Why it is important: Without RAG, AI can give outdated or wrong answers. RAG makes AI more reliable.

Simple explanation: Imagine your friend only knows things from one book. If you ask a question not in that book, they cannot answer. RAG gives your friend more books to read.

Real-life example: A medical AI that uses RAG to search through the latest research.

School example: An AI that uses RAG to search through your class notes.

Home example: An AI that uses RAG to search through your family recipes.

Nigerian example: An AI that uses RAG to search through local farming guides.

Illustration (ASCII):

   +-------------------+
   |  WITHOUT RAG      |
   | AI only knows     |
   | what it was       |
   | trained on        |
   +-------------------+
   |  WITH RAG         |
   | AI can search     |
   | your documents    |
   +-------------------+

Mini summary: RAG helps AI give better answers by using your own documents and data.


Lesson 3: How RAG Works โ€“ The Pipeline

RAG has two main steps: Indexing and Retrieval. First, you prepare your documents. Then, you search them.

Definition: The RAG pipeline is the process of making documents searchable and then finding answers.

Why it is important: Understanding the pipeline helps you build RAG systems.

Simple explanation: Imagine you have a book. First, you create an index (like the index at the back of the book). Then, when you have a question, you look up the answer in the index.

Real-life example: Google works like RAG. It indexes the web and then retrieves results for your search.

School example: A library catalog. The catalog is the index. You search it to find books.

Home example: A recipe book with a list of recipes and an index by ingredient.

Nigerian example: A farm management system that indexes all farm data and lets you search for information.

Illustration (ASCII):

   +-------------------+
   |   RAG PIPELINE    |
   +-------------------+
   | 1. INDEXING       |
   |   (Prepare docs)  |
   | 2. RETRIEVAL      |
   |   (Search docs)   |
   | 3. GENERATION     |
   |   (AI answers)    |
   +-------------------+

Mini summary: The RAG pipeline has two main steps: Indexing (preparing) and Retrieval (searching).


Lesson 4: Indexing โ€“ Preparing Your Documents

Indexing is the process of making your documents searchable. You break them into pieces and store them in a special way.

Definition: Indexing is preparing documents so that an AI can search them quickly.

Why it is important: Good indexing leads to fast and accurate searches.

Simple explanation: Imagine you are organizing a library. You put books on shelves and create a catalog. That is indexing.

Real-life example: A search engine like Google indexes the web.

School example: A student who organizes their notes by subject and date.

Home example: Organizing your photos by date and people in them.

Nigerian example: A farmer who organizes crop data by type, date, and location.

Illustration (ASCII):

   +-------------------+
   |   INDEXING        |
   +-------------------+
   | 1. Documents      |
   | 2. Break into     |
   |    chunks         |
   | 3. Create         |
   |    embeddings     |
   | 4. Store in       |
   |    database       |
   +-------------------+

Mini summary: Indexing is preparing your documents for search by breaking them into pieces and storing them.


Lesson 5: Chunking โ€“ Breaking Documents into Pieces

Chunking is breaking large documents into smaller pieces. This makes them easier to search.

Definition: Chunking is splitting documents into smaller parts.

Why it is important: Small chunks are easier to search and lead to more accurate answers.

Simple explanation: Imagine you have a long book. Instead of searching the whole book, you search one chapter at a time.

Real-life example: A search engine that breaks web pages into smaller pieces.

School example: Studying by reading one chapter at a time.

Home example: Breaking a large recipe into smaller steps.

Nigerian example: Breaking a farm report into sections by crop type.

Illustration (ASCII):

   +-------------------+
   |   CHUNKING        |
   +-------------------+
   | Big document      |
   |     |             |
   |     v             |
   | Chunk 1, Chunk 2, |
   | Chunk 3, ...      |
   +-------------------+

Mini summary: Chunking breaks large documents into smaller pieces for better searching.


Lesson 6: Embeddings โ€“ Understanding Meaning

Embeddings are a way to convert text into numbers. This helps AI understand the meaning of words.

Definition: Embeddings are numerical representations of text that capture meaning.

Why it is important: AI can only understand numbers. Embeddings let AI understand text.

Simple explanation: Imagine you assign a number to each word. The number represents the meaning. Words with similar meanings have similar numbers.

Real-life example: AI uses embeddings to understand "king" and "queen" are related.

School example: A student who creates a mind map to connect ideas.

Home example: Organizing your music by genre using a playlist.

Nigerian example: An AI that groups similar crops using embeddings.

Illustration (ASCII):

   +-------------------+
   |   EMBEDDINGS      |
   +-------------------+
   | Word -> Numbers   |
   | "Cat" -> [0.1,    |
   |         0.2, ...] |
   | "Dog" -> [0.1,    |
   |         0.3, ...] |
   +-------------------+

Mini summary: Embeddings convert text into numbers that represent meaning.


Lesson 7: Vector Databases โ€“ Storing Embeddings

A vector database is a special database that stores embeddings. It is designed to search through them quickly.

Definition: A vector database stores and searches numerical representations (embeddings).

Why it is important: Vector databases make RAG fast and efficient.

Simple explanation: Imagine you have a huge box of numbers. A vector database is like a super-fast filing system that finds the numbers you want.

Real-life example: Pinecone and Weaviate are vector databases.

School example: A library catalog that is organized to find books quickly.

Home example: A phone contact list that finds names instantly.

Nigerian example: A database of farm data organized by crop type.

Illustration (ASCII):

   +-------------------+
   |  VECTOR DATABASE  |
   +-------------------+
   | Embeddings ->     |
   | Search quickly    |
   | Example: Pinecone |
   +-------------------+

Mini summary: A vector database stores and searches embeddings quickly.


Lesson 8: Retrieval โ€“ Finding the Right Information

Retrieval is the process of finding the most relevant chunks for a question. It uses similarity search.

Definition: Retrieval is searching for the most relevant information.

Why it is important: Good retrieval finds the best information for the AI to use.

Simple explanation: Imagine you have a question. You look through a box of cards and find the card with the best answer.

Real-life example: A search engine that finds the best web pages for your question.

School example: Looking through your notes to find the information you need.

Home example: Looking for a specific recipe in a cookbook.

Nigerian example: A farmer looking for information about a specific crop in their farm records.

Illustration (ASCII):

   +-------------------+
   |   RETRIEVAL       |
   +-------------------+
   | Question ->       |
   | Search chunks     |
   | Find best match   |
   +-------------------+

Mini summary: Retrieval finds the most relevant information for a question.


Lesson 9: Generation โ€“ Creating the Answer

Generation is the final step. The AI takes the retrieved chunks and creates a final answer.

Definition: Generation is the AI creating an answer based on the retrieved chunks.

Why it is important: Generation combines all the information into a clear, concise answer.

Simple explanation: Imagine you have all the pieces. You put them together to make a complete picture.

Real-life example: An AI that summarizes multiple articles into one report.

School example: Writing a report based on research from multiple sources.

Home example: Putting together a meal plan from different recipes.

Nigerian example: An AI that combines farm data to give a single recommendation.

Illustration (ASCII):

   +-------------------+
   |   GENERATION      |
   +-------------------+
   | Chunks ->         |
   | AI creates        |
   | Final answer      |
   +-------------------+

Mini summary: Generation is the AI creating a final answer from the retrieved chunks.


Lesson 10: Building a Document-Based Assistant

Now, let us build a simple document-based assistant. It will take documents, index them, and answer questions.

Definition: A document-based assistant uses RAG to answer questions from documents.

Why it is important: It helps you quickly find information in your documents.

Simple explanation: You upload your documents. You ask questions. The assistant finds the answers.

Real-life example: A company uses a document assistant to search through its policies.

School example: A student uses an assistant to search through their textbooks.

Home example: A family uses an assistant to search through recipes.

Nigerian example: A farmer uses an assistant to search through agricultural manuals.

Illustration (ASCII):

   +-------------------+
   | DOCUMENT ASSISTANT|
   +-------------------+
   | 1. Upload docs    |
   | 2. Index them     |
   | 3. Ask questions  |
   | 4. Get answers    |
   +-------------------+

Mini summary: A document-based assistant uses RAG to answer questions from your documents.


Key Vocabulary

Word Simple Definition
RAG A technique that combines retrieval and generation.
Indexing Preparing documents for search.
Chunking Breaking documents into smaller pieces.
Embedding Converting text into numbers that represent meaning.
Vector Database A database that stores and searches embeddings.
Retrieval Finding the most relevant chunks.
Generation Creating an answer from retrieved chunks.
Pipeline The entire RAG process.
Similarity Search Finding chunks that are similar to a question.
Document Assistant A tool that uses RAG to answer questions from documents.

Important Concepts

  • RAG combines retrieval and generation to give better answers.
  • Indexing prepares documents for search.
  • Chunking breaks documents into smaller pieces.
  • Embeddings convert text into numbers that represent meaning.
  • Vector databases store and search embeddings.
  • Retrieval finds the most relevant chunks.
  • Generation creates the final answer.
  • A document-based assistant uses RAG to answer questions from documents.

Step-by-step Explanations

Step-by-Step: Building a RAG Pipeline

  1. Collect documents: Gather all the documents you want to use.
  2. Chunk documents: Break them into smaller pieces.
  3. Create embeddings: Convert the chunks into numbers.
  4. Store embeddings: Save them in a vector database.
  5. Ask a question: The user asks a question.
  6. Retrieve chunks: Find the most relevant chunks.
  7. Generate answer: The AI creates the final answer.

Real-life Examples

  • Example 1: A customer support system that uses RAG to search through help articles.
  • Example 2: A legal AI that searches through case law to help lawyers.
  • Example 3: A medical AI that searches through research papers to help doctors.

Nigerian Examples

  • Example 1: A farming assistant that searches through agricultural guides.
  • Example 2: An education assistant that searches through textbooks for students.
  • Example 3: A government assistant that searches through policy documents.

Fun Examples Children Can Relate To

  • Example 1: A homework assistant that searches through your school books.
  • Example 2: A game assistant that searches through game manuals.
  • Example 3: A story assistant that searches through all your favourite books.

Everyday Examples

  • Example 1: A recipe assistant that searches through cookbooks.
  • Example 2: A home assistant that searches through user manuals.
  • Example 3: A travel assistant that searches through travel guides.

Parent Tips

  • Tip 1: Explain RAG as giving the AI a library to search through.
  • Tip 2: Help your child collect documents for a personal RAG project.
  • Tip 3: Discuss the importance of good indexing for quick searches.
  • Tip 4: Encourage your child to test different chunk sizes.
  • Tip 5: Talk about how RAG can be used in school projects.

Interesting Facts

  • RAG was introduced by Facebook AI in 2020.
  • RAG can be used with any type of document โ€“ text, PDFs, websites.
  • Vector databases are a key part of modern AI systems.
  • RAG is used by many companies to improve their AI systems.

Did You Know?

  • Did you know that RAG can be used to search through millions of documents?
  • Did you know that embeddings can capture relationships between words?
  • Did you know that vector databases are used in many real-world applications?

Remember This

  • RAG combines retrieval and generation.
  • Indexing prepares documents for search.
  • Chunking breaks documents into pieces.
  • Embeddings convert text into numbers.
  • Vector databases store and search embeddings.
  • Retrieval finds relevant chunks.
  • Generation creates the final answer.
  • A document-based assistant uses RAG to answer questions.

Common Mistakes

  • Mistake 1: Not chunking documents.
    Correction: Always chunk documents for better retrieval.
  • Mistake 2: Using too large chunks.
    Correction: Use smaller chunks for more accurate searches.
  • Mistake 3: Not using embeddings.
    Correction: Embeddings are essential for meaning-based search.
  • Mistake 4: Not using a vector database.
    Correction: Vector databases are optimized for fast searches.

Best Practices

  • Use good quality documents.
  • Choose the right chunk size.
  • Use embeddings that capture meaning well.
  • Use a vector database for fast retrieval.
  • Test your RAG system with real questions.
  • Evaluate performance regularly.

ASCII Illustrations, Diagrams, Flowcharts, Timelines, Tables

Diagram: The RAG Pipeline

   +----------+      +----------+      +----------+      +----------+
   | DOCUMENTS|----->| CHUNKING |----->|EMBEDDINGS|----->|  VECTOR  |
   |          |      |          |      |          |      | DATABASE |
   +----------+      +----------+      +----------+      +----------+
                                                               |
                                                               v
   +----------+      +----------+      +----------+      +----------+
   |  ANSWER  |<-----| GENERATE |<-----| RETRIEVE |<-----| QUESTION |
   |          |      |          |      |          |      |          |
   +----------+      +----------+      +----------+      +----------+

Flowchart: RAG Decision Process

   +-------------------+
   |  Question asked   |
   +-------------------+
          |
          v
   +-------------------+
   |  Retrieve chunks  |
   |  (Search vector   |
   |   database)       |
   +-------------------+
          |
          v
   +-------------------+
   |  Are there good   |
   |  chunks?          |
   +-------------------+
          /          \
        Yes           No
         |             |
         v             v
+----------------+ +----------------+
|  Generate      | |  Ask for       |
|  answer        | |  clarification |
+----------------+ +----------------+

Comparison Table: Normal AI vs RAG

Feature Normal AI AI with RAG
Knowledge Fixed training data Can access external documents
Up-to-date Only as of training Can use current documents
Personalization No Can use personal documents
Accuracy May be inaccurate More accurate


End-of-Module Summary

Congratulations! You have completed Module 5 of "AI and Automation Level Two." Let us review what we covered.

  • We learned what RAG is โ€“ a technique that combines retrieval and generation.
  • We learned why RAG is important โ€“ it gives AI access to external knowledge.
  • We learned about indexing โ€“ preparing documents for search.
  • We learned about chunking โ€“ breaking documents into pieces.
  • We learned about embeddings โ€“ converting text into numbers.
  • We learned about vector databases โ€“ storing and searching embeddings.
  • We learned about retrieval โ€“ finding relevant chunks.
  • We learned about generation โ€“ creating the final answer.
  • We built a document-based assistant using RAG.

You are now ready for Module 6. In the next module, we will dive into Tool Integration and Agent Capabilities. We will learn how to give agents tools and abilities to do even more. Get ready for some exciting challenges!


Frequently Asked Questions

  1. Q: What is RAG?
    A: RAG is a technique that combines retrieval and generation.
  2. Q: Why is RAG important?
    A: It helps AI give better, more accurate answers.
  3. Q: What is indexing?
    A: Preparing documents for search.
  4. Q: What is chunking?
    A: Breaking documents into smaller pieces.
  5. Q: What are embeddings?
    A: Converting text into numbers that represent meaning.
  6. Q: What is a vector database?
    A: A database that stores and searches embeddings.
  7. Q: What is retrieval?
    A: Finding the most relevant chunks.
  8. Q: What is generation?
    A: Creating the final answer from chunks.
  9. Q: What is a document-based assistant?
    A: A tool that uses RAG to answer questions from documents.
  10. Q: Can I build my own RAG system?
    A: Yes! You can start with a simple document assistant.

Matching Exercises

Match the word on the left with its correct definition on the right.

Word Definition
1. RAG A. Breaking documents into pieces.
2. Chunking B. Combines retrieval and generation.
3. Embedding C. A database for storing numbers.
4. Vector Database D. Converting text into numbers.
5. Retrieval E. Finding the most relevant chunks.

Answers: 1-B, 2-A, 3-D, 4-C, 5-E


Scenario-based Exercises

  1. Scenario 1: You want to build a RAG system for a school library. What documents would you use? How would you index them?
  2. Scenario 2: You want to build a RAG system for a farm. What documents would you use? How would you chunk them?
  3. Scenario 3: You want to build a RAG system for a business. What documents would you use? How would you handle different file types?
  4. Scenario 4: You want to build a RAG system for your personal use. What documents would you use? How would you evaluate its performance?

Group Activity

Activity: In groups of 3-4, design a RAG system for a specific use case. Choose a domain (e.g., education, farming, business). Describe the documents, indexing, retrieval, and generation. Present your design to the class.


Individual Activity

Activity: Build a simple document-based assistant using a no-code RAG tool. Upload some documents and test it. Write a report on what you learned.


Mini Project

Project: Build a RAG system for a domain of your choice. Collect documents, index them, and create an assistant. Test it and evaluate its performance.


Practical Assignment

Assignment: Research a real-world application of RAG. Write a report on what it does, how it works, and what impact it has.


Key Takeaways

  • RAG combines retrieval and generation.
  • Indexing prepares documents for search.
  • Chunking breaks documents into pieces.
  • Embeddings convert text into numbers.
  • Vector databases store and search embeddings.
  • Retrieval finds relevant chunks.
  • Generation creates the final answer.
  • A document-based assistant uses RAG to answer questions.

Classroom Discussion Questions

  1. What is the most important part of a RAG system? Why?
  2. How can RAG be used in your community?
  3. What are some challenges in building a RAG system?
  4. How can RAG help with education?
  5. What is the future of RAG?

Preparation for the Next Module

In Module 6, we will dive into Tool Integration and Agent Capabilities. We will learn how to give agents tools and abilities to do even more. To prepare, think about a tool you would like your agent to use. It could be a calculator, a search engine, or even a camera.

See you in Module 6!


Note: This is the end of Module 5. You are now ready for Module 6, where we will dive into tool integration and agent capabilities. Keep your curiosity alive!

๐Ÿ† Get Certified

๐Ÿ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (โ‚ฆ2,000 or 20,000 coins), and get a verified certificate with your name on it.

๐ŸŽ“ Sign Up to Get Certified
๐Ÿ› ๏ธ Practice Tools
Hands-on simulators & labs - subscription required.
โ†’
๐ŸŽฏ Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
โ†’