Cosine similarity

A score for how closely two embedding vectors point in the same direction, regardless of how long the vectors are.

how close two embeddings arewhat does the 0.87 next to the result meanembedding similarity scorewhich text vectors point the same wayhow does it know two sentences mean the same thingwhat makes two chunks count as closecosign similaritywhy is this search result considered similar

See it

Live demo coming soon

What it is

Cosine similarity compares two vectors by the angle between them, ignoring their overall length. It divides their dot product by both magnitudes, giving 1 for the same direction, 0 for perpendicular directions, and -1 for opposite directions. Embedding search often uses it as a rough measure of semantic closeness.

Reach for it when vector direction carries the signal and magnitude should not dominate, especially in text retrieval. If vectors are L2-normalized first, cosine similarity becomes their dot product, which makes ranking simpler and often faster. OpenAI's text-embedding-3 vectors already arrive at unit length, so the two measures rank identically there.

Gotcha: some databases expose cosine distance, commonly 1 minus cosine similarity, so lower is better there. A high score only means the embedding model placed two items nearby; it does not prove relevance or truth. The formula is also undefined for a zero vector, which must be rejected or handled explicitly.

Ask AI for it

Implement cosine-similarity retrieval with PostgreSQL and pgvector. L2-normalize stored and query embeddings, rank with the <=> cosine-distance operator, convert distance to similarity as 1 - distance for display, and return the top 10 results above a threshold selected from labeled validation queries. Reject zero vectors and dimension mismatches, and add unit tests for identical, orthogonal, opposite, and zero-vector inputs.

You might have meant

embeddingsemantic searchvector databasererankinghybrid search