# Chapter 11: RAG, embeddings and grounding

Part of the course “AI from Zero to Autonomous Agents” (https://llm.yoga/en). Page for humans: https://llm.yoga/en/11-rag

*The open-book exam for language models: how to turn knowledge into numbers, find what fits and tie the answer to sources.*

About 40 min to read · about 30 min hands-on exercise

**Start with the missions**

This chapter also comes as 4 short missions with a case, a decision and feedback, about 10 minutes each:

- [11.1 The whole manual in the prompt, or RAG?](https://llm.yoga/en/einsatz-11-1.md)
- [11.2 How does a search for „Urlaub“ (vacation) find „Erholungsurlaub“ (paid vacation)?](https://llm.yoga/en/einsatz-11-2.md)
- [11.3 The bot can’t find error E-4711](https://llm.yoga/en/einsatz-11-3.md)
- [11.4 The hits have been useless since the switch to a local embedding model](https://llm.yoga/en/einsatz-11-4.md)

For agents, this page is split into 5 parts so that each part can be fetched in full. Read the parts the question needs.

## Parts

### Part 1: https://llm.yoga/en/11-rag-1.md

- **Getting started**: Think of two exams. In the first, you can’t bring anything.
- **Why a model has to look things up**: A language model stores its knowledge in its weights.
- **Embeddings: meaning as coordinates**: A classic search engine compares words.

### Part 2: https://llm.yoga/en/11-rag-2.md

- **How to measure closeness: cosine similarity**: If texts are points in space, you need a measure for “close”.
- **The flow: indexing and querying**: Every RAG system has two phases, as video 3 also stresses (starting at 25:02, in German).
- **Chunk size and overlap**: How big a chunk should be is the first dial you turn.

### Part 3: https://llm.yoga/en/11-rag-3.md

- **When meaning isn’t enough: hybrid search**: Semantic search has a weakness that’s easy to overlook.
- **Reranking: the second opinion**: Even hybrid search only delivers a rough preselection, say 20 to 50 candidates.
- **Contextual retrieval: telling the chunk where it comes from**: A chunk has a problem you only notice when you read it on its own.
- **Vector databases and embedding models**: An ordinary database looks for exact matches: customer number equals 4711.

### Part 4: https://llm.yoga/en/11-rag-4.md

- **Grounding: Google’s term**: In Google’s materials, you’ll come across another word for a related idea: grounding.
- **The videos in context**: Video 1 (in German) builds a local RAG agent, starting at 1:04:08, with n8n’s official “Self-hosted AI Starter Kit”: n8n, Ollama, Qdrant and PostgreSQL in Docker.
- **Typical mistakes when building**: Most RAG systems don’t fail because of the language model but at inconspicuous points before it.

### Part 5: https://llm.yoga/en/11-rag-5.md

- **Exercise: a mini RAG on your own computer**: Goal: you calculate similarities first by hand and then with a real embedding model, and build a search with an answer out of them.
- **Common misconceptions**: With RAG, a model no longer hallucinates.
- **Review questions**: 1. A question vector is q = (3, 0, 4), the vector of the car chunk a = (0, 5, 1).
- **Go further**: Lewis et al.: Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks (2020), the paper that gave RAG its name.
