Train an adapter for any embedding model in <1min
Project description
weightgain
Train an adapter for any embedding model in under a minute.
The best embedding models are locked behind an API (OpenAI, Cohere, Voyage, etc.) and can't be fine-tuned. To get around this, you can train an adapter. This is a matrix of weights you can multiply your embeddings by to optimize them for retrieval over your specific data.
weightgain lets you train an adapter in a couple lines of code, even if you don't have a dataset.
Installation
> pip install weightgain
Quickstart
from weightgain import Dataset, Adapter, Model
# Choose an embedding model
model = Model("openai/text-embedding-3-large")
# Generate synthetic data (or supply your own)
dataset = Dataset.from_synthetic_chunks(
prompt="Chunks of code from an arbitrary Python codebase.",
model=model,
llm="openai/gpt-4o-mini",
n_chunks=25,
n_queries_per_chunk=5
)
# Train the adapter
adapter = Adapter.train(dataset)
# Generate a new embedding
embedding = model.get_embedding("Embed this sentence")
new_embedding = adapter @ embedding # matrix multiplication
Usage
Choosing an Embedding Model
Weightgain wraps LiteLLM to get access to model APIs. You can see the full list of supported embedding models here.
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
model = Model("openai/text-embedding-3-large")
dataset = Dataset.from_chunks(
chunks,
model,
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.",
model=model,
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.train(
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
Behind the scenes, the adapter is just a numpy matrix that you can multiply your embeddings with:
embedding = model.get_embedding("Embed this sentence")
new_embedding = adapter @ embedding
You can access this matrix directly too:
adapter.matrix # returns numpy.ndarray
Roadmap
- Add option to train an MLP instead of a linear layer
- Add a method for easy hyperparameter search
- Move the embedding step to
Adapter.traininstead of dataset creation
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-0.5.0.tar.gz.
File metadata
- Download URL: weightgain-0.5.0.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f24dce4a79535fa390c44ffabc69c3c738e2f800149744e3016570a21f6add2a
|
|
| MD5 |
0e118db5fbda805d7f09876396002239
|
|
| BLAKE2b-256 |
e9966c48a323423a439011337fe067fb651fe986cbca12298a2e8fc9c6057d49
|
File details
Details for the file weightgain-0.5.0-py3-none-any.whl.
File metadata
- Download URL: weightgain-0.5.0-py3-none-any.whl
- Upload date:
- Size: 10.3 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 |
042dfc97f5fccbe7df81b8a2ea9dcc1efe7dfbaface4f2d3c5ec22cc9fcd63a9
|
|
| MD5 |
df345d35f5fe5c60f09561b317811789
|
|
| BLAKE2b-256 |
e1729a9123d5d83acac91e13a04e3df068af8a35e8a6d3856182d154b4e66906
|