Embedding Space Lab
Semantic embeddings · cosine similarity · RAG

Meaning as geometry

An embedding model turns text into a list of numbers: a point in a space with hundreds or thousands of dimensions. Text with similar meaning lands in similar places, and consistent directions in that space line up with consistent shifts in meaning. These three demos use small, hand-built spaces so you can see each idea in two dimensions.

1Directions in the space carry meaning

The arrow from man to woman points the same way as the arrow from king to queen. Because that offset is consistent, vector arithmetic works on meaning: start at king, subtract man, add woman, and you land near queen.

Words Pair offsets Chosen direction Analogy arithmetic
Word family
Analogy: A − B + C ≈ ?
− +
Project every word onto a direction

This is a cartoon: real models use 384 to 3,072 dimensions, and the 2D picture you see in papers is a projection. In 2D, unrelated directions such as "royalty" and "age" are forced to overlap; with hundreds of dimensions they can be nearly perpendicular, so the model can store many independent features at once. The nearest-word search skips A, B and C, as word2vec demos do.

2Cosine similarity measures the angle, not the length

Two embeddings are compared by the angle between them. Cosine similarity is 1 when they point the same way, 0 when they are perpendicular (unrelated), and −1 when they point in opposite directions. Length is divided out, so a long document and a short query can still match. Drag the arrow tips.

cos θ = A·B / (‖A‖ ‖B‖)
0.00cosine similarity · angle θ = 0°
0dot product A·B
0 · 0lengths ‖A‖ · ‖B‖
0Euclidean distance
−1 opposite0 unrelated1 same direction
Try a case

The dashed line is the straight-line distance. Try "Same direction, different length": distance is large but cosine is 1. Once vectors are normalized, cosine equals the dot product, and ranking by distance gives the same order as ranking by cosine.

Try it on text (same toy embedder as section 3)

3Semantic search inside retrieval-augmented generation

RAG gives a language model facts it was not trained on. Documents are embedded ahead of time; at question time the question is embedded too, the closest chunks by cosine similarity are retrieved, and they are pasted into the prompt. Ask a question and press Search to watch each stage.

Index time · done onceQuery time · every question
Query tokens (highlighted words are in the toy embedder's vocabulary)

Vector index (2D projection of 12-dimensional vectors)

Document chunk Query / retrieved

Positions come from a principal-component projection of the real 12-number vectors, so nearness on the map is approximate. The ranking on the right uses the full vectors.

Ranked by cosine similarity

2
0.30

"Shared" counts words the question and chunk have in common. High similarity with zero shared words is semantic search doing what keyword search can't.

Why did it match? Query vector × chunk vector

The dot product is the sum of the bottom row. Click any result to compare it. Each column here is a named topic so you can read it; in a real model the dimensions are learned and rarely mean one clean thing.

Augmented prompt sent to the LLM


    

Generated answer (simulated)

The answer is assembled from the retrieved chunks to show grounding and citations. With nothing above the threshold, a well-built RAG system says it doesn't know rather than guessing.