liveBook · Manning

Semantic similarity

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

Semantic similarity is the degree to which two pieces of text express related meaning, typically estimated from their representations.

Semantic similarity measures how closely two pieces of text relate in meaning rather than how many words they share. Dense vector representations make this comparison useful for sentence matching, semantic caching, and retrieval systems. section 2.3.1 section 7.3.1

Sentence-BERT improves sentence-level comparison by encoding each sentence independently with shared network parameters, then comparing the resulting embeddings with cosine similarity. Its siamese and triplet-training designs emphasize relationships between similar and dissimilar sentences. section 2.3.1

Use semantic similarity with these practical distinctions:

  • Match paraphrased queries even when they share few words; semantic caching compares incoming query vectors with cached-query vectors and applies a similarity threshold. section 7.3.1
  • Do not treat semantic closeness as retrieval appropriateness: embeddings do not encode domain boundaries, authority, or freshness. section 7.2

Paraphrased cache queries

“What was Uber's revenue in 2021?” and “How much money did Uber make in 2021?” can become cache hits because their embeddings occupy nearby vector-space regions despite limited word overlap. section 7.3.1

Figure 2.6 A distinction between Bert (on the left) and Sentence-Bert (on the right) lies in their approach to semantic textual similarity. In BERT, the two sentences are initially concatenated and then fed into the network. Subsequently, the similarity is computed using the activation function. Conversely, in Sentence-Bert, the two sentences are independently input into Bert, and their cosine similarity is then calculated.

Compare BERT's concatenated sentence processing with Sentence-BERT's independent encoding and cosine-similarity calculation. section 2.3.1

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

How deep it goes
Explains vector comparison and SBERT training architectures, then stops short of prescribing a universal similarity threshold or retrieval policy.
Examples
Uses Uber revenue paraphrases and mixed API-documentation and financial-filing searches to expose matching and false-retrieval behavior.
What you can do after
Evaluate semantic matches while separating meaning similarity from domain-appropriate retrieval.
How it connects
Connects BERT, Sentence-BERT, embeddings, cosine similarity, semantic caching, semantic search, and agentic routing.

Also explained in

Compare how 8 books explain semantic similarity →

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.