AI Hints
Home›LangChain Tutorial›Your First RAG Chain
LangChain · Lesson 71 of 120

The RAG Prompt

Grounding the model.

⏱ 2 min read▶ Video at 1:20:22🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

The prompt decides how well the model uses the context. A good RAG prompt tells the model who it is, to stick to the context, and how to answer. Let's look at exactly what the model receives.

The RAG Prompt
From the video: The RAG Prompt
  • The prompt matters
  • Stick to the context
  • See what the model gets

What the model sees

The system message holds the rules, and the context. The human message holds the question. We retrieve two chunks, and label each with its source file. Then, instead of calling the model, we print the filled prompt. Run it. This is everything the model will see. The rules, the relevant policy text, and the question.

rag_prompt.pypython
from langchain_core.prompts import ChatPromptTemplate
from shop_kb import build_store

prompt = ChatPromptTemplate.from_messages([
    ("system", "You answer questions for Riverside Books.\n"
               "Use ONLY the context below. Be brief.\n\nContext:\n{context}"),
    ("human", "{question}"),
])
q = "Do gift cards expire?"
docs = build_store().similarity_search(q, k=2)
context = "\n\n".join(f"[{d.metadata['source']}] {d.page_content}" for d in docs)
for m in prompt.invoke({"context": context, "question": q}).messages:
    print(f"--- {m.type}\n{m.content}")
Output
--- system
You answer questions for Riverside Books.
Use ONLY the context below. Be brief.

Context:
[gift_cards.md] # Gift cards
Riverside Books gift cards come in values from £10 to £100.
Gift cards never expire and can be used online or in our shop.
A gift card can be used across several orders until the balance is spent.
Gift cards cannot be exchanged for cash.

[shipping.md] Every order gets a tracking link by email once it leaves our warehouse.
--- human
Do gift cards expire?
Also uses kb/shipping.md
# Shipping
Standard delivery in the UK takes 3 to 5 working days and costs £2.99.
Delivery is free on orders over £25.
Express delivery arrives the next working day if you order before 2pm, and costs £5.99.
We ship to Ireland and France in 5 to 8 working days for £7.50.
Every order gets a tracking link by email once it leaves our warehouse.
Also uses kb/returns.md
# Returns and refunds
You can return any book within 30 days of delivery for a full refund.
Books must be unread and in the condition you received them.
Damaged or faulty books can be returned at any time, and we pay the return postage.
Refunds go back to the original payment method within 5 working days of us receiving the book.
To start a return, reply to your order confirmation email.
Also uses kb/gift_cards.md
# Gift cards
Riverside Books gift cards come in values from £10 to £100.
Gift cards never expire and can be used online or in our shop.
A gift card can be used across several orders until the balance is spent.
Gift cards cannot be exchanged for cash.
Also uses kb/loyalty.md
# Riverside Readers loyalty club
Joining the Riverside Readers club is free.
Members earn one point for every pound they spend.
100 points can be swapped for a £5 voucher.
Members get early access to signed editions and a birthday discount of 10 percent.
Also uses kb/shop.md
# About the shop
Riverside Books has one physical shop, at 12 Mill Lane, Bath.
The shop is open from 9am to 6pm Monday to Saturday, and 11am to 4pm on Sunday.
We sell new and second-hand books, and run a children's story time every Saturday at 10am.
You can contact us at hello@riverside-books.example or on 01225 000 000.
Also uses shop_kb.py
from pathlib import Path
from langchain_core.documents import Document
from langchain_core.vectorstores import InMemoryVectorStore
from langchain_google_genai import GoogleGenerativeAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter

def load_docs(folder="kb"):
    return [Document(p.read_text(), metadata={"source": p.name})
            for p in sorted(Path(folder).glob("*.md"))]

def build_store(folder="kb"):
    splitter = RecursiveCharacterTextSplitter(chunk_size=300, chunk_overlap=40)
    chunks = splitter.split_documents(load_docs(folder))
    embeddings = GoogleGenerativeAIEmbeddings(model="gemini-embedding-001")
    return InMemoryVectorStore.from_documents(chunks, embeddings)

Rules for a RAG prompt

Give the model a role. Tell it to use only the context, so it doesn't mix in guesses. Tell it what to do when the answer is missing. And set the style, like short and friendly, so answers fit your app.

keyvalue
Rolewho it answers for
Groundinguse only the context
Missing answerwhat to say
Styleshort, friendly

Summary

A RAG prompt sets the role, grounds the model in the context, and sets the style. Printing the filled prompt shows exactly what the model sees. Next, handling questions the documents can't answer.

Key takeaways
  • Role and rules
  • Context in the system message
  • Print the prompt to check
Next up: Saying I Don't Know — when the answer is missing