Yesterday, we built the "Library" (Vector Store). Today, weβre going to build the "Librarian."
A Librarian doesn't just point you to a shelf; they go get the right book, read the relevant page, and explain it to you. In LangChain, we do this by connecting our Retriever to our LLM using a Retrieval Chain.
ποΈ The 2-Step Architecture
To make our AI answer questions based on our data, we need to link two distinct parts:
1. The Retrieval Step: Finding the most relevant chunks from our Vector Database.
2. The Generation Step: Feeding those chunks into the LLM as "Context" so it can craft an answer.
π οΈ Step 1: The "Stuffing" Chain
First, we need a way to tell the AI: "Here is a bunch of text (the context). Use it to answer this specific question." In LangChain, this is often called the create_stuff_documents_chain because it "stuffs" all retrieved documents into the prompt.
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
# Define the "Instructional" prompt
prompt = ChatPromptTemplate.from_template("""
Answer the user's question based ONLY on the provided context.
If you don't know the answer, say you don't know.
Context: {context}
Question: {input}
""")
llm = ChatOpenAI(model="gpt-4o")
# This chain knows HOW to format the documents into the prompt
document_chain = create_stuff_documents_chain(llm, prompt)
π οΈ Step 2: The Final Retrieval Chain
Now, we link the "Librarian" (the document chain) to the "Library" (the retriever we built yesterday).
from langchain.chains import create_retrieval_chain
# 'retriever' is the object we created in Day 9
retrieval_chain = create_retrieval_chain(retriever, document_chain)
# Let's ask it something!
response = retrieval_chain.invoke({"input": "What are the main features of LangSmith?"})
print(response["answer"])
π§ Why This is Better Than a Search Engine
- No Hallucinations: Because we told the AI "Answer ONLY based on context," it won't make things up.
- Privacy: The data never leaves your "Vector Store" to train the model; it's only sent as a temporary reference during the query.
- Source Transparency: The response object actually contains a context key that shows you exactly which snippets of text the AI used to find the answer.
π― Day 10 Summary
Today, you built a production-ready AI feature! You learned:
- Document Chains: How to format multiple text chunks for an LLM.
- Retrieval Chains: How to automate the flow from "Question" to "Answer."
- Groundedness: How to prevent AI hallucinations using context.
Your Homework: Look at the response["context"] metadata. Can you see which specific part of your document the AI liked the most?
See you tomorrow! β
Top comments (0)