liveBook · Manning

Cosine distance

As explained in The Art of AI Product Development

Cosine distance measures the difference in orientation between two vectors and is commonly used to compare embeddings.

Cosine distance compares the orientation of two vectors. In semantic search, it estimates how relevant an embedded query is to embedded text by measuring their distance in embedding space. section 7.2.2

Applied in embedding-based retrieval:

  • Embed the user query with the same embedding model used for the text chunks. section 7.2.2
  • Retrieve the top-k text embeddings with the closest similarity; tune k because overly broad or narrow retrieval can hurt results. section 7.2.2
  • Treat the score as an approximation of relevance, not a guarantee: embeddings can lose information from the original text. section 7.2.2

Semantic search

A retrieval system embeds a user query, compares it with stored text embeddings using cosine distance, and returns the most similar documents. A Weaviate search may still return irrelevant results, requiring evaluation and tuning. section 7.2.2

Figure 7.6 The process of semantic search
A diagram of a diagram

AI-generated content may be incorrect.

Trace how query embedding, similarity comparison, and top-k retrieval form a semantic-search pipeline. section 7.2.2

How The Art of AI Product Development covers it

How deep it goes
Explains query embedding, similarity-based top-k retrieval, tuning trade-offs, and information loss without deriving the mathematics.
Examples
Uses embedded queries, stored text chunks, top-k results, and Weaviate retrieval as concrete examples.
What it assumes
Assumes familiarity with embeddings and introduces their use for comparing queries with stored text representations.
What you can do after
Apply it via an embedding database while tuning retrieval breadth and evaluating relevance problems.
How it connects
Connects cosine distance with embeddings, semantic search, information retrieval, and retrieval-augmented generation.

Also explained in

Compare how 6 books explain cosine distance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of AI Product Development. The section links point to the parts of the book it draws on.