AI ENGINEERING: DEEP DIVE · MODULE 01 — THE MATH UNDERNEATH · 02 A hands-on series · math + code

Module 1.2

Six sentences, one ranking

Embed every sentence once. Rank them by cosine similarity to a query. That's it — that's nearest-neighbor search. Compute it below on the full Nimbus corpus.

QUERY
"Why did my Nimbus export fail?"
QUERY EMBEDDING (8 ILLUSTRATIVE DIMENSIONS)
THE FULL CORPUS, RANKED

The math

Ranking is just argmax over cosine similarity

Cosine similarity, recap
cos θᵢ  =  (q · dᵢ) / (‖q‖‖dᵢ‖)
computed once per candidate document dᵢ
best: 0.99
Nearest neighbor
doc*  =  argmax cos(q, dᵢ)
i ∈ {1, …, 6}
doc* = "…exceeded."

"Search" is nothing more than this: score every candidate the same way, then take the one with the highest score.

The code

From raw text to a ranked list

embed_corpus.py
from sentence_transformers import SentenceTransformer

model = SentenceTransformer("all-MiniLM-L6-v2")

corpus = [
    "Nimbus exports fail if the workspace quota is exceeded.",
    "API keys rotate every 90 days...",
    # ...4 more sentences, same corpus every module
]

corpus_vecs = model.encode(corpus)    # shape (6, 384) — real models use hundreds of dims
query_vec   = model.encode(query)     # shape (384,)
nearest_neighbor.py
import numpy as np

dots  = corpus_vecs @ query_vec                                    # one dot product per row, all at once
norms = np.linalg.norm(corpus_vecs, axis=1) * np.linalg.norm(query_vec)
scores = dots / norms                                              # cosine similarity, all 6 candidates

best = np.argmax(scores)
print(corpus[best], scores[best])   # the quota-exceeded sentence, ≈ 0.99

corpus_vecs @ query_vec is a matrix times a vector — six dot products computed in one shot. Module 1.3 is about exactly this operation.

The Nimbus thread

This ranking is the whole retrieval mechanism

Everything a vector database does on top of this is about doing it fast at scale — an approximate index (HNSW, IVF) so it doesn't have to score millions of documents one by one. The ranking math itself, argmax over cosine similarity, never changes. Module 5 builds that index; the six-sentence version above is the entire idea already.

So what

A "smart search" is embed once, then rank by angle — there's no separate understanding step hiding underneath.