liveBook · Manning

Cosine similarity

As explained in Build an AI Agent (From Scratch)

Cosine similarity is a measure of the angular similarity between two vectors, commonly computed as their dot product divided by the product of their magnitudes.

Cosine similarity measures how closely two vectors point in the same direction. Its value is the cosine of the angle between them: values range from −1 to 1, with values nearer 1 indicating greater similarity. section 5.2.2

In semantic search, an embedding model converts queries and documents into vectors. Cosine similarity then ranks documents by meaning rather than exact wording, allowing a query about artificial intelligence to retrieve content about machine learning. section 5.2.2 section 5.6

A practical vector-search pipeline applies cosine similarity in four steps:

  • Generate an embedding for each stored document or text chunk. section 5.3.1
  • Generate an embedding for the incoming query. section 5.3.3
  • Calculate the query’s similarity against all stored chunk embeddings. section 5.3.3
  • Sort scores in descending order and return the top_k chunks with their scores. section 5.3.3

Comparing related sentences

Using scikit-learn’s cosine_similarity with embeddings, “The cat is sleeping on the couch” scores 0.385 against “A kitten is playing with a toy,” but 0.200 against “The dog is running in the park.” The higher score reflects semantic relatedness even without identical keywords. section 5.3.1

Listing 5.5 Implementing vector search to find chunks
def vector_search(query, chunks, chunk_embeddings, top_k=3):
    """Find the most similar chunks to the query."""
    query_embedding = get_embeddings(query)
    similarities = cosine_similarity(query_embedding, chunk_embeddings)[0]
    top_indices = similarities.argsort()[::-1][:top_k]

    results = []
    for idx in top_indices:
        results.append({
            'chunk': chunks[idx],
            'similarity': similarities[idx]
        })
    return results

Inspect the compact vector_search implementation, including query embedding, batch similarity calculation, descending ranking, and top-k result construction. section 5.3.3

How Build an AI Agent (From Scratch) covers it

How deep it goes
Defines angular similarity and demonstrates implementation with embeddings, but does not derive the underlying vector formula.
Examples
Compares cat, kitten, and dog sentences and ranks documents about artificial intelligence, machine learning, Python, and pets.
What it assumes
Assumes basic vectors and embeddings while teaching semantic similarity through concrete text comparisons.
What you can do after
Apply it via scikit-learn to rank embedded queries against stored document or chunk vectors.
How it connects
Connects embeddings, vector search, keyword search, Euclidean distance, and hybrid search.

Also explained in

Compare how 14 books explain cosine similarity →

Related concepts

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