Black Friday50% off your first month on any monthly plan with EARLY50 See plans
All 29 lessons
  1. Foundations: what RAG is and a working pipeline

    1. 1. Getting started: what RAG is and when to use it
    2. 2. Types of RAG: a map from naive to agentic
    3. 3. Lab: build the smallest RAG pipeline end to end
    4. 4. How embeddings and vector search find meaning
    5. 5. Document types: what each one breaks, and how to prepare it
  2. Better retrieval: chunking and search

    1. 6. Chunking documents so answers survive
    2. 7. Chunking strategies compared: which to use when
    3. 8. Keyword search vs vector search, and fusing both
    4. 9. Retrieval algorithms compared: which to use when
    5. 10. Reranking the top results before the model reads them
    6. 11. Rewriting the question before you search
  3. Answers you can trust, measure and debug

    1. 12. Lab: grounded answers with citations and refusals
    2. 13. Measuring a RAG system with an evaluation set
    3. 14. Lab: evaluation methods and frameworks: Ragas and DeepEval
    4. 15. Troubleshooting a RAG system, symptom by symptom
  4. Production in a company

    1. 16. Access control: only retrieve what the user may see
    2. 17. Defending against prompt injection in retrieved text
    3. 18. Running RAG in production: freshness, cost and monitoring
    4. 19. Real-world RAG: what worked, what failed, and why
  5. Agentic RAG

    1. 20. Lab: from a fixed pipeline to an agent with a search tool
    2. 21. Self-correcting retrieval: grade, retry, fall back
    3. 22. GraphRAG and routing across many sources
    4. 23. Lab: numbers, tables and financial documents
  6. Frameworks: LangChain, LangGraph and LangSmith

    1. 24. The RAG toolbox: what each library does and why you import it
    2. 25. The framework landscape: LangChain, LangGraph, LlamaIndex, Haystack
    3. 26. Lab: Ask Brightline in LangChain
    4. 27. Lab: agentic RAG as a LangGraph graph
    5. 28. Lab: tracing, evaluating and monitoring with LangSmith
    6. 29. Wrap-up: your production agentic RAG checklist
See the leaderboard

20 min

Lab: build the smallest RAG pipeline end to end

Build a working question-answering pipeline over six policy documents in about 40 lines of Python.

By the end of this lesson you can

  • Build an embed-retrieve-generate pipeline in Python
  • Print and read the retrieved passages before reading the answer
  • Spot a miss caused by top-k and fix it

From earlier lessons · 1 of 2

What are the three stages of every RAG request?

Pick one answer.

From earlier lessons · 2 of 2

Ask Brightline gives a wrong answer. Where do you look first?

Pick one answer.

Forty lines of Python will answer "How much can I spend on a hotel in Chicago?" correctly, with a citation. Then a one-word change to the question will make the same code miss half the answer.

Lab: build Ask Brightline v1

You'll build the smallest complete RAG pipeline: six policy snippets, an embedding model, a similarity search and one model call. No vector database and no framework.

First, the course helper. Save this file as rag.py. Every later lab imports it unchanged.

python
import os
import anthropic
from sentence_transformers import SentenceTransformer
MODEL = os.environ.get("RAG_MODEL", "claude-sonnet-5-5")
_client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from the environment
_embedder = SentenceTransformer("all-MiniLM-L6-v2") # small, runs on a laptop
def llm(prompt, system="You are a helpful assistant."):
msg = _client.messages.create(
model=MODEL, max_tokens=800, system=system,
messages=[{"role": "user", "content": prompt}],
)
return "".join(b.text for b in msg.content if b.type == "text") # skip any thinking blocks
def embed(texts):
return _embedder.encode(texts, normalize_embeddings=True)
rag.py
bash
python3 --version # 3.12 or newer: current numpy requires it
pip install anthropic sentence-transformers numpy # pulls in PyTorch, see lesson 24
export ANTHROPIC_API_KEY=... # your own key, from your account settings
Terminal

Step 1: the documents and the index

The corpus is six Brightline policy snippets. Each has an id you'll cite later. Embedding turns each snippet into 384 numbers that place it by meaning.

python
import numpy as np
from rag import llm, embed
DOCS = [
{"id": "travel-hotel", "text": "Hotels: up to $250 per night in Tier 1 cities (New York, San Francisco, Chicago, Boston) and $175 elsewhere. Stays above the limit need a director's written approval before booking."},
{"id": "travel-meals", "text": "Meals on business travel are reimbursed up to $75 per day. Alcohol is not reimbursable. Submit receipts within 30 days."},
{"id": "it-vpn", "text": "To reset your VPN token, open the Brightline Access app, choose Reset token and confirm with your badge PIN. Error VPN-0x80 means the token expired."},
{"id": "hr-pto", "text": "Full-time employees accrue 1.5 days of paid time off per month. Up to 10 unused days carry over to the next year."},
{"id": "hr-remote", "text": "Employees may work remotely up to 3 days a week with manager approval. Contractors follow the terms of their agency contract."},
{"id": "sec-device", "text": "Report a lost or stolen laptop to the security desk within 1 hour. Do not try to recover data yourself."},
]
vectors = embed([d["text"] for d in DOCS])
print(vectors.shape)
Expected output: (6, 384). Six snippets, 384 numbers each.

Step 2: retrieve the closest snippets

Because the vectors are normalised, a dot product gives cosine similarity: 1.0 means the same direction, near 0 means unrelated. We keep the top k.

python
def retrieve(question, k=2):
q = embed([question])[0]
scores = vectors @ q # cosine similarity, one score per snippet
top = np.argsort(-scores)[:k] # indexes of the k highest scores
return [(DOCS[i], float(scores[i])) for i in top]
for doc, score in retrieve("How much can I spend on a hotel in Chicago?"):
print(round(score, 2), doc["id"])
Expected output: 0.66 travel-hotel, then 0.41 travel-meals (your numbers will differ slightly).

Read this before you read any answer. The hotel snippet wins by a wide margin, so the model will see the right facts. This print is your first debugging tool.

Step 3: build a grounded prompt

A grounded prompt numbers each passage and tells the model to answer only from them. The numbers make citations possible, and the refusal rule gives the model a way out.

python
def build_prompt(question, hits):
context = "\n".join(
f"[{n}] ({doc['id']}) {doc['text']}" for n, (doc, _) in enumerate(hits, 1)
)
return (
f"Context:\n{context}\n\nQuestion: {question}\n"
"Answer using only the context. Cite passages like [1]. "
"If the context does not contain the answer, say you don't know."
)

Step 4: generate the answer

python
SYSTEM = "You answer Brightline employees' policy questions."
def answer(question, k=2):
hits = retrieve(question, k)
print("retrieved:", [doc["id"] for doc, _ in hits])
return llm(build_prompt(question, hits), system=SYSTEM)
print(answer("How much can I spend on a hotel in Chicago?"))
Expected output (your wording will differ): retrieved: ['travel-hotel', 'travel-meals'], then "Chicago is a Tier 1 city, so you can spend up to $250 per night [1]. Anything above needs a director's written approval [1]."

That's a complete RAG pipeline. Each answer() call makes one model call; this lab makes about four in total.

Issue: the answer needs two snippets

python
print(answer("What's the hotel limit in Denver, and can I expense wine with dinner?", k=1))
Expected output (your wording will differ): retrieved: ['travel-hotel'], then "The hotel limit in Denver is $175 per night [1]. The context doesn't say whether wine with dinner can be expensed."

The model behaved well: it refused the part it couldn't see. The miss is retrieval. With k=1 only one snippet reached the prompt, and the alcohol rule lives in another. Rerun with k=3 and travel-meals joins the context. Bigger k isn't free, though. Lesson 10 shows how to retrieve wide and keep only the best.

Put in order

Put one request through Ask Brightline in order

Drag, or focus an item and press Space, move with the arrow keys, then Space to drop.

  1. Score it against every snippet vector
  2. Keep the top k snippets
  3. Embed the question
  4. Call the model
  5. Number them in a grounded prompt
  6. Return the answer with citations

Worth keeping

  1. A working RAG pipeline is embed, score, keep top k, prompt, generate.
  2. Print the retrieved ids before reading any answer.
  3. Number the passages so the model can cite them and you can check it.
  4. A partial answer often means the needed passage never reached the prompt.

retrieve() matched "hotel in Chicago" to the right snippet without sharing most of its words. How did 384 numbers know that? Next lesson opens up embeddings and vector search.

Leaderboard

Loading…

50 XP for each lesson you finish, 10 for each right answer. Names show only for learners who turn on Show me on leaderboards.