This repository contains recipes and examples for fine-tuning embedding models using HuggingFace libraries. The focus is on creating high-quality models for semantic search and RAG applications.
.
├── embedding-models/ # Fine-tuning recipes for embedding models
│ ├── datasets/ # Dataset creation and processing scripts
│ ├── models/ # Model training and evaluation scripts
│ └── notebooks/ # Jupyter notebooks for experimentation
│
├── requirements/ # Python dependencies
│ └── embedding.txt # Dependencies for embedding models
│
└── README.md # This file
- Create a virtual environment and activate it:
python3 -m venv .venv
source .venv/bin/activate- Install the required dependencies:
pip install -r requirements/embedding.txt- Follow the instructions below to fine-tune your embedding model
- Dataset creation for embedding model fine-tuning
- Support for both local CSV files and Hugging Face datasets
- Evaluation using NDCG and other metrics
- Support for multiple embedding models (E5, BGE, etc.)
- Flexible column name configuration for different dataset formats
The scripts support two types of datasets:
-
Triplet Format: Contains three columns in order: anchor, positive, negative
- Default column names:
anchor,positive,negative - Example:
anchor,positive,negative "What is Python?","Python is a programming language...","Java is a programming language..."
- Default column names:
-
Pair Format: Contains two columns in order: anchor, positive
- Default column names:
anchor,positive - Example:
anchor,positive "What is Python?","Python is a programming language..."
- Default column names:
You can specify custom column names using the --triplet_columns or --pair_columns arguments.
# First, create the synthetic dataset
python embedding-models/datasets/create_synthetic_dataset.py \
--num_samples 1000 \
--output_path embedding-models/datasets/embedding_synthetic_training_dataset.csv
# Then train the model using the created dataset
python embedding-models/models/train.py \
--model sentence-transformers/all-MiniLM-L6-v2 \
--dataset_source embedding-models/datasets/embedding_synthetic_training_dataset.csv \
--output_dir models/all-MiniLM-L6-v2 \
--batch_size 32 \
--epochs 3 \
--learning_rate 2e-5 \
--warmup_steps 100 \
--evaluation_steps 100 \
--use_wandb \
--wandb_project embedding-fine-tuning# For triplets with custom column names
python embedding-models/models/train.py \
--model sentence-transformers/all-MiniLM-L6-v2 \
--dataset_source embedding-models/datasets/embedding_synthetic_training_dataset.csv \
--output_dir models/all-MiniLM-L6-v2 \
--triplet_columns "question,relevant_answer,irrelevant_answer" \
--use_wandb
# For pairs with custom column names
python embedding-models/models/train.py \
--model sentence-transformers/all-MiniLM-L6-v2 \
--dataset_source embedding-models/datasets/embedding_synthetic_training_dataset.csv \
--output_dir models/all-MiniLM-L6-v2 \
--pair_columns "text1,text2" \
--use_wandbpython embedding-models/models/train.py \
--model sentence-transformers/all-MiniLM-L6-v2 \
--dataset_source sentence-transformers/quora-duplicates:triplet \
--output_dir models/all-MiniLM-L6-v2 \
--batch_size 32 \
--epochs 3 \
--learning_rate 2e-5 \
--warmup_steps 100 \
--evaluation_steps 100 \
--use_wandb \
--wandb_project embedding-fine-tuningNote: For Hugging Face datasets that require a configuration (like Quora duplicates), specify the config using the format dataset_name:config_name. For example:
sentence-transformers/quora-duplicates:tripletfor triplet formatsentence-transformers/quora-duplicates:pairfor pair formatsentence-transformers/quora-duplicates:pair-classfor pair classification format
# Using a local CSV file with default column names
python embedding-models/models/evaluate.py \
--model models/all-MiniLM-L6-v2 \
--dataset_source embedding-models/datasets/embedding_synthetic_test_dataset.csv \
--batch_size 32
# Using a local CSV file with custom column names
python embedding-models/models/evaluate.py \
--model models/all-MiniLM-L6-v2 \
--dataset_source embedding-models/datasets/embedding_synthetic_test_dataset.csv \
--triplet_columns "question,relevant_answer,irrelevant_answer" \
--batch_size 32
# Using a Hugging Face dataset
python embedding-models/models/evaluate.py \
--model models/all-MiniLM-L6-v2 \
--dataset_source sentence-transformers/quora-duplicates:triplet \
--batch_size 32# 1. Train the model using Quora duplicates dataset
python embedding-models/models/train.py \
--model sentence-transformers/all-MiniLM-L6-v2 \
--dataset_source sentence-transformers/quora-duplicates:triplet \
--output_dir models/all-MiniLM-L6-v2 \
--batch_size 32 \
--epochs 3 \
--use_wandb
# 2. Evaluate on test set
python embedding-models/models/evaluate.py \
--model models/all-MiniLM-L6-v2 \
--dataset_source sentence-transformers/quora-duplicates:triplet \
--batch_size 32The scripts will:
- Load the dataset from either a local CSV file or Hugging Face (automatically detected)
- Train a model using the training dataset with the specified parameters
- Evaluate the model's performance on the test set using NDCG and other metrics
The training script integrates with Weights & Biases for experiment tracking. To use it:
-
Install Weights & Biases:
pip install wandb
-
Login to Weights & Biases:
wandb login
-
Run the training script with
--use_wandbflag:python embedding-models/models/train.py --use_wandb ...
The script will log:
- Training configuration
- Training loss
- Evaluation metrics
- Model checkpoints