Skip to content
RAG infrastructure on Kubernetes · Chapter 3 of 20 Day 2: Measuring Embeddings – Which Classification System Fits?

Day 2: Measuring Embeddings – Which Classification System Fits?

Goal: Compare two embedding models with the same cards and questions – dimensions, hits, duration, memory – and find out what happens with texts that are too long · Duration: approx. 75 min · Prerequisite: Day 1

State before this chapter

# on rag1
ls ~/kurs/01/karte-*.txt ~/kurs/01/karten-anfrage.json

Expected: the four cards and the request from Day 1.

Introduction

Yesterday you simply used bge-m3. Today you ask the question that comes first in every RAG project and becomes expensive later if you answer it wrongly: Which embedding model? It determines what is found, how fast texts are classified, how much memory every vector costs – and it can only be swapped later with a complete re-indexing (Day 13).

You compare bge-m3 with nomic-embed-text, a smaller, faster model that was trained mainly on English texts. Both get the same four cards and the same four questions.

Today you learn:

  • to derive a request for another model from an existing one,
  • to compare the hits of two models for the same questions and to recognise a miss,
  • why some models expect prefixes like search_query: – and what they bring,
  • to measure warm run time and memory requirements per model,
  • that Ollama silently truncates texts that are too long – and how to prevent that.

Background

The picture for today: two libraries, two classification systems

Imagine two libraries with the same books. One sorts by a finely structured system that also knows German terms; the other by a coarser one made for English titles. If you ask both about “printout doesn’t come out”, the librarians walk to different shelves. Which system is better is not decided by the catalogue in the shop window but by a test with your questions and your books.

Two more things belong to the classification system:

  • How many shelf axes it has – the dimension. More axes means finer distinctions, but also bigger index cards in the cabinet.
  • How long a card may be. If a text is too long, the librarian cuts it off – and by default does not tell you.

The terms

Term Meaning in the picture
Dimension number of entries in the vector (bge-m3: 1024, nomic-embed-text: 768) number of shelf axes
Background noise similarity between texts that have nothing to do with each other how close unrelated shelves stand anyway
Margin difference between the best and the second-best hit how clearly the librarian can decide
Prefix fixed text before question or document that some models saw during training “question” or “book” stamp on the slip
Context length how many tokens a model can process at once maximum card length
Truncate inputs that are too long are shortened instead of rejected the librarian tears off the rest of the card

Remember: You choose an embedding model with your own texts and questions, not by leaderboards. It stays for a long time: whoever changes it has to classify everything again.

Drill 1: The same cards with nomic-embed-text

Where you are: on rag1.

What you have afterwards: the four cards as 768-dimensional vectors from nomic-embed-text.

Step 1 – a working folder with yesterday’s cards.

# on rag1
mkdir -p ~/kurs/02
# on rag1
cd ~/kurs/02
# on rag1
cp ../01/karte-*.txt ../01/karten-anfrage.json .

cp <sources> . copies the cards and the request into the current folder (.).

Step 2 – derive the request for the other model. Only the model field changes:

# on rag1
jq '.model = "nomic-embed-text"' karten-anfrage.json > karten-anfrage-nomic.json

.model = "…" sets the field to a new value; everything else stays as it was.

Step 3 – classify.

# on rag1
curl -s http://127.0.0.1:11434/api/embed -d @karten-anfrage-nomic.json -o karten-nomic.json

Step 4 – check dimension and length.

# on rag1
jq '[.embeddings[] | length]' karten-nomic.json

Expected:

[
  768,
  768,
  768,
  768
]
# on rag1
jq '[.embeddings[] | map(. * .) | add | sqrt]' karten-nomic.json

Expected:

[
  1.0000001957962796,
  0.9999995818052767,
  1.0000000329248961,
  1.0000000289051725
]

What you see: 768 instead of 1024 numbers, also normalised. So the calculation from Day 1 works the same way.

Step 5 – duration and tokens.

# on rag1
jq '{total_duration, load_duration, prompt_eval_count}' karten-nomic.json

Expected:

{
  "total_duration": 5183672473,
  "load_duration": 4088684885,
  "prompt_eval_count": 270
}

What you see: 5.2 s cold, 4.1 s of it loading – bge-m3 needed 17.6 s yesterday. Notice: 270 tokens instead of 216. nomic splits the same German sentences into more pieces – a hint that its vocabulary was built for English.

Remember: Same texts, different token count: every model has its own vocabulary. How much text fits into a model therefore also depends on the language.

Save your progress? Sign up for free

Drill 2: Who finds the right card?

Where you are: on rag1, in ~/kurs/02.

What you have afterwards: the similarities with nomic for the four questions from Day 1 – for a direct comparison with bge-m3.

Today the output is rounded to three decimal places (* 1000 | round / 1000) and written on one line with -c, so that the questions are easier to compare.

Step 1 – classify question 1.

# on rag1
curl -s http://127.0.0.1:11434/api/embed -d '{"model":"nomic-embed-text","input":"Wie starte ich den Druckdienst neu?"}' -o f1-nomic.json

Step 2 – compare.

# on rag1
jq -c -n --slurpfile k karten-nomic.json --slurpfile f f1-nomic.json '$f[0].embeddings[0] as $q | [$k[0].embeddings[] | [., $q] | transpose | map(.[0] * .[1]) | add * 1000 | round / 1000]'

Expected (print, VPN, leave, backup):

[0.733,0.535,0.598,0.583]

Step 3 – the three other questions. Repeat steps 1 and 2 with f2-nomic.json to f4-nomic.json and the questions from Day 1 (they are in German, as in the commands; the backup question is English). In the author’s lab the result was:

Question Print VPN Leave Backup Hit
Wie starte ich den Druckdienst neu? 0.733 0.535 0.598 0.583 correct
Mein Ausdruck kommt nicht raus, was kann ich tun? 0.578 0.522 0.630 0.578 wrong
How do I restore a deleted file? 0.463 0.370 0.376 0.557 correct
Wie bekomme ich einen neuen Laptop? 0.594 0.589 0.581 0.622 (none matches)

For comparison, bge-m3 from Day 1:

Question Print VPN Leave Backup Hit
print service 0.754 0.439 0.385 0.464 correct, margin 0.29
printout 0.463 0.386 0.411 0.384 correct, margin 0.05
restore 0.418 0.404 0.358 0.573 correct, margin 0.16
laptop 0.428 0.432 0.435 0.381 (none matches)

What you see:

  • For the paraphrased question, nomic fetches the leave card. A user with a printing problem would get an answer about absences.
  • The background noise is higher with nomic (around 0.55 to 0.6 instead of around 0.4). As a result, the margin shrinks even for the clear-cut question: 0.733 against 0.598 instead of 0.754 against 0.464.
  • The numbers of two models are not comparable: 0.58 is noise with nomic, with bge-m3 it would almost be a hit. Thresholds always apply to one model only.

Remember: A miss comes along with a perfectly normal number. Only the comparison with questions whose correct card you know reveals it.

Save your progress? Sign up for free

Drill 3: nomic’s prefixes

Where you are: on rag1, in ~/kurs/02.

What you have afterwards: the answer to the question whether the model was merely used wrongly.

nomic-embed-text was trained with prefixes: documents start with search_document:, questions with search_query:. Without them, question and document never sat next to each other in training. Before you discard a model, you check whether it was used correctly.

Step 1 – put the prefix in front of every card.

# on rag1
jq '.input |= map("search_document: " + .)' karten-anfrage-nomic.json > karten-anfrage-nomic-praefix.json

.input |= map(…) changes every element of the input list: "search_document: " + . prepends the prefix.

# on rag1
jq -r '.input[0]' karten-anfrage-nomic-praefix.json

Expected:

search_document: Druckdienst neu starten: Melde dich am Druckserver druck01 an und führe sudo systemctl restart cups aus. Prüfe danach mit lpstat -r, ob der Dienst läuft. Hängen Aufträge, leere die Warteschlange mit cancel -a.

Step 2 – classify.

# on rag1
curl -s http://127.0.0.1:11434/api/embed -d @karten-anfrage-nomic-praefix.json -o karten-nomic-praefix.json

Step 3 – the paraphrased question with prefix.

# on rag1
curl -s http://127.0.0.1:11434/api/embed -d '{"model":"nomic-embed-text","input":"search_query: Mein Ausdruck kommt nicht raus, was kann ich tun?"}' -o f2-nomic-praefix.json
# on rag1
jq -c -n --slurpfile k karten-nomic-praefix.json --slurpfile f f2-nomic-praefix.json '$f[0].embeddings[0] as $q | [$k[0].embeddings[] | [., $q] | transpose | map(.[0] * .[1]) | add * 1000 | round / 1000]'

Expected:

[0.597,0.527,0.605,0.597]

What you see: the margin has become smaller, but the leave card is still ahead. With the prefixes, all four questions looked like this in the author’s lab:

Question Print VPN Leave Backup
print service 0.730 0.552 0.581 0.598
printout 0.597 0.527 0.605 0.597
restore 0.584 0.517 0.499 0.685
laptop 0.605 0.599 0.573 0.633

For the English question, the hit rises to 0.685, the margin stays almost the same (0.10 instead of 0.09). For German texts the basic problem remains: the model does not place “Ausdruck” (printout) close enough to “Druckdienst” (print service).

Remember: First use the model correctly (prefixes, read the documentation), then judge. Here the verdict stands: for German cards, bge-m3 is the better classification system.

Save your progress? Sign up for free

Drill 4: What does a model cost?

Where you are: on rag1, in ~/kurs/02.

What you have afterwards: warm run time, memory and vector size of both models.

You measure warm: send every request once so that the model is loaded, and measure the second one.

Step 1 – one question with bge-m3, warm.

# on rag1
curl -s http://127.0.0.1:11434/api/embed -d '{"model":"bge-m3","input":"Wie starte ich den Druckdienst neu?"}' -o w-bge.json

Run the command twice, then:

# on rag1
jq '{total_duration, load_duration, prompt_eval_count}' w-bge.json

Expected:

{
  "total_duration": 155474308,
  "load_duration": 5557764,
  "prompt_eval_count": 10
}

Step 2 – the same with nomic.

# on rag1
curl -s http://127.0.0.1:11434/api/embed -d '{"model":"nomic-embed-text","input":"Wie starte ich den Druckdienst neu?"}' -o w-nomic.json

Again twice, then jq '{total_duration, load_duration, prompt_eval_count}' w-nomic.json:

{
  "total_duration": 102785945,
  "load_duration": 2220201,
  "prompt_eval_count": 15
}

Step 3 – all four cards, warm.

# on rag1
curl -s http://127.0.0.1:11434/api/embed -d @karten-anfrage.json -o k-bge.json
# on rag1
jq '{total_duration, prompt_eval_count}' k-bge.json

Expected:

{
  "total_duration": 2033278824,
  "prompt_eval_count": 216
}

With karten-anfrage-nomic.json and k-nomic.json, the lab showed "total_duration": 1107745794 at 270 tokens.

Step 4 – memory.

# on rag1
ollama ps

Expected (excerpt):

NAME                       ID              SIZE      PROCESSOR    CONTEXT    RUNNER      UNTIL
nomic-embed-text:latest    0a109f422b47    389 MB    100% CPU     2048       llamacpp    4 minutes from now
bge-m3:latest              790764642607    1.2 GB    100% CPU     4096       llamacpp    4 minutes from now

Step 5 – size of the vectors.

# on rag1
ls -l karten-anfrage.json k-bge.json k-nomic.json

Expected (excerpt, only size and name):

866 karten-anfrage.json
51137 k-bge.json
38106 k-nomic.json

What you see: four cards with 866 bytes of text altogether become 51 KB of vectors as JSON. In binary it is less: 1024 numbers × 4 bytes = 4 KB per card with bge-m3, 3 KB with nomic. With a million cards that is 4 GB against 3 GB – just for the numbers, without an index.

bge-m3 nomic-embed-text
Dimension 1024 768
Memory when loaded 1.2 GB 389 MB
one question, warm 0.16 s 0.10 s
four cards, warm 2.0 s 1.1 s
paraphrased German question correct wrong

Remember: nomic is about twice as fast and needs a third of the memory – and finds the wrong card for German texts. Fast and wrong is never the better choice in RAG.

Save your progress? Sign up for free

Drill 5: Cards that are too long

Where you are: on rag1, in ~/kurs/02.

What you have afterwards: proof that Ollama silently truncates texts that are too long, and the two settings that change that.

Step 1 – build a long text. jq can multiply a text: $k * 60 concatenates the print card 60 times.

# on rag1
jq -n --rawfile k karte-druck.txt '{model: "nomic-embed-text", input: ($k * 60)}' > lang-anfrage.json
# on rag1
jq '.input | length' lang-anfrage.json

Expected:

12600

12,600 characters.

Step 2 – have it classified.

# on rag1
curl -s http://127.0.0.1:11434/api/embed -d @lang-anfrage.json -o lang.json
# on rag1
jq '{prompt_eval_count, total_duration}' lang.json

Expected:

{
  "prompt_eval_count": 2048,
  "total_duration": 8332268529
}

What you see: exactly 2048 tokens – and no error. The rest of the text was cut off. If the decisive information were in the second half of a document, it would simply not be in the vector.

Step 3 – forbid truncation. The field truncate: false turns the silent shortening into an error:

# on rag1
jq '. + {truncate: false}' lang-anfrage.json > lang-anfrage-streng.json

. + {…} adds a field to the object.

# on rag1
curl -s -w '\nHTTP %{http_code}\n' http://127.0.0.1:11434/api/embed -d @lang-anfrage-streng.json -o lang-streng.json

-w '\nHTTP %{http_code}\n' prints the HTTP status code after the request.

Expected:


HTTP 400
# on rag1
cat lang-streng.json

Expected:

{"error":"the input length exceeds the context length"}

Step 4 – and bge-m3? According to ollama show bge-m3, the model can handle 8192 tokens:

# on rag1
ollama show bge-m3

Expected (excerpt):

    architecture        bert
    parameters          566.70M
    context length      8192
    embedding length    1024

Build the same long request for bge-m3 as lang-bge.json (as in step 1, with "bge-m3") and send it. In the lab, "prompt_eval_count": 2048 came back here too, after 22.1 s. With truncate: false the answer was HTTP 413 with the same error message. So by default Ollama processes only 2048 tokens per text – no matter what the model could do.

Step 5 – raise the limit. num_ctx alone (context length) was not enough in the lab; 413 kept coming. The text apparently also has to fit into a single processing batch – only together with num_batch did it work:

# on rag1
jq '. + {truncate: false, options: {num_ctx: 8192, num_batch: 8192}}' lang-bge.json > lang-bge-8kb.json
# on rag1
curl -s -w '\nHTTP %{http_code}\n' http://127.0.0.1:11434/api/embed -d @lang-bge-8kb.json -o lang-bge-8kb-out.json

Expected:


HTTP 200
# on rag1
jq '{prompt_eval_count, total_duration, load_duration}' lang-bge-8kb-out.json

Expected:

{
  "prompt_eval_count": 3722,
  "total_duration": 60344118441,
  "load_duration": 4709881121
}
# on rag1
ollama ps

Expected (excerpt):

bge-m3:latest              790764642607    1.6 GB    100% CPU     8192       llamacpp    4 minutes from now

What you see: now the whole text is classified – 3722 tokens. But it takes 60 seconds, and the loaded model needs 1.6 instead of 1.2 GB. For comparison: 30 repetitions (1862 tokens) went through without any option.

Remember: Cards that are too long are silently shortened. truncate: false belongs in the pipeline – and the real solution is to split documents into short cards (Day 6), not to tear the window open.

Save your progress? Sign up for free

Diagnosis task

A colleague reports: “With our long operations manuals, the search only finds answers from the first pages.” You have ten minutes. What is the cause, and how do you prove it?

Solution: Classify one of the long documents and read prompt_eval_count from the response (Drill 5, step 2). If it says exactly 2048, the rest was cut off – the vector describes only the beginning. To confirm, send the same request with truncate: false: if HTTP 400 or 413 comes back with “the input length exceeds the context length”, it is proven. The fix: split documents into cards that stay well below the limit, and set truncate: false so that an error becomes loud instead of silently disappearing.

Challenge

Write three questions of your own about the four cards, one of them in English and one without any words in common with the correct card. Measure both models. Does bge-m3 hit all three, and how large is the margin to the second-best card in each case?

Save your progress? Sign up for free

Checkpoint

# on rag1
jq '.embeddings[0] | length' ~/kurs/02/karten-nomic.json

Expected:

768
# on rag1
jq '.prompt_eval_count' ~/kurs/02/lang.json

Expected:

2048

Test yourself

  1. Why can you not compare the similarity values of nomic and bge-m3 directly?
  2. How did you recognise nomic’s miss?
  3. What are search_query: and search_document: for?
  4. What do 1024 dimensions instead of 768 cost with a million cards?
  5. How do you notice that Ollama has shortened a text?

Answers:

  1. Every model has its own background noise: 0.58 was noise with nomic; with bge-m3 the noise is around 0.4.
  2. Only because the correct card was known: the leave card had the highest value at 0.63 – a normal-looking number.
  3. nomic was trained with these prefixes; without them, question and document match less well. They are part of using the model correctly.
  4. 4 GB instead of 3 GB just for the numbers (4 bytes per number), without index and metadata.
  5. prompt_eval_count is exactly at the limit (here 2048), or – with truncate: false – the request fails with HTTP 400/413.

Interview note

“How do you choose an embedding model?” – With a small set of your own questions whose correct documents are known; you measure hits and margin, not the position on a leaderboard. Add language, context length, dimension (memory), run time and licence. And mention that switching later means a complete re-indexing.

Enterprise note

In companies, the embedding model is treated like a database schema: versioned, documented and only changed with a migration. Every stored vector comes with the name and version of the model that produced it. Whoever does not store that can no longer tell after a model change which vectors belong together.

Summary

nomic-embed-text is faster (0.10 instead of 0.16 s per question) and smaller (389 MB instead of 1.2 GB), but delivers 768 instead of 1024 numbers, needs more tokens for German texts and got the paraphrased question wrong – even with the prescribed prefixes. For German cards, bge-m3 remains the course’s classification system. You also saw that Ollama silently shortens texts over 2048 tokens; truncate: false makes this visible, num_ctx and num_batch raise the limit – at the price of 60 s and 400 MB more memory.

Commands at a glance

Command What it does Parameters
jq '.model = "…"' alt.json > neu.json replace a field –
jq '.input \|= map("präfix " + .)' … prepend something to every list element \|= changes in place
jq '. + {truncate: false}' … add a field –
jq -n --rawfile k datei '{… input: ($k * 60)}' multiply a text * n repeats a string
jq -c … '… add * 1000 \| round / 1000' round to three places, on one line -c compact output
curl -s -w '\nHTTP %{http_code}\n' … show the HTTP status -w format after the request
ollama ps loaded models with memory and context –
ollama show <model> properties of a model –

Contents

More