Chapter 6 · Part 3

Finding things by meaning

We've built a space where meaning is a location and closeness is similarity. Now we cash it in for the thing embeddings are most used for: search that understands what you mean, not just the words you typed.

Old-school keyword search looks for matching words. Ask it "how do I reset my password?" and a help article titled "Recovering a locked account" might never show up — no shared words. Semantic search embeds your query and the documents into the same space and simply finds the nearest neighbors. Meaning matches, even when vocabulary doesn't.

Scroll to drop a query into a space full of help articles and watch the closest ones light up.

The query gets embedded into the same space as every document.

scroll

Search becomes nearest-neighbor lookup

The whole pipeline is short. Once everything is a vector, "find relevant documents" turns into "find the vectors closest to the query vector" — exactly the cosine similarity from Chapter 2, run against a whole collection.

search.py — semantic search over a document set
docs = [...]                              # your help articles, products, notes
doc_vecs = model.encode(docs)            # embed them once, store in an index

def search(query, k=3):
  q = model.encode(query)
  scores = [cosine(q, d) for d in doc_vecs]
  top = sorted(range(len(docs)), key=lambda i: scores[i], reverse=True)[:k]
  return [docs[i] for i in top]

search("how do I reset my password?")
# -> ["Recovering a locked account", "Change your login credentials", ...]

The same trick, everywhere

This one operation — embed, then find nearest — quietly powers a huge slice of modern software:

  • Semantic search over docs, code, and support tickets.
  • Recommendations: "more like this" is just nearest neighbors to what you liked.
  • RAG (retrieval-augmented generation): before an LLM answers, it embeds your question, retrieves the most similar documents, and pastes them into its context — so it can ground its answer in real, relevant text rather than guessing.

You now know how machines understand meaning

Step back and the whole course is one idea, built up:

  • A word becomes a vector — a place in space.
  • Closeness in that space means similar meaning (cosine similarity).
  • Those vectors are learned from context, not hand-built.
  • Meaning even supports arithmetic — king − man + woman ≈ queen.
  • Anything — sentences, images — can join the same space.
  • And finding the nearest vectors powers search, recommendations and RAG.

"Understanding meaning," for a machine, turns out to be geometry: put things in the right place, and similarity takes care of itself.

Thanks for reading. If you enjoyed this, the other courses cover how ChatGPT works and how AI generates images — same visual approach.