LangChain and LangGraph · Chapter 9 of 25

Retrievers and RAG chains

By the end of this chapter you can turn a vector store into a retriever, build a RAG chain with the | pipe (retrieve, format, prompt, model, parser), make the model answer only from what was retrieved and say "I don't know" otherwise, return the sources with the answer, and explain why the deposit question from chapter 1 is now answered right. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: RAG with LangChain
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of LangChain and LangGraph are free. This one comes with the all labs plan.

Why you need this

Chapter 1 asked the model "When will I get my deposit back after I move out?" and it said 7 days, in full. The policy says 15 working days, minus dues. Chapter 7 cut the policy into rules; chapter 8 put them in a vector store that finds the rules closest to a question. Two pieces are still missing: putting what the store finds into the model's request, and telling the model to use only that. Together, that is a RAG chain: the piece of the assistant Tanvi has been waiting for.

You already have the tools to build it: a prompt template (chapter 2), the | pipe that joins Runnables into a chain, and StrOutputParser (chapter 3). This chapter adds three small pieces: the retriever, RunnablePassthrough, and a dictionary inside a chain.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    retriever.py: a search you can pipe

    Build the store of chapter 8: one Document per rule of /workspace/data/policy.md (the whole line as page_content; rule and section in the metadata) in an InMemoryVectorStore that uses lab-embed with check_embedding_ctx_length=False.

    Turn it into a retriever that returns 2 Documents, invoke it with the tenant's question Can I smoke on the terrace?, and print the page_content of each Document it returns, one per line, in the order it returns them.

  2. Task 2

    rag_chain.py: the RAG chain

    Build the store of task 1 and a retriever with k=3. Make one chain with the pipe: a dictionary whose context is the retriever piped into a function that joins the Documents' page_content with newlines, and whose question is the question itself; then a ChatPromptTemplate; then ChatOpenAI with lab-chat; then StrOutputParser.

    The template has two messages: the system message, exactly You are RoomRaja's tenant assistant. Answer only from the context below. If the context does not say, say you don't know. then a blank line, a line Context:, and the context; and the human message, the question.

    Invoke the chain with Is breakfast included? and then with Is there a swimming pool?, and print each answer on its own line.

  3. Task 3

    with_sources.py: answers with sources

    Build the store and a retriever with k=3, and the prompt, model and parser of task 2 (the same system message). Make a chain that returns a dictionary with three keys: docs (the Documents the retriever found), question, and answer (the model's answer, made from those same Documents).

    Invoke it with How long does a plumbing repair take? and print two lines: the answer, then Sources: and the rule numbers of the retrieved Documents, in the order they were retrieved, joined with a comma and a space.

  4. Task 4 · interview question

    Interview: deposit_right.py, the deposit question, now right

    Chapter 1's question, exactly: When will I get my deposit back after I move out?

    Ask it twice: first to ChatOpenAI with lab-chat alone (the question only, no system message), then through the RAG chain of task 2 (k=3, the same system message).

    Print two lines: Without RAG: and the model's answer, then With RAG: and the chain's answer.

    The interviewer will ask what changed between the two answers, and what could still make the RAG answer wrong.

What you will learn

  • Retrievers: a vector store as a Runnable, with as_retriever and k.
  • The RAG chain: retrieve, format, prompt, model, parser.
  • Grounding: answer only from the context, and "I don't know".
  • Sources: the answer and the Documents it came from, together.

More in RAG with LangChain

Build "Retrievers and RAG chains" yourself

RoomRaja, a Pune co-living company, answers tenants on WhatsApp by hand. You build its assistant. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of LangChain and LangGraph are free. This one comes with the all labs plan.