liveBook · Manning

Similarity metric

As explained in Build an Advanced RAG Application (From Scratch)

A similarity score is a numerical value indicating the degree of relatedness between two items or representations.

A similarity metric produces a numerical score describing how closely two items or vector representations are related. In semantic search, it compares a query embedding with document embeddings so related meanings can rank above exact keyword matches. section 3.3 section 4.3

Similarity-based retrieval proceeds through these steps:

  • Represent the query and searchable documents as dense vectors. section 4.3
  • Measure vector proximity with cosine distance, which emphasizes orientation, or Euclidean distance, which measures straight-line separation. section 4.3
  • Sort the resulting scores and return the highest-ranking matches. section 4.3
  • Use bi-encoder embeddings for comparison, or a cross-encoder that jointly processes two sentences for a fine-grained similarity score. section 4.3

Cosine search over hotel reviews

Encode the query "Hotel near the Louvre with great food nearby," compute cosine scores against review embeddings, select the five highest scores, and print each hotel, review, and score. A NumPy implementation calculates dot products and vector norms before sorting the scores in descending order. section 4.3

Figure 3.2 Imagine a map where countries are placed based on how similar their names are. This plot is like that map but for words instead of countries. Words close together have similar meanings. For example, China is closer to Russia than France because their names are more related.

Use the vector-space illustration to see why nearby representations indicate related meanings, such as dog, puppies, canines, and pets. section 3.3

How Build an Advanced RAG Application (From Scratch) covers it

How deep it goes
Explains vector proximity, contrasts cosine and Euclidean distance, and implements cosine top-k retrieval.
Examples
Uses dog-related terms, country-capital vectors, and hotel reviews to illustrate semantic proximity and ranked retrieval.
What it assumes
Assumes embeddings as dense vector representations and explains vectors as numerical lists used for comparison.
What you can do after
Apply it via NumPy to encode a query, compare embeddings, and retrieve top-k results.
How it connects
Connects embeddings, semantic search, cross-encoders, bi-encoders, cosine distance, and Euclidean distance.

Also explained in

Compare how 14 books explain similarity metric →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build an Advanced RAG Application (From Scratch). The section links point to the parts of the book it draws on.