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.4.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.4.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: weightgain-0.4.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.4.0.tar.gz
Algorithm Hash digest
SHA256 a555be37f1cc81740932dc7472fcfd80faa6ffd0eb317319a937d7997b7ae479
MD5 24d0474be2a77216a4601fc6f77785b8
BLAKE2b-256 2e06d73111b1c4ad1400c02bcdcf2fbb8244422d68a26092f7c85d2915ba7db9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: weightgain-0.4.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.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 51371f8befd8423e90808d7a359c861fb7a3e18fe6e6e7b38c062c915b59b4bb
MD5 45c0ca03034e5fa3945a936e3525dd68
BLAKE2b-256 ef8ef121a3101e1dec381d3c0eae31d64f714110484e8b50c96312d637b57274

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