# How Do You Fine-Tune Embedding Models?

> How to train an embedding model on your own labelled pairs, test it fairly and re-index safely, explained simply for your AI interview.

**In short:** You fine-tune an embedding model by training it on labelled pairs: text that should match, and near misses that should not. Then you test it on searches it never saw. If you change the model's own weights, re-embed your whole index, because old and new vectors can't be compared.

AI and LLM interview question Q301, Fine-tuning and adaptation. Advanced, occasional (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/how-do-you-fine-tune-embedding-models

## Say it in 30 seconds

I fine-tune an embedding model on pairs of text that should match, like a claims search and the note the adjuster opened, plus close look-alikes that should not. Training pulls matches together and pushes the rest apart. I test on searches the model never saw, and check the new model beats the old one. If I retrain the model's own weights, I re-embed the whole index, because old and new vectors don't line up.

## Say it in 2 minutes

I start with pairs. I take real searches from our logs, and the note the adjuster opened in the end is the right answer. Each one is a positive pair. Before I add anything else, I set aside about one search in ten as a test set, so its text never reaches training. Then I add hard negatives: notes that look close, like another water claim from the same policy type, but are wrong. Without them, the model learns only the easy difference between a kitchen leak and a car crash.

Next I train on the rest. The model moves matching pairs closer together and pushes the wrong ones apart. I keep each learning step small (the learning rate), so it learns our claim words without losing general language. A leaked test set makes the score look better than it is, so the held-out searches stay out of training.

Then I test. The old model and the new one answer the same held-out searches, and I count how often the right note lands in the top three. In our made-up example, that rose from 61 percent to 79 percent. That held-out score is what decides the release.

Last, the step people forget: the new model's vectors don't match the old ones. So I re-embed the whole index before switching, and keep the old index until the new one wins in a small live trial. If the team has no labelled pairs, I first generate some, and I say that the data is the weak point.

## What is fine-tuning an embedding model, in plain words?

An embedding model turns a piece of text into a list of numbers, called a vector. Texts with a similar meaning get lists that sit close together. Fine-tuning means training that existing model a bit more on your own examples, so it learns what "similar" means in your work.

Think of a new claims handler at an insurer. The handbook never says that "burst pipe" and "water leak in the kitchen" are the same kind of claim. After a few months of real cases, she knows it. A general embedding model may still put those two phrases far apart. A fine-tuned one learns the link from your own cases.

- **Embedding:** A list of numbers that stands for the meaning of a piece of text.
- **Positive pair:** Two texts that should match, like a search and the note that solved it.
- **Hard negative:** A near miss that looks like a match but is wrong. It teaches the model the fine lines.
- **Held-out set:** Examples kept out of training and used only to test the model.
- **Re-embed:** Recalculate every stored vector with the new model.

## How do you build the training pairs?

The training data decides most of the result. Google's guide to tuning text embeddings says supervised tuning uses labelled examples that show the output you want:

> "Supervised tuning uses labeled examples that demonstrate the type of output you'd like from your text embedding model during inference." (Google Cloud, tuning text embeddings)
>
> "using "entailment" pairs as positives and "contradiction" pairs as hard negatives" (SimCSE paper, 2021)

A positive pair is a search and the note that solved it. A hard negative is a near miss from the same family of documents. SimCSE, a 2021 paper on sentence embeddings, used the same idea with pairs of sentences where one follows from the other or contradicts it: an entailment pair (a sentence and another that follows from it) became the positive, and the contradiction pair became the hard negative. A 2020 paper on dense passage retrieval found that a small set of questions and passages can be enough to train the embeddings.

Hard negatives matter because the easy ones teach little. The ANCE paper of 2020 found that the negatives used in training were not like the documents a search meets later. Its fix was to pick negatives from your own corpus, using a search index that finds the closest documents to each query, so the near misses are the ones the model will meet.

Labelled pairs are often scarce. The GPL paper of 2021 said that search systems built on embeddings (called dense retrievers) "require large amounts of training data which is not available for most domains". Its method "combines a query generator with pseudo labeling from a cross-encoder". So if you have no labels, you can generate queries and let a cross-encoder label the pairs, but treat those machine labels as a weaker source.

> **Tip:** Watch for false negatives. A hard negative can be a right answer that nobody labelled, so check a sample of pairs by hand before training.

1. **Collect real pairs:** Take searches and the notes people opened to solve them. Keep only clear matches.
2. **Split before anything else:** Set aside about one search in ten as a test set. Its text must never reach training.
3. **Add hard negatives:** For each match, add a near miss from the same kind of document, and check a sample by hand.
4. **Train gently:** Take small learning steps (the learning rate), so the model learns your words without losing general language.
5. **Compare on the test set:** Score the old and the new model on the same held-out searches.
6. **Re-embed and switch:** Rebuild every stored vector with the new model, then move traffic over in steps.

Here is a made-up example. A mid-size insurer logged 8,000 searches where an adjuster opened the note that solved the claim. It set aside 800 as a test set. Each of the other 7,200 became a positive pair, and each got a hard negative from the same policy type. That gave about 14,400 training pairs.

*Diagram, One fine-tuning run, from pairs to release: Matching pairs and near misses feed one training run. The new model then answers searches it never saw, next to the old one. The score decides whether it goes live.*
1. The right note goes in as a match.
2. The near miss goes in as a wrong answer to push away.
3. The new model is then run on searches it never trained on.
4. Both models answer the same searches, and the scores are compared.
5. The held-out score decides whether the new model goes live.

## Should you retrain the whole model or add a small layer?

There are two common ways. A full fine-tune changes the model's own weights. An adapter keeps the model frozen and learns a small add-on that reshapes its output. Both start from the same pairs.

- **Full fine-tune: Change the model.** Can fit your language best, but needs GPUs (graphics chips built for training) and a careful test. You also decide how the tuned model is served.
- **Small adapter: Add a layer.** Cheaper and faster, and the base model stays as it was. It may be weaker when your words are far from general text.

> "The output is a matrix that you can use to multiply your embeddings." (OpenAI cookbook (archived))

OpenAI's cookbook shows the adapter idea in an old recipe, dated March 2022. It says: "This recipe is archived and may reference outdated models or APIs." The recipe trains a matrix that multiplies your vectors, and its authors saw gains "as little as ~100 training examples" on other data sets. Treat those numbers as old.

Google's tuning guide is clear on cost: "Tuning requires GPU accelerators." Its list of tunable models names three text embedding models: text-embedding-004, text-embedding-005 and text-multilingual-embedding-002. Check that list for your model before you plan. It also says that "Unlike text model tuning jobs, a text embedding tuning job doesn't deploy your tuned models" to an endpoint. So plan who runs the new model before you book the job.

> **Tip:** If someone asks which to pick, start with the adapter because it is cheap to test. Move to a full fine-tune only if the held-out scores say so.

## How do you know the new model is better?

Test on searches the model never trained on, and run the same searches through both models. Count the score that matters to users. For a claims search, that is the hit rate: how often the right note appears in the top three results.

OpenAI's archived cookbook recipe makes the order clear: "it's important to split data into training and test sets before generating synthetic negatives or positives." Split first, then build the negatives, so no test text leaks into training.

In the made-up insurer example, the old model put the right note in the top three for 61% of the 800 held-out searches. The new model did it for 79%. Those are the numbers that decide the release.

Here is the same count in plain Python, on ten made-up searches. Each rank is where the right note landed, with 1 as the top result:

```python
# Made-up ranks: where the right claim note landed for ten held-out searches.
# 1 is the top result, 0 means not in the top 5. A hit is a rank of 1 to 3.
OLD_RANK = [2, 1, 0, 1, 0, 0, 0, 3, 1, 1]
NEW_RANK = [1, 1, 2, 1, 3, 0, 1, 3, 2, 1]

def hit_rate(ranks):
    hits = [r for r in ranks if 1 <= r <= 3]
    return len(hits) / len(ranks)

def show(name, ranks):
    print(f"{name}: hit rate at 3 = {hit_rate(ranks):.0%} on {len(ranks)} searches")

show("Old model", OLD_RANK)
show("New model", NEW_RANK)
```

Output:

```text
Old model: hit rate at 3 = 60% on 10 searches
New model: hit rate at 3 = 90% on 10 searches
```

Ten searches are far too few to trust, so the real test is the 800-search set above.

> **Tip:** Check the held-out set once, at the end. If you keep tuning until the test score looks good, you have trained on the test set without saying so.

Also check general searches, such as a question that has nothing to do with claims. A model tuned hard on claims can get worse on everything else, so keep a small set of general searches in the test.

## Why must you re-embed the whole index?

Each model draws its own map of meaning. After a full fine-tune, a vector from the old model sits at a place the new model would never put it. So comparing a new query with old vectors gives scores that look like results but can't be trusted. An adapter that leaves the base model alone may not need that rebuild, but check how the layer is applied to stored vectors before you skip it.

*Diagram, Two ways to switch to a new embedding model: Two ways to switch. Rebuilding every vector first makes the scores comparable. Mixing new queries with old vectors after a full fine-tune gives scores that look like results but can't be trusted.*
1. Every stored note is rebuilt by the new model first.
2. Queries then meet vectors from the same model, so the scores compare.
3. A new query is run against the old vectors.
4. A new query is compared with old vectors, so the scores can't be trusted.

Re-embedding costs time and money, so plan it early. The made-up insurer keeps 2.4 million claim notes in its index. Put that rebuild in the plan before anyone promises a launch date.

### How would you roll out a new embedding model safely?

I would build the new index next to the old one, and leave live search alone while I build it. Then I would move traffic over in steps:

1. **Send a small share:** Send a small share of searches to the new index, such as 5%.
2. **Watch the numbers:** Watch the hit rate and the complaints for two weeks.
3. **Switch all traffic:** Switch all traffic and keep the old index for a week.
4. **Clean up:** Then remove the old index and its vectors.

> **Tip:** Say in advance what would make you roll back: a drop in the hit rate, or adjusters saying the top result is wrong more often.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: an embedding model trained on your own labelled pairs.
- [ ] The data: matching pairs plus hard negatives, with a test set split off first.
- [ ] The judge: held-out searches, run on the old and new model side by side.
- [ ] The re-embed: every stored vector is rebuilt before the switch.
- [ ] Full fine-tune or adapter, and why you picked it.
- [ ] An honest limit: it helps most when your words differ from the base model's.

**Common traps**

- Trap: "Train on everything you have, then test on the same data." Better: Testing on training data flatters the model. Hold out a test set first and never train on it.
- Trap: "Just swap in the new model. The old vectors will still work." Better: After a full fine-tune they won't line up with the new model's. Re-embed the whole index before you switch.
- Trap: "A lower training loss means a better search." Better: Training loss is the model's own error score during training. It measures fit to the training data, not new searches. Judge the model on searches it never saw, with the hit rate.

## Can you spot the bad pair?

**Spot the bad pair:** Your team builds pairs for the claims search. Which label teaches the model the wrong thing?

A. Query: burst pipe, kitchen floor. Note: water damage from a ruptured supply pipe in the kitchen. Label: match.
B. Query: hail on the roof. Note: hail damage to tiles, roof repaired. Label: match.
C. Query: water in the basement after a storm. Note: flooded basement after heavy rain. Label: not a match.
D. Query: car hit from behind. Note: rear-end collision, third-party claim. Label: match.

**Answer:** C. The first, second and fourth pairs are real matches. In the third, the search and the note describe the same event, but the label says not a match, which teaches the model the reverse. Labels come from people or clicks, so check a sample by hand before training.

**Quick check:** Your fine-tuned model scores 18 points higher on the held-out searches (61% to 79%). What do you do before going live?

A. Switch the live search today, the score is higher
B. Re-embed the whole index with the new model, then run a small live trial
C. Retrain on the held-out searches so the score rises more
D. Lower the batch size and train again

**Answer:** B. The score is only part of the job. The stored vectors must match the new model, and a small live trial shows whether the gain holds. Training on the held-out set would spoil the test.

## Can you answer these from memory?

**Flash cards**

- **What is fine-tuning an embedding model?** Training an existing embedding model on your own labelled pairs, so it learns your meaning of similar.
- **What is a hard negative?** A near miss that looks similar but is wrong. It teaches the model the fine lines.
- **Why keep a held-out set?** It tests the model on searches it never trained on, so the score is honest.
- **Full fine-tune or adapter?** Full changes the model's weights. An adapter learns a small add-on and leaves the model alone.
- **Why re-embed everything?** After a full fine-tune, old and new models place meaning differently, so mixed vectors can't be compared.
- **What decides go-live?** The held-out score and a small live trial, not the training loss.

## Likely follow-up questions

- Q147 When must you re-embed everything? https://designervenkat.online/interview/ai/when-must-you-re-embed-everything
- Q116 How do you choose an embedding model? https://designervenkat.online/interview/ai/how-do-you-choose-an-embedding-model
- Q294 How do you prepare a fine-tuning dataset? https://designervenkat.online/interview/ai/how-to-prepare-a-fine-tuning-dataset
- Q297 How do you spot overfitting during fine-tuning? https://designervenkat.online/interview/ai/how-do-you-spot-overfitting-during-fine-tuning
- Q300 How do you evaluate a fine-tuned model? https://designervenkat.online/interview/ai/how-do-you-evaluate-a-fine-tuned-model

## Learn it in depth

- [Indexing Embeddings](https://designervenkat.online/learn/ai-engineering/indexing-embeddings)
- [Choosing an embedding model](https://designervenkat.online/learn/ai-engineering/embedding-models)
