Have you ever asked an AI about something that happened this morning, or about a private document on your laptop, and it hallucinated an answer? That's because LLMs have a "cutoff date."
RAG fixes this. Instead of trying to memorize the whole world, the AI "looks up" the relevant information in your documents before it answers. Today, weβll build the foundation of a RAG pipeline.
ποΈ The 5 Steps of RAG
To give an AI a library, we follow a simple assembly line:
1. Load: Pulling data from a PDF, Website, or Text file.
2. Split: Breaking long documents into small, bite-sized "chunks."
3. Embed: Converting those text chunks into numbers (vectors) that represent their meaning.
4. Store: Saving those numbers in a "Vector Database."
5. Retrieve: Finding the right chunk when a user asks a question.
π οΈ Step 1: Loading & Splitting
AI can't read a 50-page PDF all at once. We have to "chunk" it so the AI only reads the relevant parts.
from langchain_community.document_loaders import WebBaseLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
# 1. Load a webpage
loader = WebBaseLoader("https://docs.smith.langchain.com/user_guide")
docs = loader.load()
# 2. Split it into 1000-character chunks
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
splits = text_splitter.split_documents(docs)
print(f"Created {len(splits)} chunks of data.")
π§ Step 2: The "Brainy" Storage (Vector DB)
We don't search for words; we search for meaning. Using Embeddings, the word "King" will be numerically close to the word "Queen," even if they are spelled differently.
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
# 3. Create the "Library" (Vector Store) using your chunks
vectorstore = Chroma.from_documents(
documents=splits,
embedding=OpenAIEmbeddings()
)
# 4. Turn the library into a "Retriever"
retriever = vectorstore.as_retriever()
π― Day 9 Summary
Today, you learned how to bridge the gap between static AI knowledge and your dynamic data. We covered:
- The RAG Concept: Why "Search + Generate" is better than just "Generate."
- Document Loaders: Bringing external data into Python.
- Text Splitters: Why chunking matters for accuracy.
- Vector Stores: Searching by meaning, not just keywords.
Your Homework: Find a long article online and try to load it using the WebBaseLoader. See how many "chunks" it creates when you set the chunk_size to 500!
See you tomorrow! β
Top comments (0)