Lecture 12: RAG, Semantic Search, and Grounding AI
Source: Google Research
Part 1: The problem
Part 2: Semantic search
Part 3: The RAG pipeline
Part 4: No-code tools
Source: SemiAnalysis
Source: Bhavishya Pandit
Today’s topic:
What if we could give the LLM access to real, verified information when answering?
RAG = Retrieval-Augmented Generation
Why this works:
It’s like giving the AI an open-book exam! 📖
Source: AWS
| Feature | Keyword Search | Semantic Search |
|---|---|---|
| Matching | Exact words only | Meaning-based |
| “cheap flights” | ✅ “cheap flights” | ✅ “budget airfare” too! |
| Synonyms | ❌ Misses them | ✅ Understands them |
| Typos | ❌ Breaks results | ✅ Often still works |
| Context | ❌ Ignores it | ✅ Considers it |
| Technology | String matching | Embeddings |
Example query: “How do I fix a broken pipe?”
From Lecture 06:
Text becomes vectors (lists of numbers):
| Text | Vector (simplified) |
|---|---|
| “king” | [0.82, 0.15, -0.43, …] |
| “queen” | [0.79, 0.18, -0.41, …] |
| “apple” | [-0.12, 0.67, 0.23, …] |
Key properties:
| Model | Dimensions | Use Case |
|---|---|---|
| OpenAI text-embedding-3-small | 1,536 | General purpose |
| Google Gemini Embedding | 768–3,072 | Adjustable size |
| Cohere Embed v4 | 1,536 | Multilingual, text + images |
How do we measure if two vectors are similar?
\[\text{similarity}(A, B) = \frac{A \cdot B}{\|A\| \times \|B\|}\]
Interpretation (illustrative; varies by model):
| Score | Meaning | Example |
|---|---|---|
| 0.9–1.0 | Very similar | “car” vs “automobile” |
| 0.7–0.9 | Related | “car” vs “truck” |
| 0.4–0.7 | Loosely related | “car” vs “road” |
| 0.0–0.4 | Unrelated | “car” vs “banana” |
In RAG systems:
What can you ingest?
| Format | Examples | Challenges |
|---|---|---|
| Papers, reports | Tables, columns, headers | |
| Word/Docs | Reports, notes | Formatting, styles |
| Web pages | Articles, docs | Navigation, ads |
| Code | .py, .js files | Comments vs. code |
| Transcripts | Meeting notes | Speaker identification |
Why parsing is hard:
Good news: Gemini Notebook and ChatGPT handle extraction automatically
An ingestion pipeline: collect, parse, chunk, embed, store. Source: Microsoft (2024)
Why chunk?
Chunking parameters:
| Parameter | Typical Values | Trade-off |
|---|---|---|
| Chunk size | 256–1,024 tokens | Small = precise, Large = more context |
| Overlap | 10–20% | Prevents losing info at boundaries |
| Strategy | Sentence, paragraph, semantic | Depends on document structure |
Rule of thumb: start with 512 tokens, 20% overlap, then adjust to your documents and retrieval quality
Chunk size trade-offs:
| Size | Pros | Cons |
|---|---|---|
| Small (256) | Precise retrieval | Loses context |
| Medium (512) | Balanced | Good default |
| Large (1024) | Rich context | May dilute relevance |
Source: Mastering LLM
Convert chunks to vectors & store:
| Database | Type | Speed (1M vectors) |
|---|---|---|
| Pinecone | Cloud | ~50ms queries |
| Chroma | Local/Cloud | ~100ms queries |
| FAISS | Local | ~10ms queries |
Why vector databases?
No-code tools handle this for you
Retrieval (when you ask a question):
Example: “What is our refund policy?”
| Rank | Chunk | Score |
|---|---|---|
| 1 | “Returns and refunds: Customers may return…” | 0.92 |
| 2 | “Our guarantee covers full refunds…” | 0.87 |
| 3 | “Payment methods accepted…” | 0.54 ❌ |
Parameters: top-k (3–10), threshold (set per model)
The LLM receives a prompt like this:
System: Answer the user’s question using ONLY the context provided. If the answer isn’t in the context, say “I don’t have that information.”
Context: [Chunk 1]: “Returns and refunds: Customers may return items within 30 days…”. [Chunk 2]: “Our guarantee covers full refunds for defective products…”
User question: “What is your refund policy?”
The LLM now:
Three ways to customise LLM behaviour:
| Aspect | Prompt Engineering | RAG | Fine-tuning |
|---|---|---|---|
| What it does | Careful instructions | Add external knowledge | Retrain model weights |
| Cost | Free or $ | $ | $$$ |
| Setup time | Minutes | Hours | Days–Weeks |
| Data freshness | Training cutoff | Real-time | Training cutoff |
| Accuracy (domain) | Low–Medium | High | High |
| Hallucination risk | High | Low | Medium |
| Cites sources | ❌ | ✅ | ❌ |
| Best for | Simple tasks | Knowledge-intensive QA | Style/behaviour change |
When to use each:
Yes, the evidence is strong:
| Study | Finding |
|---|---|
| Lewis et al. (2020) | Original RAG paper: beat models without retrieval on knowledge-intensive tasks |
| Shuster et al. (2021) | Retrieval cut hallucinated replies by over 60% in dialogue systems |
| Gao et al. (2024) | Survey: RAG is a promising fix for hallucination and outdated knowledge |
| Liu et al. (2023) | “Lost in the middle”: LLMs use beginning and end of context better than middle |
Hallucination rates comparison:
| Setting | Hallucination Rate |
|---|---|
| Base LLM (no RAG) | 15–40% |
| LLM + RAG | 5–15% |
| LLM + RAG + verification | 2–8% |
Rates vary by domain and implementation
The “Lost in the Middle” problem:
Liu et al. (2023): LLMs pay most attention to the
Implication for RAG: put the most relevant chunks first or last, not in the middle
Source: Liu et al. (2023)
Common failure modes:
| Failure Type | What Happens | Rough frequency (illustrative) |
|---|---|---|
| Retrieval failure | Wrong chunks retrieved | 15–25% of queries |
| Lost in the middle | Relevant info in middle ignored | Common with many chunks |
| Context overflow | Too much text, truncated | Depends on doc size |
| Outdated docs | Stale information retrieved | Depends on maintenance |
| Extraction errors | PDF tables/images parsed incorrectly | 10–30% of complex docs |
Retrieval failures happen when:
Example: Retrieval failure
Your document says: “Project Falcon launches on 15 March”
You ask: “When does the new mobile app launch?”
Problem: nothing in the text tells the model that “Falcon” is the mobile app
Result: wrong chunks retrieved, wrong answer
Mitigation strategies:
For document preparation:
For querying:
Golden rule: Trust, but verify 🔍
| Tool | Free? | Best For | Key Feature |
|---|---|---|---|
| Gemini Notebook (ex-NotebookLM) | ✅ Yes | Research, study notes | Multi-source synthesis |
| ChatGPT + Files | ✅ Free tier | General documents | Easy upload & chat |
| Claude + Files | ✅ Free tier | Long documents | 200K token context |
| Google AI Studio | ✅ Free tier | Experimentation | Gemini models |
All of these ground answers in your files:
No coding required, just upload and ask
What is Gemini Notebook?
Features:
| Feature | What It Does |
|---|---|
| Source grounding | Only answers from your docs |
| Citations | Points to exact source passages |
| Audio Overview | Generates podcast-style summary |
| Study guides | Creates questions & summaries |
| Cross-referencing | Finds connections between sources |
Best for: Research projects, exam prep, literature reviews, understanding complex reports
Open the playground and work through three tabs:
1. Text splitting
2. Semantic search
3. Context generation
| Industry | Application | How RAG Helps | Example Company |
|---|---|---|---|
| Customer Support | AI chatbots | Answer questions from product docs | Intercom, Zendesk |
| Legal | Research assistants | Search case law by meaning | Harvey AI, Thomson Reuters |
| Healthcare | Clinical support | Find relevant patient records, guidelines | Epic, Microsoft |
| Education | Personal tutors | Answer questions from course materials | Khan Academy, Duolingo |
| Finance | Analyst tools | Search earnings reports, SEC filings | Bloomberg, Kensho |
| Research | Literature review | Find related papers, summarise findings | Elicit, Semantic Scholar |
| Developer Tools | Documentation QA | Answer questions from codebases | GitHub Copilot, Cursor |
In each case, users need answers grounded in specific documents, with a citation for where the answer came from
Market size: enterprise RAG solutions expected to reach $40B+ by 2035 (estimates vary)
Advanced RAG: LangChain is a popular framework for building RAG applications. Explore it if you know Python or JavaScript and want to build your own
LLMs hallucinate and cannot see your private documents
Semantic search finds text by meaning, using embeddings
RAG chunks, embeds, retrieves and generates
One study: retrieval cut hallucinated replies by over 60%
Retrieval can fail, and old documents give old answers
Gemini Notebook and ChatGPT with files need no code
RAG reduces errors but does not remove them, so check the citations
Quick reference:
| Concept | Key Numbers |
|---|---|
| Cosine similarity | 0.9+ = very similar (model-dependent) |
| Chunk size | 256–1024 tokens |
| Overlap | 10–20% |
| Top-k retrieval | 3–10 chunks |
| Hallucination reduction | 60%+ (one study) |
Upload your docs, ask specific questions, and always check citations