How do we measure whether two embeddings are close? The usual measure is cosine similarity. It compares the direction of two vectors. The closer to one, the more similar the meanings.
We embed a customer question, and three sentences from the shop. Then we compute the cosine similarity between the question and each sentence. Run it. The delivery sentence scores highest, even though it shares hardly any words with the question. The unrelated sentences score lower.
cosine.pypython
import mathfrom langchain_google_genai import GoogleGenerativeAIEmbeddingsdef cosine(a, b): dot = sum(x * y for x, y in zip(a, b)) return dot / (math.sqrt(sum(x * x for x in a)) * math.sqrt(sum(y * y for y in b)))emb = GoogleGenerativeAIEmbeddings(model="gemini-embedding-001")question = emb.embed_query("When will my parcel arrive?")texts = ["Standard delivery takes 3 to 5 working days.", "Gift cards never expire.", "Story time is every Saturday at 10am."]for text, vec in zip(texts, emb.embed_documents(texts)): print(f"{cosine(question, vec):.3f} {text}")
Output
0.731 Standard delivery takes 3 to 5 working days.
0.580 Gift cards never expire.
0.563 Story time is every Saturday at 10am.
The exact numbers depend on the embedding model, so don't treat any value as a magic threshold. What matters is the ranking. The best match comes first. In practice, you never compute this by hand. A vector store does it for you, across thousands of chunks.
Cosine similarity compares embeddings, and the highest scores mean the closest meanings. Next, the vector store that searches all your chunks this way.