Menu

LLMs course · Lesson 3 of 12

What Are Embeddings? Vectors, Similarity and Semantic Search Explained

What embeddings are in AI, how cosine similarity finds text with similar meaning, and how embeddings power semantic search and RAG, with runnable NumPy code.

  • Beginner
  • 5 min read
  • Updated Oct 2026
On this page
  1. Meaning as a position in space
  2. Measuring similarity: cosine similarity
  3. How semantic search works in an application
  4. Practical choices
  5. Common mistakes
  6. Key takeaway

An embedding is a list of numbers that represents the meaning of a piece of text (or an image, or a product). Texts with similar meaning get similar lists of numbers. That turns a fuzzy question, “are these two sentences about the same thing?”, into simple maths: measure how close their numbers are.

Embeddings are behind semantic search, recommendations, duplicate detection, clustering of support tickets and, most importantly for AI engineers, retrieval-augmented generation (RAG), where an application finds the right documents to give a language model.

Meaning as a position in space

Think of each embedding as a point in space. A real embedding model produces hundreds or a few thousand numbers per text, so the space has hundreds of dimensions, but the idea is the same as on a map: things that are close together are similar.

  • “How do I get a refund?” and “I want my money back” share almost no words, but they mean nearly the same thing, so their embeddings are close.
  • “I forgot my password” is about something else, so its embedding is far away from both.

Embedding models learn this from huge amounts of text: words and sentences used in similar contexts end up with similar vectors.

Measuring similarity: cosine similarity

The most common measure is cosine similarity. It compares the direction of two vectors: 1 means they point the same way (very similar), 0 means unrelated, and negative values mean opposite. It ignores the length of the vectors, which is usually what you want for meaning.

In this example, each text has a hand-made embedding with just four numbers, roughly standing for money, account access, delivery and feelings. Real models learn their dimensions automatically, and they do not have clear names like these, but the search works the same way:

import numpy as np

# Toy 4-number "embeddings". Real models learn hundreds of dimensions; these are hand-made to show the idea.
# Dimensions (roughly): money, account access, delivery, feelings
texts = {
    "How do I get a refund?":            [0.9, 0.1, 0.2, 0.1],
    "I want my money back":              [0.8, 0.0, 0.1, 0.4],
    "I forgot my password":              [0.0, 0.9, 0.0, 0.1],
    "Can't log in to my account":        [0.1, 0.8, 0.0, 0.2],
    "Where is my parcel?":               [0.1, 0.0, 0.9, 0.2],
    "My order hasn't arrived yet":       [0.2, 0.0, 0.8, 0.3],
}
names = list(texts)
vectors = np.array(list(texts.values()))

def cosine(a, b):
    return float(a @ b / (np.linalg.norm(a) * np.linalg.norm(b)))

query_text, query = "refund please, I was charged twice", np.array([0.9, 0.1, 0.1, 0.3])
scores = sorted(((cosine(query, v), n) for n, v in zip(names, vectors)), reverse=True)
print(f"query: {query_text}")
for score, name in scores[:3]:
    print(f"  {score:.3f}  {name}")
query: refund please, I was charged twice
  0.985  I want my money back
  0.972  How do I get a refund?
  0.416  My order hasn't arrived yet

The two refund texts score close to 1, even though “I want my money back” shares no words with the query. That is semantic search: finding by meaning, not by matching words. In a real application, an embedding model creates the vectors for you; you never set the numbers by hand.

How semantic search works in an application

  1. Index once. Split your documents into chunks, create an embedding for each chunk, and store the vectors with the original text, usually in a vector database or a vector index.
  2. Search each time. Create an embedding for the user’s question with the same model.
  3. Find the nearest chunks by cosine similarity (or a related distance), using an index that makes this fast even for millions of vectors.
  4. Use the results: show them as search results, or pass them to an LLM to write an answer, which is RAG. See RAG vs fine-tuning for when to use retrieval.

Practical choices

  • Use the same model for documents and questions. Vectors from different models cannot be compared. If you change models, re-embed everything.
  • Choose a model for your language and domain. Test a few on your own examples instead of trusting general leaderboards.
  • Dimensions and cost. Bigger vectors can capture more detail, but use more storage and make search slower.
  • Combine with keyword search. Embeddings are weaker at exact matches such as product codes, error numbers and names. Many systems use hybrid search: keyword search plus vector search, then a reranker.
  • Keep metadata. Store the source, date and access permissions with each vector, so you can filter results and cite them.

Common mistakes

  1. Embedding whole long documents as one vector. The meaning gets blurred; split documents into chunks first.
  2. Mixing embedding models in one index.
  3. Not testing retrieval. Build a small set of questions with known correct documents and measure how often they are found.
  4. Forgetting updates. When documents change or are deleted, their vectors must be updated too.

Key takeaway

Embeddings turn meaning into numbers, so similar texts get vectors that point in similar directions. Cosine similarity measures that closeness, which makes semantic search possible and is the foundation of RAG. In interviews, explain embeddings with an example like the refund query above, and mention hybrid search for exact terms. Test yourself with the LLM interview questions.

By , Data Engineer and Founder of DataDank · Last reviewed Oct 2026 · Concepts apply to all embedding models. The example uses hand-made 4-number vectors to show the idea clearly; it runs on Python 3 with NumPy, and its output is real.

Progress is saved in this browser only. No account needed.

Search
Filter by type