Loading…
Glossary term
A list of numbers that represents the meaning of text, images or other data, used for search and similarity.
An embedding turns something, such as a word, sentence, image or product, into a list of numbers so that similar things get similar lists. 'Car' and 'automobile' end up close together, while 'car' and 'banana' land far apart.
Embeddings are the common currency of modern AI: they let computers measure meaning by distance.
Embed sentences and compare them
from sentence_transformers import SentenceTransformer
import numpy as np
model = SentenceTransformer("all-MiniLM-L6-v2")
a, b, c = model.encode(["a cat naps", "a kitten sleeps", "stock prices fell"])
cos = lambda x, y: float(x @ y / (np.linalg.norm(x) * np.linalg.norm(y)))
print(cos(a, b), cos(a, c)) # first is much higherThey power semantic search, recommendations, clustering and the retrieval step of RAG.