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





