Skip to content

Courses / RAG Systems / Module 4

Module 4: Choosing embedding models: benchmarks, multilingual and Indic, fine-tuning embeddings

10 h · theory 3 · hands-on 4 · project 2 · revision 1

You will be able to

  • Read MTEB results critically for your domain
  • Compare open and hosted embedding models on your golden set
  • Handle Hindi and other Indian languages
  • Fine-tune an embedding model on in-domain pairs

Capstone increment: Embedding model chosen by measured recall, with a fine-tuned variant behind a flag.

Job-description skills: Embedding models · Lab tools: sentence-transformers, FlagEmbedding

1. What an embedding model learns

A retrieval embedding model is a bi-encoder: it maps the query and each passage to vectors separately, trained with contrastive learning so that a query lands near the passages that answer it and away from those that don’t. Because passages are encoded independently, you can embed millions in advance and compare with fast vector search.

Details matter in practice. Many models expect prefixes or instructions (E5 uses “query: ” and “passage: ”; some BGE versions use an instruction for queries). Use the similarity the model was trained with, usually cosine on normalised vectors. Check the maximum input length, the dimension and the licence before anything else.

ShortlistMTEB, licence, sizeYour golden setrecall@10, nDCGPer languageEnglish, Hindi, …Cost + latencydims, GPU, API pricePick + recordADR with numbersMine pairsquery ↔ right chunkHard negativesclose but wrongFine-tuneMNRL lossRe-scoreheld-out questionsDecide with your data, not the leaderboardIf the best model is still short of your bargap remainsif better
A model that tops MTEB can lose on your documents. Measure the shortlist on your own questions, in every language your users write in.

2. Read MTEB critically

The Massive Text Embedding Benchmark (MTEB) and its multilingual extension rank models across many tasks. For RAG, look at the retrieval tasks, not the overall average, and prefer tasks that resemble your domain. Leaderboard positions shift often, some models have seen benchmark-like data in training, and small score gaps rarely survive on your own documents.

Use MTEB to build a shortlist of three to five models that meet your constraints (licence, size, languages, maximum length, hosted or self-hosted), then decide on your golden set. Record recall@10 and nDCG@10 for each, along with latency and cost per million tokens.

Common misconception: “The top MTEB model is the best for us.” It is the best on average over public benchmarks. Your support tickets, contracts or circulars are not the average.

3. Hindi and other Indian languages

If your users write in Hindi, Tamil, Bengali or Hinglish, evaluate per language. Multilingual models such as BGE-M3 and multilingual-e5 map many languages into one space so a Hindi question can retrieve an English passage, but quality varies a lot by language and by script. Romanised Hindi (“chutti kitne din milti hai”) and code-mixed text are harder still.

Build a golden set for each language your users actually use, including transliterated queries. Options when one language lags: translate queries to the corpus language (IndicTrans2 from AI4Bharat is a strong open model), index translated copies of key documents, or fine-tune on in-language pairs. Indic scripts also take more tokens per word with many tokenizers, which raises embedding and generation cost.

In production: Report retrieval metrics per language on your dashboard. An average across languages can look healthy while Tamil users get poor answers.

4. Fine-tune when the gap is real

Fine-tune an embedding model when your domain has vocabulary the base model doesn’t understand (product codes, legal terms, internal acronyms) and the best off-the-shelf model still misses your bar. You need query–passage pairs: mine them from search logs and click data, or generate synthetic questions for each chunk with an LLM and filter them.

Train with in-batch negatives (MultipleNegativesRankingLoss in sentence-transformers) and add hard negatives: passages that look relevant but aren’t, often mined with the base model itself. Evaluate on held-out questions that were never used to generate training pairs. Expect real gains on narrow domains; expect little on general text.

Tip: Matryoshka training lets you truncate embeddings to fewer dimensions with a small quality loss. It can cut storage and search cost by 4× or more; measure the loss on your data.

Deep dive

Deep dive: hard negatives and the training loss

When you fine-tune an embedding model on your own (question, passage) pairs, the loss most libraries use is MultipleNegativesRankingLoss: InfoNCE with in-batch negatives, where every other passage in the batch counts as wrong. It is cheap, but random negatives are easy, so the model can reach a low loss and still confuse look-alike passages.

Hard-negative mining fixes this. For each training question, retrieve the top results with the current model, drop the true passage (and anything labelled relevant), and add one or two of the remaining high-ranked passages as explicit negatives. Watch for false negatives: a mined “negative” that actually answers the question teaches the model the wrong thing, so filter mined negatives with a cross-encoder score or a margin.

  1. Collect 1,000–10,000 real (question, passage) pairs from logs, or generate questions for each chunk with an LLM and spot-check them.
  2. Hold out 10–20% as a retrieval eval set before you train anything.
  3. Mine hard negatives with the base model: results ranked 5–30 that aren’t labelled relevant.
  4. Train on (question, positive, hard negative) triplets with MultipleNegativesRankingLoss, with large batches (more in-batch negatives), for 1–3 epochs.
  5. Compare recall@10 and nDCG@10 with the base model on the held-out set, and keep the fine-tuned model only if the gain is real.
How an embedding model learnsFive support questions and their passages. Training pulls each question towards its own passage and pushes it away from the other passages in the same batch.
Negatives in each batch:

Loss on training batches 0.001 · loss over all passages 0.549 · questions answered with the right passage 3 / 5

QuestionTop passageMargin over its look-alike
refund for my laptopLaptops can be refunded within 14 days ✓0.05 vs “Clothing refunds: 30 d…”
refund for a shirtLaptops can be refunded within 14 days ✗-0.03 vs “Laptops can be refunde…”
laptop delivery is lateClothing orders delayed: what to do ✗-0.00 vs “Clothing orders delaye…”
shirt hasn’t arrived yetClothing orders delayed: what to do ✓0.01 vs “Late laptop deliveries…”
reset my passwordReset your account password ✓no look-alike

Similarity grid (rows are questions, columns are passages; darker is more similar):

Guided experiments
  1. Random negatives, 100 steps. The training loss is almost zero, so training “worked”. How many questions get the right passage, and what are the margins? Why can a model ace its training batches and still confuse laptops with shirts?
  2. Switch to random + hard negatives and replay from 0 to 100 steps. Which column of the grid separates? This is why embedding fine-tuning recipes mine hard negatives (look-alike passages that are wrong).
  3. With hard negatives, try τ = 1. The loss stays high: the softmax is too soft to push the look-alikes apart. Compare τ = 0.05 and 0.1. Most models train with τ around 0.01–0.1.
  4. Your own domain will have look-alikes too (two product variants, two policy versions). How would you find hard negatives for them automatically? (Hint: retrieve with the current model and keep the high-ranked wrong passages.)
Common misconception: A lower training loss is not evidence that the model got better at your retrieval task. Only held-out recall@k is.

5. Dimensions, cost and changing models

Storage is chunks × dimensions × bytes per value, plus index overhead. Two million chunks at 1,024 float32 dimensions is about 8 GB of raw vectors before the HNSW graph. Quantization (Module 5) and Matryoshka truncation reduce this; hosted APIs charge per token embedded.

Changing the embedding model means re-embedding every chunk, because vectors from different models don’t share a space. Store the model name and version with the index, build the new index alongside the old one, compare on the golden set, then switch with an alias (Module 16).

Deep dive

Deep dive: Matryoshka dimensions and quantized vectors

Matryoshka models are trained with the loss applied at several prefix lengths at once, so the first 64, 128, 256 … dimensions are each a usable embedding. You can store shorter vectors and lose a little quality instead of all of it.

Quantizing vectors is a separate choice from truncating them. Int8 (scalar) quantization keeps every dimension at lower precision; binary quantization keeps only the sign of each dimension. Both usually need a re-scoring step with fuller vectors to recover quality.

Worked example: storage for 1 million chunks

1,024 dims × 4 bytes (float32)            = 4.1 GB
Matryoshka, truncated to 256 dims         = 1.0 GB
int8, all 1,024 dims (1 byte each)        = 1.0 GB
binary, 1,024 bits = 128 bytes per vector = 0.13 GB (32× smaller)
common recipe: search the binary vectors for the top 100, re-score them with int8 or float32

Hands-on: coding window

The coding window and the full VS Code lab are available after you sign in.

Sign in to code

Labs

Lab 4.1: Model shoot-out

Score three embedding models (for example bge-small-en-v1.5, multilingual-e5-base and one hosted API model) on the capstone golden set, plus a 15-question Hindi or Hinglish set.

Expected: A table of recall@10, nDCG@10, latency and cost per model and language.

Lab 4.2: Fine-tune with hard negatives

Generate 1,000 synthetic question–chunk pairs with an LLM, mine one hard negative per pair with the base model, and fine-tune bge-small with sentence-transformers on a free Colab GPU.

Expected: Held-out recall@10 before and after, and the training time.

Lab 4.3: Model behind a flag (capstone increment)

Make the embedding model a config value stored with the index metadata, index with your chosen model, and add the fine-tuned model behind a flag with its own index.

Expected: Switching the flag queries the other index; docs/adr/0003-embeddings.md records the numbers.

Go deeper

Videos

Further reading

GitHub repositories

  • embeddings-benchmark/mteb: How retrieval tasks are defined; run one MTEB retrieval task on a model you are considering.
  • huggingface/sentence-transformers: examples/sentence_transformer/training: MultipleNegativesRankingLoss, hard-negative mining and Matryoshka loss.
  • FlagOpen/FlagEmbedding: BGE-M3: dense, sparse and multi-vector outputs from one multilingual model, and its fine-tuning scripts.
  • AI4Bharat/IndicTrans2: Translation between English and 22 Indian languages, useful for query translation when a language lags.

Checkpoint quiz

1. Why can a bi-encoder search millions of passages quickly?
2. How should you use MTEB?
3. A model scores well on English but poorly on Hinglish queries. Sensible next step?
4. What are hard negatives?
5. You switch embedding models. What must happen?

Interview questions

  1. How do you choose an embedding model for a new RAG system?
  2. How would you support Hindi and Hinglish queries over English documents?
  3. When is fine-tuning an embedding model worth it, and how do you build the data?
  4. How do you migrate a production index to a new embedding model with no downtime?
  5. What is Matryoshka representation learning and when would you use it?

Ask the professor

Sign in to ask questions about this module.

Discussion

Community discussion appears here once giscus is configured (see README).