Building a Simple RAG App in Python
Bring retrieval and generation together in a basic Python workflow that can later be connected to real models and databases.
Explanation
A minimal RAG app can use a simple retriever, a prompt builder, and a model call function.
Even a prototype should keep retrieval logic and generation logic separated.
Prototypes are useful because they reveal where evaluation and quality issues appear.
Why this topic matters in practice
In generative AI products, the model is only one part of the system. The surrounding workflow determines whether the output is useful, safe, and maintainable. This lesson matters because it helps you connect the idea to tasks such as tutoring, search, copilots, business assistants, and production automation.
Examples
Study helper
Retrieve relevant lesson chunks before generating an explanation for a student.
FAQ assistant
Search policy snippets, then answer only from those passages.
Internal help desk
Retrieve troubleshooting steps, then generate concise guidance for staff.
A minimal end-to-end RAG pipeline
The code below is intentionally concise so the underlying pattern stays clear. It focuses on the application logic you can reuse, even if you later switch model providers or deployment environments.
knowledge_base = [
"Machine learning uses data to find patterns.",
"Supervised learning uses labeled examples.",
"Overfitting happens when a model memorizes training data."
]
def retrieve(question):
question = question.lower()
return [item for item in knowledge_base if any(word in item.lower() for word in question.split())]
def build_prompt(question, passages):
evidence = "\n".join(passages) if passages else "No evidence found."
return f"Use this evidence to answer the question:\n{evidence}\n\nQuestion: {question}"
question = "What is supervised learning?"
passages = retrieve(question)
prompt = build_prompt(question, passages)
print("Retrieved passages:", passages)
print("\nPrompt:\n", prompt)How the coding section works
- The retriever is intentionally simple so the full pipeline is easy to understand.
- You can replace the retriever later with embeddings and a vector database.
- This kind of prototype is ideal for learning the moving parts before scaling up.
Implementation advice
When turning this lesson into a real feature, think beyond the code snippet itself. Decide what inputs should be allowed, how you will validate outputs, how you will recover from errors, and how you will measure whether the feature is actually helping users. Those surrounding choices often determine whether an AI feature feels polished or unreliable.
Summary / key takeaways
- A working RAG system combines retrieval, prompt construction, and generation.
- Building a simple prototype helps you understand failure points quickly.
- Start simple, measure quality, and improve one layer at a time.
Exercises
- Add one more knowledge base sentence and test a new question.
- Why is it useful to separate the retrieve function from the build_prompt function?
- How would you improve the retrieval logic in this example?