Train an adapter for any embedding model in <1min
Project description
weightgain
Fine-tune any embedding model in under a minute. Even models from OpenAI, Cohere, Voyage, etc.
It works by training an adapter that sits on top of the model, instead of modifying the model itself. This adapter transforms your embeddings after they're generated to boost retrieval accuracy and overall RAG performance.
Weightgain lets you train an adapter in just a couple lines of code, even if you don't have a dataset.
Installation
> pip install weightgain
Quickstart
from weightgain import Dataset, Adapter
# Generate a dataset (or supply your own)
dataset = Dataset.from_synthetic_chunks(
prompt="Chunks of code from an arbitrary Python codebase.",
llm="openai/gpt-4o-mini",
)
# Train the adapter
adapter = Adapter("openai/text-embedding-3-large")
adapter.fit(dataset)
# Apply the adapter
new_embeddings = adapter.transform(old_embeddings)
Usage
Choosing an Embedding Model
Weightgain wraps LiteLLM to provide access to models. You can fine-tune models from OpenAI, Cohere, Voyage, and more. Here's the full list of supported models.
Building the Dataset
You need a dataset of [query, chunk] pairs to get started. A chunk is a retrieval result, e.g. a code snippet or excerpt from a document. You can either generate a synthetic dataset or supply your own.
If you already have chunks:
from weightgain import Dataset
chunks = [...] # list of strings
dataset = Dataset.from_chunks(
chunks,
llm="openai/gpt-4o-mini",
n_queries_per_chunk=1
)
This will use gpt-4o-mini (or whatever LiteLLM model you want) to generate 1 query per chunk.
If you don't have chunks:
dataset = Dataset.from_synthetic_chunks(
prompt="Chunks of code from an arbitrary Python codebase.",
llm="openai/gpt-4o-mini",
n_chunks=25,
n_queries_per_chunk=1
)
This will generate chunks using the prompt, and then generate 1 query per chunk.
If you have queries and chunks:
qa_pairs = [...] # list of (str, str) tuples
dataset = Dataset.from_pairs(qa_pairs, model)
Training the Adapter
from weightgain import Adapter
adapter = Adapter.fit(
dataset,
batch_size=25,
max_epochs=50,
learning_rate=100.0,
dropout=0.0
)
After training, you can generate a report with various plots (training loss, cosine similarity distributions before/after training, etc.):
adapter.show_report()
Using the Adapter
old_embeddings = [...] # list of vectors
new_embeddings = adapter.transform(old_embeddings)
Behind the scenes, an adapter is just a matrix of weights that you can multiply your embeddings with. You can access this matrix like so:
adapter.matrix # returns numpy.ndarray
Roadmap
- Add option to train an MLP instead of a linear layer
- Add a method for easy hyperparameter search
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file weightgain-1.0.0.tar.gz.
File metadata
- Download URL: weightgain-1.0.0.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf6897db9b10c30e7164f613f2eb0eba92f11e0cadc233a1d60bce67ec9cd558
|
|
| MD5 |
edf05358cb776881e90814c074468f1c
|
|
| BLAKE2b-256 |
cfa538510d49b690c6c43bcf7c972b7facd2020554d801421773b0b93169d256
|
File details
Details for the file weightgain-1.0.0-py3-none-any.whl.
File metadata
- Download URL: weightgain-1.0.0-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8d8eaed2b7c729e2572490c0047ddba807b0f3eb0f2d8e588633390c4a8f61d
|
|
| MD5 |
feb93ad0c38bdcbcd6cfa06f1d6144ce
|
|
| BLAKE2b-256 |
cfe9afe6f2331c3a964ae829d355d20de44d02b1ac146f2315287a4526b4bcde
|