Embedding – Mangrove

GlossaryLanguage models

Embedding

Definition: An embedding is a list of numbers (a vector) that represents a piece of text so that texts with related meanings get vectors that lie close together.

An embedding model turns a word, sentence or passage into a fixed-length list of numbers. OpenAI’s documentation says “The distance between two vectors measures their relatedness”, with small distances meaning closely related texts. Its text-embedding-3-small model returns 1,536 numbers per text by default and text-embedding-3-large returns 3,072, and a dimensions parameter can shorten them (OpenAI documentation). Simple arithmetic on word vectors can capture relations between words: the vector for “King” minus the vector for “Man” plus the vector for “Woman” lands closest to the vector for “Queen”, a result the word2vec paper cites from Mikolov, Yih and Zweig (2013).

Retrieval-augmented generation can use embeddings to find passages. Each passage in a collection is embedded once and stored. When a question arrives, it is embedded too, with the same model in OpenAI’s guide or with a separate question model in Lewis et al. (2020), and the passages whose vectors lie closest to the question’s vector are added to the model’s input. OpenAI’s guide measures closeness with cosine similarity, a score of how closely two vectors point in the same direction, and says the choice of distance function “typically doesn’t matter much” (OpenAI documentation). For example, an agent asking about a bank’s limits on lending to one borrower may retrieve a policy passage worded as a “single-counterparty exposure cap”, because the two texts mean similar things even though they share few words.

Tomas Mikolov, Kai Chen, Greg Corrado and Jeffrey Dean described two models for learning word vectors from large amounts of text in Efficient Estimation of Word Representations in Vector Space (16 January 2013), and later published code for them under the name word2vec. Learned word vectors are older, as the paper itself says: A Neural Probabilistic Language Model by Yoshua Bengio, Rejean Ducharme, Pascal Vincent and Christian Jauvin (Journal of Machine Learning Research, 2003) learned “a distributed representation for each word” together with a model of word sequences. Neither paper uses the term “embedding”: Bengio et al. call the vectors word feature vectors, and Mikolov et al. call them word vectors.