What are embeddings?

How do you make a computer understand that "vegan protein bowl" and "plant-based protein dish" mean similar things? The answer is embeddings, a way to convert text into numbers that capture semantic meaning.

How embeddings work

Text goes in, a vector of numbers comes out.

More dimensions let the model capture more nuances of meaning. With only 10 dimensions, the model could not distinguish "vegan protein bowl" from "vegan dessert" because there would not be enough axes to separate all the concepts. 768 is a sweet spot, enough to capture rich semantic meaning without being wastefully large.

EMBEDDING_MODEL is defined in your .env file from the setup step. It tells LiteLLM which provider to use for generating embeddings (e.g. "google/text-embedding-004" for Google or "text-embedding-3-small" for OpenAI).

02_embeddings.ipynb
python
import litellm

response = litellm.embedding(
    model=EMBEDDING_MODEL,
    input=["Vegan protein bowl with quinoa"]
)

vector = response.data[0]["embedding"]
print(f"First 10 values: {vector[:10]}")
print(f"Total dimensions: {len(vector)}")  # 768

Generate an embedding using LiteLLM

768 dimensions means the model captures 768 different learned features of meaning. Each number represents something the model learned during training, concepts like "food-related," "healthy," "protein content," and hundreds of other abstract features that humans can't easily name.

AI prompt: Try it with AI

Loading practice…

Quiz: Quiz

Loading practice…

Flashcards: Flashcards

Loading practice…