AssayAssay
03 / 08

Lesson 03 · Foundations · 13 min

Embeddings and similarity

An embedding turns text into a list of numbers positioned so that related text lands nearby. The corpus here becomes 1024 numbers per chunk. A query becomes 1024 numbers, and retrieval is the arithmetic of finding the closest ones.

Cosine similarityThe angle between two vectors, from 1 for identical direction down to 0 for unrelated. Length is ignored, which is why a short passage can beat a long one.

"Similar" means something specific and slightly alien: near in the space this model was trained to build. That is usually close to what you mean by relevant, and sometimes is not. A question and its answer are related but do not read alike — a question is short and interrogative, the answer is declarative.

Which is why embedding models for retrieval are asymmetric, and why the API takes an input type. The same sentence embedded as a query lands somewhere different from the same sentence embedded as a passage. Getting that wrong costs recall in a way nothing in your logs will point at.

// Indexing the corpus
embed({ texts: chunks, inputType: "passage" })

// Answering a question
embed({ texts: [question], inputType: "query" })
Reading similarity scores as a shapeFurniture and large items0.53Standard return window0.45Warranty period0.44Delivery charges0.310.40 floor · below this, nothing relevant was found
Real scores from this corpus for 'how long do I have to return a sofa'. The absolute numbers mean little; the gap between the first and second is what says retrieval found something distinct.

Read the scores in the lab as a shape, not a number. An absolute value of 0.5 means little on its own; a top hit at 0.53 with the next at 0.45 means retrieval found something distinct. Four hits clustered at 0.30 means it found nothing and is showing you the nearest available text.

Similarity has no idea what is true. Two passages giving different return windows are both highly similar to a question about return windows. Retrieval finds text about the topic; deciding which applies is somebody else's job, and lesson 6 is about whose.

Checkpoint

Find a question where the top two scores are close, and say why the model cannot tell them apart.

The easy case: one passage answers it, and retrieval either finds that passage or it does not.

Retrieval only, no generation. Watch the gap between the top score and the second — that gap tells you more than either number.

Nothing retrieved yet. Pick a question and press Retrieve — the passages come back with their similarity scores.