liveBook · Manning

Cosine distance

As explained in AI Agents in Action

Cosine distance is a vector distance measure defined as one minus cosine similarity, ranging from zero for most similar vectors to two for semantically opposite vectors.

Cosine distance measures how far apart two vectorized texts are by subtracting cosine similarity from 1. It ranges from 0 for identical or most-similar items to 2 for semantically opposite items. section 8.3.1 section 8.3.4

Use the measure to:

  • Compare document vectors while disregarding their overall size. section 8.3.1
  • Interpret vector-store results where smaller distance means greater similarity. section 8.3.4
  • Support semantic search beyond matching exact keywords or phrases. section 8.3.4

Document similarity and semantic search

A TF–IDF example vectorizes eight documents, computes pairwise cosine similarities, and lets a user inspect one document against the set. A Chroma example embeds documents, stores them, and returns query results with cosine-distance scores. section 8.3.1 section 8.3.4

Listing 8.5 document_query_chromadb.py (relevant code sections)
embeddings = [get_embedding(doc) for doc in documents]     #1
ids = [f"id{i}" for i in range(len(documents))]           #1

chroma_client = chromadb.Client()               #2
collection = chroma_client.create_collection(
                       name="documents")       #2
collection.add(     #3
    embeddings=embeddings,
    documents=documents,
    ids=ids
)

def query_chromadb(query, top_n=2):      #4
    query_embedding = get_embedding(query)
    results = collection.query(
        query_embeddings=[query_embedding],
        n_results=top_n
    )
    return [(id, score, text) for id, score, text in
            zip(results['ids'][0],
                results['distances'][0], 
                results['documents'][0])]

while True:     #5
    query = input("Enter a search query (or 'exit' to stop): ")
    if query.lower() == 'exit':
        break
    top_n = int(input("How many top matches do you want to see? "))
    search_results = query_chromadb(query, top_n)

    print("Top Matched Documents:")
    for id, score, text in search_results:
        print(f"""
ID:{id} TEXT: {text} SCORE: {round(score, 2)}
""")    #5

    print("\n")
###Output
Enter a search query (or 'exit' to stop): dogs are lazy
How many top matches do you want to see? 3
Top Matched Documents:
ID:id7 TEXT: The dog is lazy but the brown fox is quick! SCORE: 0.24
ID:id5 TEXT: The brown fox is quick and the blue dog is lazy! SCORE: 0.28
ID:id2 TEXT: The quick brown fox jumps over the lazy dog. SCORE: 0.29

Shows the practical retrieval pattern: create embeddings, add documents to Chroma, query with an embedding, and return IDs, distances, and text. section 8.3.4

How AI Agents in Action covers it

How deep it goes
Defines the equation, range, and score interpretation, then demonstrates TF–IDF and embedding-based implementations without covering more advanced similarity measures.
Examples
Uses eight sample documents, a similarity matrix, interactive document selection, and a Chroma embedding query.
What it assumes
Assumes vector representations and introduces similarity through TF–IDF before moving to document embeddings.
What you can do after
Operate scikit-learn and Chroma workflows that compare vectors or retrieve documents using distance scores.
How it connects
Connects cosine similarity with vectorization, TF–IDF, embeddings, vector databases, and semantic search.

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 AI Agents in Action. The section links point to the parts of the book it draws on.