Skip to main content

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

  1. Add option to train an MLP instead of a linear layer
  2. Add a method for easy hyperparameter search
  3. Move the embedding step to Adapter.train instead 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

weightgain-0.2.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

weightgain-0.2.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file weightgain-0.2.0.tar.gz.

File metadata

  • Download URL: weightgain-0.2.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.19

File hashes

Hashes for weightgain-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b649bf8e52e51b8f33b65c6677c9b3f6c51ca2d559aa8263e4a6f63bee9f7f3d
MD5 db31d31321821ea2a9c9035a10628d24
BLAKE2b-256 64e00efb3191faadb4d5522f437ff45651db46429bcaef0f70fd0b8d1a2462ff

See more details on using hashes here.

File details

Details for the file weightgain-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: weightgain-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.19

File hashes

Hashes for weightgain-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5a72212fd2303f89c0ed68dcbfacb297956506a4fc1b1fff4266b3cb59581e61
MD5 837999b996964275d0a311731ce5b09d
BLAKE2b-256 2dc8da79e90f847c4e14cf54ee4497d883e10cad4dbd7bf01ddf720ed48be060

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page