Similarity and cosine similarity
Words made simple
A few useful meanings before you read the explanation.
- Cosine similarity
- A measure comparing the directions of two vectors. It is often used to estimate how similar two embeddings are.
- Embedding
- A list of numbers that represents the meaning of some content. Similar meanings can produce nearby lists of numbers.
- Vector
- An ordered list of numbers. An embedding is a vector used to represent content for tasks such as similarity search.
More words from this lesson
- Retrieval
- Finding and fetching information that is useful for the current question.
- Chunking
- Dividing a long document into smaller pieces so relevant parts can be found and sent to the model.
Overview
- Similarity measures how close two vectors are according to a selected metric.
- Cosine similarity compares their direction, effectively asking whether they point in a similar direction regardless of magnitude.
- Dot product and Euclidean distance are other common metrics.
Why this matters
A retrieval engine needs a numerical way to order candidate documents.
An everyday example
Two arrows pointing northeast are directionally similar even if one is longer.
A technical example
For nonzero vectors, cosine(a,b) = dot(a,b)/(length(a)*length(b)). The Python example in section 2 implements it.
How it works
- Encode query
- Calculate or approximate distances to indexed vectors
- Sort by score
- Return the top candidates after required filtering.
Architecture and flow
Query vector + document vectors → selected metric → ranked candidates.
The insurance assistant example
Compare a policy question with policy chunks, then verify whether the retrieved clauses actually answer it.
When to use it
When the metric matches the embedding model's recommendation and the index configuration.
When to avoid it
Do not reuse a score cutoff from another model or treat 0.9 as “90% probability of correctness.”
Interview answer
“Cosine compares vector directions. Its score helps rank candidates, but relevance thresholds must be calibrated on our own embedding model and data.”
Benefits, trade-offs, and production considerations
- Cosine ranges from -1 to 1 mathematically; database scores may be transformed into another range.
- Normalized vectors can make dot product equivalent for ranking.
- Reject zero or invalid vectors and do not mix incompatible model spaces.
Can you explain it without the notes?
Similarity and cosine similarity
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
Similarity measures how close two vectors are according to a selected metric. Cosine similarity compares their direction, effectively asking whether they point in a similar direction regardless of magnitude. Dot product and Euclidean distance are other common…
Turn this concept into a clear answer.
Practice this conceptKeep connecting the ideas.
Next lesson: Embedding models, generation, storage, and limitationsAdapted from 13_AI.MD · “Similarity and cosine similarity”. Examples are study fragments; framework behavior can depend on your version.