What Are Embeddings

Updated · 5 min read
mediumPython 3.13aiembeddingsvectorssemantic-searchpython

An embedding is a list of numbers that represents the meaning of a piece of information, such as a word, a sentence, an image or a document. An embedding model produces it, and it is designed so that similar meanings get similar numbers. "Car" and "automobile" share no letters, yet their embeddings sit close together, while "banana" lands far away.

Embeddings let computers compare meaning, not just spelling, and they power semantic search, recommendations and retrieval for AI assistants.

Meaning as a Position

Think of a large library. A good librarian shelves books by topic, so a book about cars sits next to one about automobiles even if their titles share no words. An embedding gives every text a position like that, but in hundreds of directions instead of along one shelf.

Similar meanings land close together

Uses of Embeddings

UseDescription
Semantic searchIt is used to find documents by meaning, even without the same words
Retrieval for AI assistantsIt is used to find the passages a model needs to answer a question (RAG)
RecommendationsIt is used to suggest items similar to ones a person liked
Similarity matchingIt is used to find duplicate questions, related tickets or similar products
GroupingIt is used to cluster many texts into topics without labels

Example: Embedding Four Words

This lesson uses all-MiniLM-L6-v2, a small, free embedding model that runs on your own computer, through the sentence-transformers library (pip install sentence-transformers). The first run downloads the model, about 90 MB, and prints some download messages.

Python
from sentence_transformers import SentenceTransformer
 
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")  # downloads once
words = ["car", "automobile", "vehicle", "banana"]
vectors = model.encode(words, normalize_embeddings=True)
 
print("Each word becomes", vectors.shape[1], "numbers")
print("car starts with:", vectors[0][:5].round(3))
similarity = vectors @ vectors.T
print(" " * 11, " ".join(f"{w[:5]:>5}" for w in words))
for word, row in zip(words, similarity):
    print(f"{word:11}", " ".join(f"{v:5.2f}" for v in row))
Output
Each word becomes 384 numbers
car starts with: [-0.033  0.106  0.019  0.052 -0.037]
              car autom vehic banan
car          1.00  0.86  0.87  0.39
automobile   0.86  1.00  0.85  0.27
vehicle      0.87  0.85  1.00  0.31
banana       0.39  0.27  0.31  1.00

Explanation: model.encode() turns each word into 384 numbers. The individual numbers mean nothing on their own; only their pattern matters. normalize_embeddings=True scales each vector to length 1, so multiplying the vectors (@) gives their similarity, from about -1 to 1. Car, automobile and vehicle score 0.85 to 0.87 with each other, and banana scores only 0.27 to 0.39 with all three. Your numbers may differ slightly in the last digit on a different computer.

Choosing an Embedding Model

OptionDescription
Small local modelIt is used for learning and private data; free, runs on your computer, such as all-MiniLM-L6-v2
Hosted embedding APIIt is used for higher quality and many languages; you pay per token, like other model APIs
The same model everywhereEmbeddings from different models can't be compared; embed documents and questions with one model

Common Mistakes

Mixing vectors from different models. Compare only vectors made by the same model.

Reading meaning into single numbers. Only the whole vector, compared with others, carries meaning.

Treating high similarity as correctness. Similar text can still contain a wrong date or amount.

Interview Corner

What is an embedding?

A list of numbers that represents meaning, produced by an embedding model, so that similar meanings get nearby vectors. It is used for semantic search, retrieval, recommendations and clustering.

Why can't you compare embeddings from two different models?

Each model places meanings in its own space, so the same sentence gets unrelated numbers in each. Similarity is only meaningful within one model's space.

Practice

Exercise: your own word groups. Embed "teacher", "instructor", "professor", "pizza" and "sandwich". Which pairs score highest? Does "pizza" sit closer to "sandwich" or to "teacher"?

Check your understanding:

  1. What does an embedding model produce?
  2. How many numbers did all-MiniLM-L6-v2 produce for each word?
  3. Why are car and automobile close although they share no letters?

Quick check: embeddings

4 questions
1What is an embedding?
2Which pair had the lowest similarity in the example?
3Why can't you compare vectors from two embedding models?
4What does high similarity between two sentences tell you?

Key Takeaways

Quick reference

NeedCode
Load a modelSentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
Embed textsmodel.encode(texts, normalize_embeddings=True)
Sizevectors.shape[1]
Similarity of normalized vectorsa @ b

Was this tutorial helpful?

Lesson 90 of 186