Shaf Lab

Embeddings

Numeric vectors that represent text or other data by meaning.

Embeddings are numeric vectors that represent text, images, or other data by meaning. Two pieces of text with similar intent should sit close together in that space, even if they share few words. You produce embeddings with a model, store them in an index, and later compare a query vector to those you already have.

The word embedding has two common meanings. Inside a model, a token embedding is the learned vector looked up from a table for each token ID, and it is the model's first step on any input. A text embedding is a single vector for a whole passage, produced by an embedding model and used for search, clustering, and deduplication. In both cases training pushes items that appear in similar contexts toward nearby vectors, so meaning ends up encoded as geometry, and closeness is usually measured with cosine similarity.

They are the basis of vector search and of most retrieval-augmented generation. You also use them to cluster documents, to find near-duplicates, or to route a question to the right tool. The quality of the embedding model, and the way you chunk the original text, matter as much as the database you pick.

Chunk so that each vector stands for a coherent idea: a section, a function, a paragraph, not an entire book and not a single leftover sentence. Keep the raw text with the vector so you can show it later. When the corpus changes, re-embed what changed instead of hoping an old vector still means the same thing.

Do not embed secrets you would not store in clear text. A vector can leak more than it looks like, and it is still a copy of the input in another form.

Cosine similarity

A measure of how closely two vectors point in the same direction, ignoring their length.

Cosine similarity compares two vectors by the angle between them: their dot product divided by the product of their lengths. It ranges from -1 to 1. A value near 1 means the vectors point the same way, near 0 means they are unrelated, and negative values mean they point in opposing directions. Because length is ignored, a long passage and a short one can still score as similar if they are about the same thing.

If vectors are normalized to length one, which many embedding models do by default, cosine similarity equals the plain dot product, and vector indexes can use whichever is faster. Treat scores as relative, not absolute: each model has its own typical range, and unrelated texts may still score well above zero. Pick any cutoff by checking real examples, and use the metric the embedding model was trained with.