LyraLearn AI Learning Platform
Exams
← Module 4 Β· Embeddings
🎧 Listen

Similarity and Distance

Once your text is a set of vectors, "find the most relevant document" becomes "find the nearest vector" β€” a geometry problem. The whole value of embeddings depends on one measurement: how close are two vectors? Get the distance metric right and semantic search just works; get it wrong and your retrieval silently degrades.

Two vectors from a common origin with a small angle between them illustrate cosine similarity, next to a ranked list of nearest chunks with a threshold line.

Nearby vectors mean similar meaning

The embedding model was trained to place similar text close together, so proximity in vector space is similarity in meaning. To answer a question, you embed the question, then look for the stored chunks whose vectors sit nearest to it. Those nearest chunks are your best candidates for a grounded answer β€” which is exactly what the AI Tutor does before it writes a response.

The standard way to measure "nearness" for text embeddings is cosine similarity.

Cosine similarity vs. cosine distance

Cosine similarity measures the angle between two vectors, ignoring their length:

Because angle ignores magnitude, a short question and a long paragraph about the same topic still score as highly similar β€” which is what you want.

In practice databases work with cosine distance, which is just 1 βˆ’ cosine similarity:

So when you search, you sort by distance ascending and take the smallest values. SQL Server 2025 exposes this directly as VECTOR_DISTANCE('cosine', a, b).

Searching in SQL Server 2025

A semantic search is conceptually a single ordered query. In LyraLearn the AI Tutor embeds your question into a 768-dim vector, then asks SQL Server for the closest lesson chunks:

SELECT TOP (5) chunk_text,
       VECTOR_DISTANCE('cosine', embedding, @question_vector) AS dist
FROM   LessonChunks
ORDER  BY dist ASC;

The TOP (5) is the k in k-nearest-neighbor search β€” you keep the few closest chunks, not everything. Those become the grounding context fed to the model.

Practical pitfalls

A few things that trip up real systems:

🧠 Quiz yourself on this lesson →

Ask the AI Tutor

Grounded in the course lessons β€” it cites its sources and says when it doesn't know.