Lesson 3 of 3 Optimizing Retrieval Performance

Selecting and Fine-Tuning Embedding Models

This lesson explains how embedding models differ, how they are evaluated on your own data, how an open embedding model is fine-tuned, and what changing models requires.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify the properties that distinguish embedding models.
  • Describe retrieval metrics for comparing models.
  • Explain how an embedding model is fine-tuned.
  • Explain why a new model requires re-embedding.

How embedding models differ

An embedding model maps text to a vector so that texts with similar meaning are close together. Models differ in their training data, so in which domains and languages they place related texts close together, and in practical properties.

PropertyWhy it matters
Retrieval qualityHow often relevant passages rank highly for your queries
DimensionsStorage and memory per vector and search speed; more is not always better
Maximum input tokensChunks longer than the limit are truncated
LanguagesMultilingual corpora need models trained across languages
Cost and latencyEmbedding the whole corpus and every query has a price
Deployment optionsServerless deployments, managed compute for open models, region availability

Examples in Foundry include text-embedding-3-small (1,536 dimensions) and text-embedding-3-large (3,072 dimensions), which accept a dimensions parameter that shortens vectors to save storage at a modest cost in quality, and partner and open models such as multilingual embedding models from the model catalog.

Evaluating embedding models

Public benchmarks such as the Massive Text Embedding Benchmark (MTEB) give a shortlist, but domain language, such as part numbers, legal phrases or clinical terms, can change the ranking. The decision rests on a retrieval evaluation with your data: a set of real or realistic queries, each labelled with the passages that answer it.

  • Recall at k: the share of queries for which a relevant passage appears in the top k results.
  • Mean reciprocal rank (MRR): how high the first relevant passage ranks on average.
  • Normalized discounted cumulative gain (NDCG): ranking quality that rewards relevant passages near the top, including graded relevance.

Each candidate model embeds the same chunks and queries, and the metrics are compared together with cost, latency and storage. The comparison should include hybrid search, because a weaker embedding model combined with keyword search can beat a stronger one alone.

Fine-tuning an embedding model

When general models consistently miss domain relationships, an open embedding model can be fine-tuned on pairs of queries and relevant passages from the domain. A contrastive loss, such as multiple negatives ranking loss in the sentence-transformers library, pulls each query towards its passage and away from other passages in the batch. Hard negatives, passages that look similar but do not answer the query, make training more effective.

from sentence_transformers import SentenceTransformer, losses
from sentence_transformers.trainer import SentenceTransformerTrainer
from datasets import load_dataset

model = SentenceTransformer("BAAI/bge-base-en-v1.5")
pairs = load_dataset("json", data_files="train_pairs.jsonl")["train"]   # columns: anchor, positive
trainer = SentenceTransformerTrainer(model=model, train_dataset=pairs,
                                     loss=losses.MultipleNegativesRankingLoss(model))
trainer.train()
model.save_pretrained(args.output)   # an Azure Machine Learning job output

The training runs as an Azure Machine Learning command job on GPU compute, with a held-out set of query and passage pairs for evaluation with recall and NDCG. The resulting model is registered and deployed to a managed online endpoint or Foundry managed compute, and used by the ingestion pipeline and at query time. Training pairs can come from search logs, from experts, or from synthetic generation reviewed by experts.

Changing embedding models

Vectors from different embedding models, or from the same model with different dimensions, are not comparable. Changing the model therefore means re-embedding the entire corpus and building a new vector index, and query embeddings must switch at exactly the same time.

  1. Create a new index

    With vector fields sized for the new model's dimensions.

  2. Re-embed the corpus

    Run ingestion with the new model into the new index.

  3. Evaluate

    Compare retrieval and answer metrics against the current index.

  4. Switch

    Point the application, and its query embedding, at the new index, for example through an index alias or configuration.

  5. Retire

    Keep the old index until the switch is proven, then delete it.

Worked example

Domain vocabulary

A pharmaceutical company's RAG assistant uses a general embedding model. Recall at 5 is 61 percent on an expert-labelled set of 800 questions, mostly because questions use brand names while documents use generic compound names. Hybrid search improved recall only slightly. The company has 12,000 expert-validated question and passage pairs.

The model does not place brand and generic names close together, which is a domain relationship that fine-tuning can teach. Fine-tune an open embedding model on the 12,000 pairs, holding out the 800-question set for evaluation, measure recall at k and NDCG against the current model, and if it is better, re-embed the corpus into a new index and switch over.

A larger general embedding model may help a little but has the same blind spot. Adding a synonym map for every brand name could help keyword search, but vector search would still miss the relationship. Fine-tuning the chat model would not change which passages are retrieved.

Check yourself

Why can a team not switch to a new embedding model for queries while keeping the existing document vectors?

Show answer

Vectors from different models are not comparable. The corpus must be re-embedded with the new model into a new index, and queries switched at the same time.

Exam Tips

Remember for the exam

  • Compare embedding models on your own labelled queries with recall at k, MRR and NDCG.
  • text-embedding-3 models accept a dimensions parameter that trades quality for storage.
  • Fine-tune open embedding models on query and passage pairs with a contrastive loss, as an Azure Machine Learning job.
  • A new embedding model requires re-embedding the corpus into a new index and switching queries at the same time.