DevKits
Concept

How Embeddings Work — Vectors, Similarity & Why RAG Needs Them

An embedding turns a piece of text into a list of numbers (a vector) that captures its meaning, so that semantically similar texts end up close together in vector space. Embeddings are the foundation of semantic search, recommendation, clustering, and retrieval-augmented generation (RAG). This guide explains what an embedding is, how similarity is measured (cosine, dot product, Euclidean), and how vector databases use them.

Last updated:

Core Concepts

What an embedding is
An embedding model (like OpenAI's text-embedding-3, Cohere Embed, or an open model) maps text to a fixed-length vector — often 384, 768, 1024, 1536, or 3072 floating-point numbers. The key property is that meaning becomes geometry: 'dog' and 'puppy' land near each other, while 'dog' and 'tax return' land far apart. The model learned this during training so that related concepts cluster together, regardless of the exact words used.
Measuring similarity: cosine, dot product, Euclidean
To find related text you compare vectors. Cosine similarity measures the angle between two vectors (ignores magnitude) and is the most common choice for text — a value near 1 means very similar, near 0 means unrelated. Dot product multiplies and sums component-wise and is used when vectors are normalized (then it equals cosine) or when magnitude carries meaning. Euclidean (L2) distance measures straight-line distance — smaller is more similar. Most text-embedding pipelines normalize vectors and use cosine or dot product.
Vector databases and nearest-neighbour search
You rarely compare against millions of vectors one by one. A vector database (Pinecone, Weaviate, pgvector, Qdrant, Milvus) stores embeddings and uses Approximate Nearest Neighbour (ANN) indexes like HNSW to find the top-k most similar vectors in milliseconds. You embed a query, ask the database for its nearest neighbours, and get back the most semantically relevant stored items — the retrieval step behind semantic search and RAG.
Why RAG depends on embeddings
Retrieval-Augmented Generation grounds an LLM in your own data. You chunk documents, embed each chunk, and store the vectors. At query time you embed the user's question, retrieve the most similar chunks, and paste them into the prompt as context. The LLM then answers using that retrieved text instead of only its training data — reducing hallucination and letting it cite fresh, private information. Without embeddings there is no efficient 'find the relevant chunks' step.

Frequently Asked Questions

What is the difference between cosine similarity and dot product?

Cosine similarity measures the angle between two vectors and ignores their length, so it only cares about direction (meaning). Dot product multiplies components and is affected by magnitude. When vectors are normalized to unit length, dot product and cosine similarity give the same ranking. For text embeddings, cosine (or dot product on normalized vectors) is the usual choice.

How big is an embedding vector?

It depends on the model. Common sizes are 384, 768, 1024, 1536 (OpenAI text-embedding-3-small), and 3072 (text-embedding-3-large). Larger vectors can capture more nuance but cost more to store and compare. Some models support dimensionality reduction so you can trade a little accuracy for cheaper storage.

Do I need a vector database for embeddings?

For a few thousand vectors you can compute cosine similarity in memory or with a library. Beyond that, a vector database with an ANN index (HNSW) gives you fast top-k search over millions of vectors. pgvector adds vector search to Postgres; Pinecone, Weaviate, Qdrant, and Milvus are dedicated options.

Try these related tools