What Are Embeddings?
Learn how AI converts text and other data into numerical vectors that capture meaning. Embeddings are the foundation for semantic search, recommendations, clustering, and many modern RAG systems.
By the end of this lesson, you will be able to:
- Explain what an embedding is in simple terms.
- Understand how text can be represented as a vector.
- Explain semantic similarity between embeddings.
- Understand dimensions and vector distance at a high level.
- Create embeddings with Python and use them for similarity search.
What Is an Embedding?
An embedding is a numerical representation of data. For text, an embedding model maps words, sentences, paragraphs, or documents into a vector of numbers.
The important idea is that the vector is designed to capture useful relationships in the original data. Texts with related meanings can have vectors that are close together in the embedding space.
Instead of asking whether two sentences contain exactly the same words, embeddings let applications compare their semantic representations.
From Text to a Vector
Suppose we have three pieces of text. An embedding model converts each one into a vector with many numerical values.
The actual vector values usually do not have human-readable meanings such as “password = 0.44”. The useful information comes from the position of the complete vector in the embedding space.
Embeddings Capture Semantic Similarity
Consider these sentences:
“How can I change my password?”
“I forgot my password and need a new one.”
“How can I change my password?”
“The weather is sunny today.”
What Does “Dimension” Mean?
A vector has dimensions. A tiny educational example might represent a sentence with only three numbers:
Real embedding models generally use far more dimensions. You do not need to interpret each dimension individually. Think of the complete vector as a point in a high-dimensional mathematical space.
Think of Embeddings as a Meaning Map
A useful mental model is a map. Similar concepts tend to occupy nearby regions, while unrelated concepts may be farther apart.
How Do We Compare Two Embeddings?
Once text has been converted into vectors, we need a mathematical way to compare them. A common choice is cosine similarity.
Similarity scores depend on the embedding model and the implementation. Do not assume one universal threshold works for every dataset.
Python Example: Create an Embedding
An embedding API can convert text into a vector that your application can store, compare, or send to a vector database.
from openai import OpenAI
client = OpenAI()
text = "How do I reset my password?"
response = client.embeddings.create(
model="YOUR_EMBEDDING_MODEL",
input=text,
)
vector = response.data[0].embedding
print("Dimensions:", len(vector))
print("First values:", vector[:5])
Python Example: Compare Two Embeddings
You can calculate cosine similarity with Python to estimate how closely two vectors are related.
import numpy as np
def cosine_similarity(a, b):
a = np.array(a)
b = np.array(b)
return np.dot(a, b) / (
np.linalg.norm(a) * np.linalg.norm(b)
)
similarity = cosine_similarity(
embedding_a,
embedding_b,
)
print(similarity)
In a real application, the two embeddings would normally come from the same embedding model so that they share the same vector space.
Embeddings Power Semantic Search
Traditional keyword search mainly looks for matching words. Semantic search can retrieve content based on meaning.
“Steps for recovering a forgotten password”
0.91“How to restore access after a failed login”
0.87“Update your account security settings”
0.74Embeddings in RAG
Embeddings are one of the key building blocks of retrieval- augmented generation. A typical RAG pipeline turns documents into embeddings, stores them, embeds a user's query, and retrieves the most relevant chunks.
Where Are Embeddings Used?
Find content by meaning rather than exact keywords.
Retrieve relevant document chunks before generation.
Find items with similar semantic representations.
Group similar documents, messages, or topics.
Use vector representations as features for downstream tasks.
Identify content that is semantically similar.
Important Embedding Decisions
Common Misconceptions
Usually, the useful information is distributed across the complete vector.
Similarity is a retrieval signal. Your application still needs evaluation and validation.
Embeddings are representations used for tasks such as retrieval and comparison.
Build a Tiny Semantic Search Experiment
Create embeddings for five short FAQ questions and compare them with a new user query.
- Create five FAQ texts about account, billing, shipping, and technical support.
- Generate an embedding for each FAQ.
- Generate an embedding for the user query.
- Calculate similarity scores.
- Sort the FAQs from most similar to least similar.
Test Your Understanding
A numerical vector representation of data that can capture useful relationships for comparison and retrieval.
They allow applications to compare representations by semantic similarity rather than relying only on exact word matches.
It is a common way to compare the orientation of two vectors and estimate their similarity.
Documents and queries are embedded into vectors so relevant document chunks can be retrieved for the generation step.
Embeddings turn meaning into a form your application can compare.
Key Takeaways
- Embeddings represent text or other data as numerical vectors.
- Semantic similarity can be estimated by comparing vectors.
- Cosine similarity is a common vector comparison technique.
- Embeddings are fundamental to semantic search and RAG.
- Real systems require evaluation of retrieval quality, not just similarity scores.
- Embedding model and version choices matter when storing and retrieving vectors.