LESSON 26 · EMBEDDINGS

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.

28 min read • Beginner • Generative AI

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.
1

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.

✦
CORE IDEA Turn meaning into numbers that machines can compare.

Instead of asking whether two sentences contain exactly the same words, embeddings let applications compare their semantic representations.

2

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.

TEXT “How do I reset my password?”
→
EMBEDDING MODEL Encode meaning
→
VECTOR [0.12, -0.08, 0.44, …] many dimensions

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.

3

Embeddings Capture Semantic Similarity

Consider these sentences:

TEXT A

“How can I change my password?”

HIGH similar meaning
TEXT B

“I forgot my password and need a new one.”

TEXT A

“How can I change my password?”

LOWER different topic
TEXT C

“The weather is sunny today.”

Key point: Embeddings are useful because semantic relationships can be compared mathematically.
4

What Does “Dimension” Mean?

A vector has dimensions. A tiny educational example might represent a sentence with only three numbers:

sentence embedding [ 0.21, -0.73, 0.48 ] dimension 1 · dimension 2 · dimension 3

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.

5

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.

Illustrative embedding space
password login account invoice payment weather rain
Illustration only — real embedding spaces have many dimensions.
6

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.

cosine similarity cos(θ) = (A · B) / (||A|| × ||B||) Higher similarity generally means the vectors point in more similar directions.
Very similar 0.92 Example score
Somewhat similar 0.61 Example score
Less similar 0.08 Example score

Similarity scores depend on the embedding model and the implementation. Do not assume one universal threshold works for every dataset.

7

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.

Python Create an embedding
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])
What happened? The text was sent to an embedding model, which returned a numerical vector. Your application can now compare that vector with other vectors.
8

Python Example: Compare Two Embeddings

You can calculate cosine similarity with Python to estimate how closely two vectors are related.

Python Cosine similarity
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.

10

Embeddings 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.

01DocumentsSource content
→
02ChunkSmaller passages
→
03EmbedVectors
→
04StoreVector database
→
05RetrieveRelevant chunks
→
06GenerateGrounded answer
Remember: Embeddings retrieve relevant information; the language model then uses the retrieved context to generate the final response.
11

Where Are Embeddings Used?

Semantic search

Find content by meaning rather than exact keywords.

RAG

Retrieve relevant document chunks before generation.

Recommendations

Find items with similar semantic representations.

Clustering

Group similar documents, messages, or topics.

Classification

Use vector representations as features for downstream tasks.

Duplicate detection

Identify content that is semantically similar.

12

Important Embedding Decisions

ModelChoose an embedding model appropriate for your language and task.
DimensionsUnderstand the vector size and storage implications.
ChunkingFor documents, the text chunk size affects retrieval quality.
SimilarityChoose and evaluate an appropriate similarity or distance method.
EvaluationTest retrieval with real queries and expected relevant documents.
VersioningKeep compatible embedding versions when storing vectors.
13

Common Misconceptions

MYTH Each number has a simple human meaning.

Usually, the useful information is distributed across the complete vector.

MYTH Higher similarity always means “correct”.

Similarity is a retrieval signal. Your application still needs evaluation and validation.

MYTH Embeddings replace the language model.

Embeddings are representations used for tasks such as retrieval and comparison.

HANDS-ON PRACTICE

Build a Tiny Semantic Search Experiment

Create embeddings for five short FAQ questions and compare them with a new user query.

  1. Create five FAQ texts about account, billing, shipping, and technical support.
  2. Generate an embedding for each FAQ.
  3. Generate an embedding for the user query.
  4. Calculate similarity scores.
  5. Sort the FAQs from most similar to least similar.
Challenge: Try three different user queries and inspect whether the highest-ranked FAQ is actually the most useful one.
QUICK QUIZ

Test Your Understanding

1. What is an embedding?

A numerical vector representation of data that can capture useful relationships for comparison and retrieval.

2. Why are embeddings useful for semantic search?

They allow applications to compare representations by semantic similarity rather than relying only on exact word matches.

3. What is cosine similarity used for?

It is a common way to compare the orientation of two vectors and estimate their similarity.

4. How do embeddings fit into RAG?

Documents and queries are embedded into vectors so relevant document chunks can be retrieved for the generation step.

30-SECOND RECAP

Embeddings turn meaning into a form your application can compare.

TextHuman-readable content
EmbeddingNumerical vector
SimilarityCompare vectors
RetrievalFind relevant information

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.