Zero to AI Engineer

Module 20 of 54

Module 20: Documents, Embeddings & Semantic Search

4 min read734 words
What you'll learn
Explain embeddings in plain termsUnderstand semantic vs. keyword searchDescribe how documents are chunked and indexedSee why this powers RAG

"Keyword search finds the exact word. Semantic search finds the meaning — even when the words are completely different."

Level: Intermediate · Time: ~13 min · Prerequisites: Module 7

Learning Objectives

By the end of this module, you will be able to:

  • Explain embeddings in plain terms
  • Understand semantic vs. keyword search
  • Describe how documents are chunked and indexed
  • See why this powers RAG

1. Embeddings: Meaning as Numbers

An embedding turns a piece of text into a list of numbers (a "vector") that captures its meaning. Texts with similar meaning get similar vectors — they sit close together in "meaning space." A typical embedding isn't two or three numbers but hundreds or thousands (say, 768 or 1,536 dimensions), because meaning is rich and needs many "axes" to represent — one loosely capturing formality, another topic, another sentiment, and so on. You never read these numbers by hand; you just compare them.

Explain like I'm new: Imagine a huge map where every sentence is a pin. Sentences about dogs cluster in one region, sentences about taxes in another. An embedding is just the coordinates of a pin. Nearby pins mean similar things — even if they share no words.

Concept: A model learns these vectors from massive amounts of text, so words used in similar contexts land near each other. That's why "king − man + woman" famously lands near "queen": the relationships between meanings are baked into the geometry of the space.

2. Semantic Search

Because similar meanings are nearby, you can search by meaning, not keywords:

  • Keyword search: "car" won't match a document that says "automobile."
  • Semantic search: "car" and "automobile" have close embeddings, so it matches.

To search, you embed the query, then find the document chunks whose embeddings are closest.

Key idea: Semantic search = "find text that means the same thing." It works by comparing embedding vectors for closeness (distance in meaning space). This is what lets AI apps find the relevant passage even when phrased differently.

3. Preparing Documents: Chunking

You can't embed a whole 300-page manual as one vector — it'd be too coarse. Squeezing an entire book into a single point in meaning space blurs everything together, so a query about one specific topic can't find the exact spot that answers it. So documents are split into chunks (paragraphs or sections), each embedded separately. Good chunking (right size, sensible boundaries) is quietly one of the biggest factors in retrieval quality — many teams add a little overlap between neighboring chunks so a sentence that spans a boundary isn't lost.

python
[object Object],
chunks = text_splitter.split_documents(docs)
vectors = embeddings.embed_documents(chunks)   ,[object Object],

Real-world use case: A company chatbot that answers from the employee handbook: the handbook is chunked and embedded once. When someone asks "how many vacation days do I get?", the question is embedded, the closest chunks are retrieved, and the answer comes from those — accurate and grounded.

4. Why This Powers RAG

Semantic search over embedded documents is the retrieval engine behind Retrieval-Augmented Generation (next module). It's how an LLM gets the right facts to answer from — instead of guessing from memory.

Common mistake: Chunking too big or too small. Huge chunks bury the answer in noise; tiny chunks lose context. Start around a paragraph, and adjust based on how good your retrieved results are.

Hands-On: Try This

Try this: Think of three ways to ask "how do I reset my password?" (e.g., "forgot my login," "can't sign in," "change credentials"). Keyword search treats these as unrelated; semantic search sees they mean the same thing. That's the power you're unlocking.

✅ Checkpoint

  1. What does an embedding capture?
  2. How does semantic search differ from keyword search?
  3. Why are documents split into chunks?

Answers: 1) The meaning of text, as a vector of numbers. 2) It matches by meaning, not exact words. 3) So retrieval can find the specific relevant passage, not a whole document.

Key Takeaway: Embeddings turn text into vectors that capture meaning, so similar texts sit close together. Semantic search finds relevant text by meaning (not keywords) by comparing embeddings. Documents are chunked and embedded so retrieval returns the right passage — the engine that powers RAG.

Further Learning

Adapted from the LangChain for Beginners curriculum (MIT License).