Introduction

Welcome to our second lesson in the "Introduction to RAG" course! In the previous lesson, you learned how RAG evolved from traditional Information Retrieval (IR). Today, we'll connect those ideas to concrete code and illustrate a simple end-to-end RAG workflow. By the end of this lesson, you'll see how indexing, retrieval, prompt augmentation, and final text generation fit together to produce a targeted answer.

In this lesson, we showcase a scenario involving "Project Chimera." Think of this as an internal project of a company — here, it's just an example to demonstrate how a naive, context-free system can invent inaccurate details, whereas a RAG-based system will provide reliable answers using information from an authoritative knowledge base. Please note that we are deliberately using extremely simplified methods (like simple keyword matching) to illustrate how each part of a RAG pipeline can work. Later in this course, we will explore more realistic and robust approaches — such as using embeddings and vector databases — for each component of our RAG pipeline.

The RAG Workflow: Four Key Steps

Before diving into the code, let's take a quick look at the four primary steps of our simple RAG workflow:

  • Indexing: Documents are structured in a way that makes them easy to search.
  • Retrieval: The most relevant piece of text is fetched based on a user query.
  • Prompt (Query) Augmentation: The retrieved text is combined with the user's question to form a context-rich prompt.
  • Generation: A language model processes the prompt and produces a final answer anchored to the provided text.

This process ensures that answers are backed by your data, reducing the risk of fabricated or off-topic responses. Let's examine each step through code!

Indexing: Organizing Documents

We start by defining our knowledge base. Below, "Project Chimera" serves as the example domain:

const KNOWLEDGE_BASE = {
    "doc1": {
        "title": "Project Chimera Overview",
        "content": (
            "Project Chimera is a research initiative focused on developing " +
            "novel bio-integrated interfaces. It aims to merge biological " +
            "systems with advanced computing technologies."
        )
    },
    "doc2": {
        "title": "Chimera's Neural Interface",
        "content": (
            "The core component of Project Chimera is a neural interface " +
            "that allows for bidirectional communication between the brain " +
            "and external devices. This interface uses biocompatible " +
            "nanomaterials."
        )
    },
    "doc3": {
        "title": "Applications of Chimera",
        "content": (
            "Potential applications of Project Chimera include advanced " +
            "prosthetics, treatment of neurological disorders, and enhanced " +
            "human-computer interaction. Ethical considerations are paramount."
        )
    }
};

Here's a quick breakdown:

  • We define a JavaScript object named KNOWLEDGE_BASE that contains multiple documents.
  • Each entry has an ID (e.g., "doc1") and both a title and content field.
  • "Project Chimera" information is now the authoritative data source for the RAG system.

Keep in mind this is a very simplified approach for educational purposes; in a real-world production scenario, your KNOWLEDGE_BASE would consist of more advanced components, such as a Vector Database. But don't worry, we'll be dealing with these databases in Course 3!

Retrieval: Locating Relevant Information

Next, we create a function to return the best document from our knowledge base, based on simple keyword overlap:

function ragRetrieval(query, documents) {
    const queryWords = new Set(query.toLowerCase().split(' '));
    let bestDocId = null;
    let bestOverlap = 0;
    
    for (const [docId, doc] of Object.entries(documents)) {
        const docWords = new Set(doc.content.toLowerCase().split(' '));
        const overlap = [...queryWords].filter(word => docWords.has(word)).length;
        
        if (overlap > bestOverlap) {
            bestOverlap = overlap;
            bestDocId = docId;
        }
    }
    
    return documents[bestDocId] || null;
}

Let's walk through the code:

  • The query is split into lowercase words and stored in a set.
  • Each document's text is similarly tokenized.
  • The function picks the document with the greatest word overlap.
  • If no match is found, it returns null.
Query Augmentation: Creating Context-Rich Prompts
Generation: Producing Tailored Answers
Conclusion and Next Steps

You've now implemented:

  • A simple knowledge base indexing scheme.
  • Basic retrieval to find the most relevant document.
  • Prompt augmentation to combine user queries and reference data.
  • Generation that relies on actual context, lowering the chance of hallucinations.

Up next, you'll get hands-on practice with these steps in coding exercises. As you progress, you'll see how RAG can be extended to tackle more complex tasks and domains — whether you're talking about a literal "Project Chimera" or a real-world internal project.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal