Skip to main content

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()

Example 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

  1. Add option to train an MLP instead of a linear layer
  2. Add a method for easy hyperparameter search

Metadata

Release files for weightgain 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for weightgain 1.0.0
File Size Uploaded
weightgain-1.0.0.tar.gz 10.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for weightgain 1.0.0
File Interpreter ABI Platform
weightgain-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.2 kB

Release files / weightgain-1.0.0.tar.gz

Download URL weightgain-1.0.0.tar.gz
Size 10.0 kB
Tags Source
SHA-256 checksum
How to use checksums
cf6897db9b10c30e7164f613f2eb0eba92f11e0cadc233a1d60bce67ec9cd558
BLAKE2b-256 checksum
How to use checksums
cfa538510d49b690c6c43bcf7c972b7facd2020554d801421773b0b93169d256
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.19

Release files / weightgain-1.0.0-py3-none-any.whl

Download URL weightgain-1.0.0-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a8d8eaed2b7c729e2572490c0047ddba807b0f3eb0f2d8e588633390c4a8f61d
BLAKE2b-256 checksum
How to use checksums
cfe9afe6f2331c3a964ae829d355d20de44d02b1ac146f2315287a4526b4bcde
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.19

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page