Embeddings Explained: How AI Turns Text into Numbers

Embeddings are the foundational concept behind semantic search, RAG, and recommendation systems — converting text (or images, audio) into a list of numbers that captures genuine semantic meaning.

from sentence_transformers import SentenceTransformer

model = SentenceTransformer('all-MiniLM-L6-v2')

embedding = model.encode("The cat sat on the mat")
print(len(embedding))   # e.g. 384 numbers - a "vector" representing this sentence's meaning

# Semantically SIMILAR sentences produce NUMERICALLY similar vectors
similar = model.encode("A feline rested on the rug")
different = model.encode("The stock market crashed today")

# Cosine similarity: similar/different meaning is measurable as vector distance
from sklearn.metrics.pairwise import cosine_similarity
cosine_similarity([embedding], [similar])    # high similarity score
cosine_similarity([embedding], [different])   # low similarity score

The Key Intuition

Embeddings place semantically similar concepts near each other in a high-dimensional mathematical space — “cat” and “feline” end up close together numerically, “cat” and “stock market” end up far apart, purely because the model learned these relationships from patterns across enormous amounts of training text, without anyone explicitly programming a rule about cats and felines being related.

Why This Unlocks So Many Applications

Once meaning is represented as numbers, you can use genuinely simple, well-understood mathematical operations (distance, similarity, clustering) to build search, recommendations, deduplication, and classification — embeddings are the bridge that lets standard, well-established computational techniques operate meaningfully on human language.