Lessons and outlineLesson 5 of 11

AI Concepts in PicturesLesson 5

18 min read

Retrieval in Pictures: Embeddings, Vector Search and RAG

How AI looks things up in your documents before it answers.

In plain English

RAG looks things up first, then answers. Embeddings turn text into numbers so similar meanings sit close together. Vector search finds the nearest chunks. Reranking and hybrid search improve results. Always show sources.

Embedding
A list of numbers that stands for the meaning of a text.
Chunk
A small piece of a document, about one idea.
Reranking
A second pass that sorts results by how well they answer.
Picture it
How RAG answers
1
Question
Turned into an embedding
2
Find nearest chunks
Vector search, then rerank
3
Give them to the model
With the question
4
Answer with sources
Or say I do not know

RAG is short for retrieval-augmented generation. In plain words: look things up first, then answer. The AI searches your documents for useful pieces. Then it writes an answer using those pieces. This lesson shows how the search works, with the key ideas: embeddings, vector search, chunking and reranking.

The big picture

Picture a helpful librarian. You ask a question. The librarian finds the right pages, then explains them. RAG is the same. The search finds the pages. The model writes the explanation.

Embeddings: a map of meaning

A computer cannot compare meaning directly. So it turns text into a list of numbers called an embedding. Texts with similar meaning get similar numbers. Imagine a giant map. Each text is a dot. Dots about refunds sit near each other. Dots about shipping sit in another corner.

  • A question becomes a dot on the map too.
  • The nearest dots are the most related pages.
  • This works even when the words differ. A search for cancel a plan can find a page called ending your subscription.
Try it yourself
A map of meaning

Pick a question and see which texts sit nearest.

Each dot is a piece of text, placed by meaning. Pick a question and see which texts sit closest.

refund policymoney backcancel my plandelivery timetrack my ordershipping costreset passwordchange my emaillogin problem

Closest: money back, refund policy, cancel my plan. The words differ, but the meaning is close, so the search still finds them.

The stored dots live in a vector database. It is built to find the nearest dots fast, even among millions. The word vector just means a list of numbers.

Chunking

Long documents are cut into smaller pieces called chunks. Each chunk gets its own dot. If chunks are too big, they mix topics. If they are too small, they lose meaning. A good chunk is about one idea.

  • Reranking: the first search returns, say, twenty chunks. A second step sorts them by how well they answer the question. The best few go to the model.
  • Hybrid search: combine meaning search with plain keyword search. Keywords are good for names, codes and exact phrases.
Try it yourself
Walk through one RAG answer

Step through the five stages.

A person asks a question in plain words.

“How many leave days do I get?”

Where RAG goes wrong

  • The right page was never found, because of bad chunks or old documents.
  • The page was found but ignored, because too much text was added.
  • The question has no answer in the documents, and the model guesses.
  • The documents are stale. Keep them fresh.

A word on knowledge graphs

A knowledge graph stores facts as dots and links, such as person works at company. It helps with questions about relationships. Some systems combine it with RAG. Start with plain RAG first.

rag-in-steps.txt
1 Prepare   cut documents into chunks, turn each into an embedding, store them
2 Ask       turn the question into an embedding
3 Find      get the nearest chunks, rerank, keep the best few
4 Answer    give the model the question and those chunks
5 Check     show the sources, and say I do not know when nothing fits

A quick example

A staff handbook has two hundred pages. A new hire asks how many leave days she gets. The system turns the question into an embedding. It finds the three nearest chunks. One of them is the leave policy. The model reads that chunk and answers, with a link to the page.

→Show the source next to every answer. People trust an answer more when they can open the page behind it.
⚠RAG does not remove mistakes. A model can still misread a page. Test with real questions.
Key takeaways
  • ✓RAG means look things up first, then answer.
  • ✓Embeddings turn text into numbers so similar meanings sit close together.
  • ✓Vector search finds the nearest chunks fast. Chunking cuts documents into pieces.
  • ✓Reranking and hybrid search improve what is found.
  • ✓Always show sources and allow the answer I do not know.
Hands-on exercise

Plan a small RAG system

  1. Pick a set of documents, such as a staff handbook.
  2. Decide how you would chunk it, with one idea per chunk.
  3. Write five real questions, and one question that has no answer in the documents.
  4. Write what the system should say for that last question.
Check yourself
Q1What is an embedding?show answer

A list of numbers that stands for the meaning of a text. Similar meanings get similar numbers.

Q2Why use hybrid search?show answer

Meaning search is good at ideas. Keyword search is good at exact names and codes. Together they find more.

Q3What should RAG say when the documents do not contain the answer?show answer

That it does not know. Guessing is how errors enter.

0 / 11 finished0%