RAG: Fine-tuning an embedding model to improve search accuracy

Semyon Saprin5 Jun 2025

Problem

PGK Digital's data team is building an AI assistant over legal documents. The general-purpose embedding model it used (deepvk/USER-bge-m3) did not separate neighbouring sections well, so the retriever sometimes passed the wrong context to the language model. Only about 100 expert-labelled question-chunk pairs existed, and GPU memory was limited.

Example from the article: for "What ways are there to agree on a contract price?", the right chunk is "1.2 Ways to agree on a contract price", while "1.3 Rules for changing the contract price" is a near miss the base model can rank too high.

Solution in brief

The team fine-tuned the embedder on triplets (question, relevant chunk, hard negative) with a triplet margin loss, following the NV-Retriever approach. An LLM generated and filtered the questions, three teacher embedding models mined the hard negatives, and LoRA kept training on one A100. On the expert test pairs, Recall@5 rose from 0.675 to 0.794.

Questions this case answers
  • How to adapt a pretrained text embedding model to a narrow domain when only about a hundred labelled query-passage pairs and one mid-range GPU are available
  • How to build hard-negative training triplets for a retriever from an unlabelled corpus using synthetic queries and teacher embedding models
  • How to improve retrieval accuracy in a RAG system over narrow-domain documents when a general embedding model confuses neighbouring sections?
  • How to build an embedding training set from about 100 expert-labelled pairs and an unlabelled document corpus?

Tools and methods

  • Synthetic questions plus LLM filtering — build anchor-positive pairs from unlabelled chunks.
  • Hard negative mining with teacher models — picks chunks that look relevant but are not.
  • Triplet margin loss with LoRA — trains about 1.9% of parameters of BGE-M3 (deepvk/USER-bge-m3).
  • multilingual-E5, ruBERT, LaBSE — teachers for negative mining.
  • Qwen2.5-14B, LlamaIndex — question generation.
Continue reading — it's free

Google or a one-time email code. No password.