Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Generative AI Notes
Topic #1103

RAG Pipeline (with Code)

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).

Related Notes

Want to go beyond the notes?

Join CodingNow's Generative AI course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →