How to build a mock RAG pipeline in Python

Build a minimal Retrieval-Augmented Generation pipeline that retrieves the best-matching document by keyword overlap and generates a template-based answer.

Easy Python 3.9+ Aug 9, 2026 AI & LLM integration patterns 14 views 0 copies

Python code

34 lines
Python 3.9+
def simple_rag_pipeline(question, documents):
    """
    A minimal mock RAG pipeline: retrieve relevant context, then generate an answer.
    """
    # Step 1: Retrieve — mock retrieval by simple keyword scoring
    scores = []
    for doc in documents:
        doc_words = set(doc.lower().split())
        question_words = set(question.lower().split())
        overlap = len(doc_words & question_words)
        scores.append(overlap)
    
    # Get the most relevant document(s)
    best_idx = max(range(len(scores)), key=lambda i: scores[i])
    retrieved_context = documents[best_idx] if scores[best_idx] > 0 else "No relevant documents found."

    # Step 2: Generate — mock generation using template
    answer = (
        f"Based on the retrieved context: \"{retrieved_context}\"\n"
        f"Answer to '{question}': "
        f"The best matching document has {scores[best_idx]} keyword overlaps."
    )
    return answer


if __name__ == "__main__":
    docs = [
        "Python is a high-level programming language.",
        "RAG stands for Retrieval-Augmented Generation.",
        "Mock pipelines simulate real system behavior without expensive components."
    ]
    question = "What is RAG?"
    result = simple_rag_pipeline(question, docs)
    print(result)

Output

stdout
Based on the retrieved context: "RAG stands for Retrieval-Augmented Generation."
Answer to 'What is RAG?': The best matching document has 1 keyword overlaps.

How it works

This function emulates a two-stage RAG flow: retrieve then generate. The retrieval step scores each document by counting how many unique words it shares with the question, then picks the highest-scoring one. Generation is mocked with an f-string that embeds the retrieved context and the overlap count, mimicking how a real LLM would incorporate retrieved content. This approach is useful for prototyping and testing RAG workflows before wiring in actual search and language models.

Common mistakes

  • Forgetting to lowercase text before splitting, causing case-sensitive mismatches.
  • Using `max` directly on scores list without a tie-breaking strategy, which may return arbitrary indexes.
  • Not handling the case where no document has any overlap, resulting in an empty context.
  • Hardcoding questions or documents instead of passing them as parameters.

Variations

  1. Use a scoring function based on TF-IDF or cosine similarity from scikit-learn for more realistic retrieval.
  2. Replace the template generation with a call to an LLM API like OpenAI to produce the answer.

Real-world use cases

  • Prototyping a RAG-based customer support bot before integrating expensive vector search and LLM calls.
  • Unit-testing internal retrieval logic with a mock generator to validate pipeline flow without external dependencies.
  • Creating a quick demo for stakeholders to illustrate how RAG could answer domain-specific questions.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from AI & LLM integration patterns

Related tutorials and quizzes for this topic.