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













