A concrete, working RAG pipeline — from raw document to a grounded answer — with a real (simplified) implementation showing how the pieces actually connect in code.
Ingestion Pipeline (Runs Once Per Document)
def ingest_document(file_path):
raw_text = extract_text(file_path) # see Document Processing
chunks = chunk_text(raw_text, chunk_size=500,
overlap=50) # see Chunking Strategies
for chunk in chunks:
embedding = embed(chunk.text)
vector_db.upsert(
id=chunk.id,
vector=embedding,
metadata={"source": file_path, "text": chunk.text}
)
Query Pipeline (Runs Per User Question)
def answer_question(user_question):
query_embedding = embed(user_question)
retrieved_chunks = vector_db.query(
vector=query_embedding, top_k=5
) # see Vector Retrieval
context = "\n\n".join(c.metadata["text"] for c in retrieved_chunks)
prompt = f"""
Answer the question using ONLY the context below. If the
answer isn't in the context, say so.
Context:
{context}
Question: {user_question}
""" # see RAG Prompt
response = llm_client.generate(prompt=prompt)
return response.text
Where Real Systems Add More
This simplified version omits several things production systems typically add: reranking retrieved chunks before building the prompt (see Reranking), metadata filtering (see Metadata Filtering), citation tracking (which chunk supported which part of the answer), and evaluation/monitoring of retrieval and answer quality (see RAG Evaluation).
Practical Use Case
This exact two-function pattern (ingest once, query many times) is the backbone of nearly every document-QA, internal-knowledge-search, or customer-support-bot application built on RAG — the specific details (chunking strategy, prompt wording, reranking) are what differentiate a good implementation from a mediocre one.
Common Mistakes
- No instruction telling the model to acknowledge when the context doesn't contain the answer — without this, it may fabricate a plausible-sounding response instead
- Not tracking which source document/chunk supported the final answer, making it impossible to show citations or debug a wrong answer
- Skipping error handling for empty retrieval results (no relevant chunks found) — the pipeline needs an explicit path for "nothing relevant was retrieved"
Interview Relevance
"Write pseudocode for a basic RAG pipeline" — a very common practical interview exercise; the ingest/query split above, including the explicit "don't know" instruction in the prompt, is the expected shape.
Practice Question
Extend the query pipeline above to handle the case where retrieved_chunks is empty (no relevant content found).