Embeddings & Vector Search
Generative & Agentic AI · Working with LLMs · Intermediate · 14 min
Turn meaning into numbers so computers can find 'similar' things.
A map where every sentence is a pin with an arrow from the centre. Two sentences mean the same thing when their arrows point the same way, no matter how long the arrows are.
The idea in everyday words
Imagine a giant map where every sentence is a pin. Sentences with similar meaning are pinned close together: 'puppy' sits next to 'dog', far from 'invoice'. An embedding is a pin's GPS coordinates: just a list of numbers.
What is an embedding?
A list of numbers (e.g. 768 or 1536 of them) that captures meaning.
- "king" − "man" + "woman" ≈ "queen" (famous example)
- Models: OpenAI text-embedding-3, Cohere, open source BGE, E5, all-MiniLM (via Sentence-Transformers).
Similarity = angle between arrows
Cosine similarity compares the directions of two arrows: 1 = same direction (very related), 0 = at right angles (unrelated), −1 = pointing exactly the opposite way.
Careful: −1 is about geometry, not "opposite meaning". For real text embeddings, cosine tells you how related two texts are (same topic), not whether they agree. Antonyms like "hot" and "cold" often score high, because they appear in very similar sentences. Strongly negative scores are rare with real text embeddings.
cos(a, b) = (a · b) / (|a| × |b|)
[1, 0] vs [1, 0] → 1.0, [1, 0] vs [0, 1] → 0.0
Vector databases
Store millions of embeddings and find the nearest ones fast (approximate nearest neighbour, e.g. HNSW). Open source: Chroma, Qdrant, Weaviate, Milvus, pgvector. Managed: Pinecone, AWS OpenSearch, GCP Vertex AI Vector Search, Azure AI Search.
Step by step
- Embed docs:
'Dogs are loyal' → [0.8, 0.1, …]
- Store:
Save vectors in Chroma / pgvector.
- Embed query:
'Which pets are faithful?' → [0.79, 0.12, …]
- Search:
Nearest vector = 'Dogs are loyal' (cosine 0.93).
Code example
import math
def cosine(a, b):
dot = sum(x * y for x, y in zip(a, b))
return dot / (math.sqrt(sum(x*x for x in a)) * math.sqrt(sum(y*y for y in b)))
docs = {"dog": [0.9, 0.1, 0.0], "puppy": [0.85, 0.15, 0.05], "invoice": [0.0, 0.1, 0.95]}
query = [0.88, 0.12, 0.02]
for name, vec in docs.items():
print(name, round(cosine(query, vec), 3))Interview tips
Explain cosine similarity with a 2D drawing.
Know ANN index trade-off: speed vs exact recall.
Mention dimensionality and storage cost (1536 floats × 4 bytes ≈ 6KB per vector).
Challenge: Cosine Similarity
Return the cosine similarity of two equal-length vectors, rounded to 2 decimals. If either vector has zero length (all zeros), return 0.
Example: cosine([1, 0], [1, 1]) → 0.71
Solve it in the browser with tests, hints and an AI coach.
Practice problems
- Amazon Titan Text Embeddings models (Bedrock docs)
- LeetCode: K Closest Points to Origin (nearest-neighbour thinking) (Medium)
- Sentence Transformers documentation (open-source embeddings)
- Amazon Bedrock samples (embeddings and RAG notebooks)
More in Working with LLMs
- Prompt Engineering
- In-Context Learning & Inference Parameters
- Decoding: Greedy, Beam Search, Sampling & Structured Output
- Building With LLM APIs
- Context Engineering & Prompt Caching
Previous: Context Engineering & Prompt Caching · Next: GenAI on AWS: The Big Picture