Page languageFrançaisEnglish
Teacher area
Laurent PoupetClaude trainer and artificial intelligence architectStudent area

Category · Data and retrieval-augmented generation

What is cosine similarity?

Cosine similarity is a way of measuring whether two embeddings are close, by looking at the angle between them. The closer it is to 1, the closer the meanings of the two texts. In pgvector, the <=> operator gives the cosine distance.

By , Claude trainer and artificial intelligence architect · Updated

How it works

  • It equals the dot product of the two vectors divided by the product of their lengths. It therefore depends only on the direction of the vectors, not on their length.
  • OpenAI embeddings are already normalised to a length of one, so a simple dot product gives the same value, faster to compute.
  • In pgvector, the cosine distance equals one minus the similarity. The smaller the distance, the closer the two texts are in meaning.

The trap to avoid

The trap is setting a universal threshold. Scores do not have the same scale from one embedding model to another, so the threshold is recalibrated at every model change.

Related terms

Where this is taught

This idea comes up throughout the lessons; we work on it with your own examples during a lesson.

Lesson content (step-by-step instructions, exercises with solutions, quizzes) is for students only, with an access code received after the first lesson.

See the full program

All glossary terms

Trial lesson · 30 min · $20

Book (new tab)