Skip to content

Your first embedding in 5 minutes

Theory later. Right now the goal is to see actual numbers come out of a real model, because everything after this is easier once you’ve watched it happen once.

Pick whichever route suits you. Both take about the same time.

No account, no card, no API key. You download a small model and run it on your own machine. First run pulls about 90 MB and takes a minute; after that it’s instant and offline.

  1. Install the library

    Terminal window
    pip install sentence-transformers

    It’ll pull in PyTorch, so this is the slow step — a couple of minutes, and a few hundred MB. Go make tea.

  2. Write the script

    Save this as first_embedding.py:

    first_embedding.py
    from sentence_transformers import SentenceTransformer
    model = SentenceTransformer("all-MiniLM-L6-v2")
    text = "The spare set for the front door is in the kitchen drawer."
    vector = model.encode(text)
    print(f"Text: {text}")
    print(f"Dimensions: {len(vector)}")
    print(f"First five: {vector[:5]}")
  3. Run it

    Terminal window
    python first_embedding.py

You’ll see something along these lines (your exact numbers will differ slightly by version and hardware, which is fine and expected):

Text: The spare set for the front door is in the kitchen drawer.
Dimensions: 384
First five: [-0.0234 0.0871 -0.0412 0.0159 0.0623]

A number where you expected a sentence. You gave the model twelve words and it handed back a long list of floating-point values. That list is the embedding. There’s no other magic object hiding behind it — this is the thing everybody’s talking about.

A fixed length. 384 numbers from MiniLM, 1,536 from OpenAI’s small model. Notice that the length has nothing to do with how long your text was. Embed a single word or embed three paragraphs; you get the same size list either way. That’s what makes them comparable — you can’t measure the distance between a 5-element list and a 900-element one, so the model always gives you the same shape.

Numbers that mean nothing individually. The first value being 0.0212 tells you precisely nothing. Don’t stare at it. As we said on the previous page, these are only useful in comparison to other embeddings.

You must use the same model for everything you intend to compare. An embedding from MiniLM and an embedding from OpenAI are not comparable, even if you force the dimensions to match. They’re coordinates in two unrelated spaces. Comparing them is like comparing a grid reference in London to one in Tokyo — the numbers are similar-looking and the answer is meaningless.

That includes model versions. If you re-embed your database with a newer model, you have to re-embed all of it, not just the new rows. Mixing generations is one of the most common ways a working search quietly turns to mush, and it produces no error message at all — just steadily worse results that nobody can explain.

You’ve made one embedding. Now make nine and build the actual search:

Or if you’d rather understand before you build, how text becomes numbers explains what happened inside that encode() call. Wondering which model to use at all? Choosing an embedding model covers the ones people actually pick.