Module 2 cheat sheet: RAG: Chat with Your Documents
Load → split → embed → store once; retrieve → generate for every question, with sources and an “I don’t know” rule.
Key ideas
Document=page_content+metadata. Lab 06 →RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=100). Lab 07 →- Same embedding model for docs and questions. Lab 08 →
- FAISS score = distance (lower better); cosine = similarity (higher better). Lab 09 →
- Hybrid (BM25 + vector) for production. Lab 10 →
- Answer only from context, return pages. Lab 11 →
The RAG chain
rag_chain = (
{"context": retriever | format_docs,
"question": RunnablePassthrough()}
| prompt | llm | StrOutputParser()
)Loaders
| Source | Loader |
|---|---|
| PyPDFLoader (1 doc / page) | |
| .csv | CSVLoader (1 doc / row) |
| .txt | TextLoader |
| web | WebBaseLoader |
| folder | DirectoryLoader |
Retrievers
| Retriever | Use when |
|---|---|
| similarity | default |
| MMR | repetitive results |
| score threshold | drop unrelated |
| BM25 | codes, IDs, names |
| Ensemble | production hybrid |
| MultiQuery | vague questions |
| ParentDocument | small search, big context |
Common mistakes
- Chunks too small (no meaning) or too big (noisy).
- Different embedding models for docs and questions.
- Reading FAISS distance as similarity.
- Default BM25 tokenizer missing codes like UPI-U30.
- No “I don’t know” rule, no sources.
Created by Ajeetkumar · EasyLearnSpace