Embeddings and semantic similarity

AI & LLM fundamentals · Practical · 11 min

Embeddings power search by meaning, duplicate detection, recommendations and the retrieval step of RAG.

Ordinary search matches words. Search for "cancel my subscription" and a help article titled "How to end your plan" may never show up, because no words match. Embeddings let a computer compare meaning instead.

An embedding is a long list of numbers (often hundreds or a few thousand) that a special embedding model produces for a piece of text. The numbers themselves mean nothing to a human. What matters is that texts with similar meaning get similar lists, and unrelated texts get very different ones. You can picture each text as a point on a map where "cancel my subscription" and "end your plan" land close together, and "banana bread recipe" lands far away.

To compare two embeddings you compute a similarity score, most often cosine similarity: roughly 1 means very similar, around 0 means unrelated.

What people use them for:

  • Semantic search: embed every document once, embed the question, return the closest documents.
  • Grouping and de-duplication: find support tickets that describe the same problem in different words.
  • Recommendations: "articles similar to this one".
  • Classification: compare a text with example texts for each category.

Practical points:

  • Embedding models are separate, cheaper models (for example OpenAI's text-embedding models or Google's Gemini embedding models; open models exist too). They have their own token limits.
  • Only compare embeddings made by the same model; numbers from different models are not compatible. Changing the model means re-embedding everything.
  • Embeddings capture topic and meaning well, but can miss exact details like a specific product code; combining them with keyword search often works best.

Example

Text                              Embedding (shortened)
"cancel my subscription"     ->   [0.12, -0.48, 0.33, ...]
"how to end your plan"       ->   [0.10, -0.45, 0.30, ...]
"banana bread recipe"        ->   [-0.61, 0.07, -0.22, ...]

Similarity to "cancel my subscription":
  "how to end your plan"   0.91   <- close in meaning
  "banana bread recipe"    0.04   <- unrelated

Remember

  • An embedding is a list of numbers representing a text's meaning
  • Similar meaning gives high cosine similarity
  • Only compare embeddings from the same embedding model

Watch out for

  • Mixing embeddings from two different models in one search index; the scores become meaningless.

Check yourself

  1. Why can embedding search find 'How to end your plan' for the query 'cancel my subscription'?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: developers.openai.com/api/docs/guides/embeddings