Vector Embeddings Cheat Sheet
Generate, store, and search dense vector embeddings for semantic search, covering models, distance metrics, and vector databases.
2 PagesBeginnerJan 15, 2026
Generate an Embedding
Convert text into a fixed-length dense vector using a sentence embedding model.
python
from sentence_transformers import SentenceTransformermodel = SentenceTransformer("all-MiniLM-L6-v2") # 384-dim vectorssentences = ["How do I reset my password?", "Password reset instructions"]embeddings = model.encode(sentences, normalize_embeddings=True)print(embeddings.shape) # (2, 384)
Compute Similarity
Measure semantic closeness between two vectors with cosine similarity.
python
import numpy as npdef cosine_similarity(a, b): return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))score = cosine_similarity(embeddings[0], embeddings[1])print(f"similarity: {score:.4f}") # closer to 1.0 = more similar
Store and Query in a Vector Database
Upsert embeddings into a vector database and run an approximate nearest-neighbor query.
python
import pineconepc = pinecone.Pinecone(api_key="...")index = pc.Index("docs")index.upsert(vectors=[ ("doc-1", embeddings[0].tolist(), {"text": sentences[0]}), ("doc-2", embeddings[1].tolist(), {"text": sentences[1]}),])query_vec = model.encode("reset my login").tolist()results = index.query(vector=query_vec, top_k=3, include_metadata=True)
Distance Metrics
The most common ways to compare two vectors, and when to use each.
- Cosine similarity- angle between vectors; ignores magnitude, most common for text
- Dot product- fast, magnitude-sensitive; use with normalized embeddings for ranking
- Euclidean (L2) distance- straight-line distance; common for image and general-purpose embeddings
- Hamming distance- bit differences between binary/quantized vectors, very fast
- HNSW index- graph-based ANN index trading recall for speed at scale
Popular Vector Databases
Common storage backends for production embedding search.
- pgvector- Postgres extension, good when you already run Postgres
- Pinecone- managed, serverless, low-ops vector search
- Qdrant- open source, self-hostable, strong filtering support
- Weaviate- open source with built-in hybrid search and modules
- Chroma- lightweight, embedded, ideal for prototyping and small apps
Pro Tip
Normalize embeddings at generation time so you can use the cheaper dot-product metric in your vector index — it's mathematically equivalent to cosine similarity but avoids a normalization step on every query.
Was this cheat sheet helpful?
Explore Topics
#VectorEmbeddings#VectorEmbeddingsCheatSheet#DataScience#Beginner#GenerateAnEmbedding#ComputeSimilarity#Store#Query#Databases#MachineLearning#CheatSheet#SkillVeris