Skip to main content

Fine-tune BERT for vectorized query search

Project description

Vector Query Optimizer

How it works and what it does:

This project aims to allow easy fine-tuning and improvement of BERT models that are used as embedding models utilizing a Parameter Efficient Fine-Tuning(PEFT) method Low-Rank Adaptation(LoRA).

Fine-tuning and improvement of embedding models are needed when you are querying over vector databases and the results are not what you expect. Using LoRA results in efficient fine-tuning while also preventing catastrophic forgetting.

To see the improved results, this project also lets you run inference easily, utilizing the open-source vector database ChromaDB.

If you have a GPU, running scripts or using the package will automatically use it so you don't have to worry about it.


How to Use

Scripts

You can download the source code and run the scripts.

You can either run the bash scripts which are named like this: run_script.sh

or

Directly use torchrun command through terminal within the vecquery_tune/scripts directory.

Here is an example usage for running the fine_tune.py script:

torchrun fine_tune.py \
    --model_name bert-base-uncased \
    --data_path data.json \
    --path_to_save_peft_folder ./ \
    --epochs 5 \
    --batch_size 32 \
    --max_len 256 \
    --lr 2e-5

Package

First, install the package via pip:

pip install vecquery_tune

Then, you can use the 'FineTune' class to define a method with which you can fine-tune a BERT model.

Here is an example usage:

from vecquery_tune import FineTune

# fine tune model
fine_tune = FineTune(
    data_path='data.json',
    model_name='bert-base-uncased',
    path_to_save_peft_folder='./'
)

fine_tune(
    epochs=5,
    batch_size=32,
    max_len=256,
    lr=2e-5
)

'data.json' file must be of the format:

[
    {
        'input': 'query input',
        'output': 'the desired output of the query'
    },
]

To see the improved results, you need to first create a database and add your data into it. This package utilizes ChromaDB to run inference and see the results. Before using the 'Inference' class, you need to use 'CreateDatabase' class and create a database.

Here is how you can use the 'CreateDatabase' class:

from vecquery_tune.vecquery_tune import CreateDatabase

# create database
create_database = CreateDatabase(
    data_path='data.csv',
    model_name='bert-base-uncased',
    peft_folder_path='./peft_model',
    collection_name='collection',
    client_path='./'
)

create_database(
    metadata_column='time,author', # seperate metadata columns by either ';' or ','
    documents_column='docs',
    max_len=256
)

After creating the database, you can run inference via the 'Inference' class. Keep in mind that if you supplied a collection name and a client path different than the default ones, you need to supply them here as well.

Here is how you can use the 'Inference' class:

from vecquery_tune.vecquery_tune import Inference

# inference
inference = Inference(
    peft_folder_path='./peft_model',
    model_name='bert-base-uncased',
    collection_name='collection',
    client_path='./',
    num_results=5
)

inference(
    metadata_column='author',
    documents_column='docs',
    max_len=256
)

Issues with Installing ChromaDB

Please note that if you encounter issues while installing ChromaDB, you may need to install Visual Studio. You can take a look at this SO answer for more information.

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

vecquery_tune-0.0.7.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

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

vecquery_tune-0.0.7-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file vecquery_tune-0.0.7.tar.gz.

File metadata

  • Download URL: vecquery_tune-0.0.7.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for vecquery_tune-0.0.7.tar.gz
Algorithm Hash digest
SHA256 4232e42e13902739983411302f6bce520c6189da0d669f34ac2005d551d5fe04
MD5 b774959250a4752fe45df4b60a5d008a
BLAKE2b-256 9625bcc6508e7dfe48e3afce01e08d1294f789817a9bb3b1e88ddfca2620d6ea

See more details on using hashes here.

File details

Details for the file vecquery_tune-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: vecquery_tune-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for vecquery_tune-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 c0e87684382c1b43d025ebc70e04b646ce4de290ed81f76c01335dacb1160fb3
MD5 565c4ef54390a94b2066a38224610b2a
BLAKE2b-256 0fb54b594bb6d1b65f75b94f3f6bbd76d14a7380b8b6c7bc00cee725e1dd46cb

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