Latticework

Command Palette

Search for a command to run...

Embeddings

Use Cases

18 min

Explanation

Once you can measure similarity between two vectors, the natural next step is retrieval: given a query vector, find the k most similar vectors out of a whole pool of candidates. This is the core operation behind semantic search, recommendation systems, and retrieval-augmented generation (RAG) — "find me the most relevant things to this query" is always, underneath, "rank everything by similarity and take the top k."

def cosine_similarity(a, b):
    dot = sum(x * y for x, y in zip(a, b))
    mag_a = sum(x ** 2 for x in a) ** 0.5
    mag_b = sum(x ** 2 for x in b) ** 0.5
    return dot / (mag_a * mag_b)

def top_k_similar(query, candidates, k):
    scored = [(name, cosine_similarity(query, vec)) for name, vec in candidates.items()]
    scored.sort(key=lambda pair: -pair[1])
    return [name for name, _ in scored[:k]]
Try it

This is a brute-force O(n) nearest-neighbor search -- real vector databases (Pinecone, pgvector, FAISS) use approximate-nearest-neighbor indexes to make this sub-linear at scale, but the underlying question they're answering is exactly this.

Loading editor…
Exercise

Write `top_k_similar(query, candidates, k)`: `candidates` is a dict mapping name to vector. Return a list of the `k` names whose vectors have the highest cosine similarity to `query`, most similar first.

Quiz

What real system is `top_k_similar` a simplified version of?

Checkpoint

You can implement brute-force top-k nearest-neighbor retrieval by cosine similarity, the core operation behind semantic search and recommendations.