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.
Python code
34 linesdef 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
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
- Use a scoring function based on TF-IDF or cosine similarity from scikit-learn for more realistic retrieval.
- 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
More from AI & LLM integration patterns
- Cache LLM Completions by Hashing the Prompt in Python easy
- Chain of Thought Prompting in Python: Step-by-Step Reasoning Demo easy
- Circuit Breaker Pattern in Python for LLM API Calls medium
- Cosine Similarity to Retrieve Top K Chunks in Python easy
- Demonstrate Prompt Injection Bypass in Python easy
- How to Accumulate Streamed Tokens into a Final String in Python easy
Keep learning
Related tutorials and quizzes for this topic.