An embedding model maps text to a fixed-length vector such that semantically similar text lands nearby. Similarity is measured by: - Cosine similarity — angle between vectors; the default for normalised text embeddings. - Dot product — cosine when vectors are unit-normalised. - Euclidean (L2) distance — sensitive to magnitude.
python
import numpy as np
def cosine(a, b):
return float(np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)))
q = embed("how do I rotate an API key?")
best = max(chunks, key=lambda c: cosine(q, c.vector))Practical notes: - Dimensions typically range 384-3072. Bigger is not automatically better; Matryoshka embeddings let you truncate dimensions to trade a little accuracy for large storage savings. - Always embed queries and documents with the *same* model and the same instruction prefix, if the model uses one. - Re-embedding is required whenever you change embedding models — versions are not interchangeable.
Gotcha: embeddings encode topical similarity, not logical truth. "The contract is not upgradeable" and "The contract is upgradeable" sit very close together.