AI Hints
Home›LangChain Tutorial›RAG Foundations
LangChain · Lesson 58 of 120

What Is RAG

Answers from your documents.

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

Ask a model about Riverside Books' return policy, and it will guess, because it has never seen the shop's documents. Retrieval augmented generation, or RAG, fixes this. Find the relevant documents first, then give them to the model with the question.

What Is RAG
From the video: What Is RAG
  • Models don't know your documents
  • They guess
  • Retrieve, then generate

Without RAG

Let's see the problem. We ask a plain model what express delivery costs at Riverside Books. Run it. The model has never seen the shop's price list, so it either invents a price, or says it doesn't know. Either way, it's not the real price, which is five pounds ninety nine.

no_rag.pypython
from langchain.chat_models import init_chat_model

model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)
q = "How much does express delivery cost at Riverside Books? One sentence."
print(model.invoke(q).text)
Output
Express delivery at Riverside Books costs $9.99 for orders under $50 and is free for
orders of $50 or more.

The RAG pipeline

RAG has two phases. First, indexing, done once. Load the documents, split them into chunks, turn each chunk into an embedding, and store them in a vector store. Then, at question time, find the chunks most similar to the question, and put them in the prompt. The model answers from real text.

  1. Load
  2. Split
  3. Embed
  4. Store
  5. Retrieve + answer
The RAG pipeline
From the video: The RAG pipeline

Summary

RAG retrieves relevant documents and gives them to the model, so answers come from your real data. Next, the document object that holds each piece of text.

Key takeaways
  • Models guess without data
  • Index once
  • Retrieve at question time
Next up: Documents — text plus metadata