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

Example 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.3.0.tar.gz (10.2 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.3.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for weightgain-0.3.0.tar.gz
Algorithm Hash digest
SHA256 a738c875ad32da2639a169fb18c7eacaf4800d7d8471ba17ae8f892448070a5f
MD5 daeb34ecb81177a1711cfaaaea5e0a79
BLAKE2b-256 141a59517c2d853194ae771a0e0f40717507c7afb5b3a07e4dd34edba20b9d97

See more details on using hashes here.

File details

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

File metadata

  • Download URL: weightgain-0.3.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

Hashes for weightgain-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d9802a9ba4a7154dff0e635003b1b14d12c2ff3a87d56ce23e5081f7728e71d6
MD5 86f48232dda6688cbc2eda12ee91377d
BLAKE2b-256 9f5dca5743657a3333f341bbf9aea18a2b870c1673cbbdb020279baaa4d353e1

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