Skip to main content
In this lesson we’ll build a simple retrieval-augmented generation (RAG) pipeline that constrains a language model to answer using only retrieved documents. We’ll use two TechCrunch articles as the source documents:
  • “Anthropic claims its new models beat GPT-4” (March)
  • “AI21 Labs’ new text-generating AI model is more efficient than most” (March)
Workflow overview: load the articles from the web, split them into smaller chunks, create embeddings for those chunks, index them in an in-memory FAISS vector store, and then run a retrieval chain that supplies retrieved context to an LLM with instructions to answer only from that context. Key benefits of this pattern:
  • Keeps LLM input within the model context window.
  • Reduces hallucination by restricting answers to retrieved text.
  • Makes long-document QA, summarization, and fact-checking more reliable.
Below is a consolidated, corrected, and cleaned-up implementation that follows the same sequence as the original lesson.
FAISS (Facebook AI Similarity Search) is an in-memory vector index that provides fast nearest-neighbor search. It is non-persistent by default—use a disk-backed store (for example, Chroma) or persist FAISS manually for production workflows that need durability.
What the retrieval chain does
  • The retriever issues a nearest-neighbor search over the vector store to find the most relevant chunks for a query.
  • The combiner (here, a “stuff” chain) concatenates the retrieved chunks into the context variable passed to the PromptTemplate.
  • The LLM then answers the user query constrained by that context and the template instruction.
Example expected output (from the provided TechCrunch articles):
The chain returns the model’s answer together with metadata (for example, which chunks were retrieved), which helps with debugging and traceability. This mirrors the standard RAG pattern: retrieve relevant context, then synthesize the response using the LLM.
The image shows a JupyterLab interface displaying code in a Python notebook. The code appears to be related to AI models and their specifications, mentioning Anthropic and Meta's Llama 2.
Best practices and tuning
  • Chunk sizing:
    • Default example: chunk_size=200, chunk_overlap=50.
    • For long, dense documents, increase chunk_size. For short or highly topical documents, use smaller chunks.
  • Temperature:
    • Use temperature=0.0 for deterministic answers constrained to retrieved context.
    • Increase temperature only for creative tasks where you are not strictly relying on retrieved facts.
  • Persistence and scaling:
    • For experiments and prototypes, in-memory FAISS is convenient.
    • For production or multi-run persistence, use a disk-backed or managed vector database.
  • Prompt flexibility:
    • Construct prompt_template programmatically when you need to inject dynamic instructions or system messages before creating the PromptTemplate and chain.
Tuning quick reference Links and references Notes
  • The example focuses on a minimal, reproducible retrieval chain using LangChain-style primitives. Adapt components (loader, splitter, embeddings, vector store, combiner) to your infrastructure and scale requirements.
  • Always test with your target documents and queries to find the best chunk size, overlap, and retriever configuration for accuracy and latency.

Watch Video

Practice Lab