🎉 embedding-ada
📜 Description
embedding-ada is a Python package used to fine-tune embedding adapter on top of embedding models from Hugging Face and OpenAI. This package is build using Llama-Index.
Why Use embedding-ada?
Improved Retrieval Performance: By fine-tuning adapters, this package helps you bring relevant documents closer to the query embeddings, improving the results of approximate nearest neighbor searches.
🚀 Installation
You can install embedding-ada directly from PyPI using pip:
pip install embedding-ada
💻 Usage
from embedding_ada.adapter import EmbeddingModelTrainer
# Define your training files
train_files = ["path/to/your/example.pdf"]
# Initialize the trainer
trainer = EmbeddingModelTrainer(
embedding_model_name='BAAI/bge-small-en',
train_files=train_files,
model_output_path="model_output_test",
epochs=10,
llm_type="openai" # or "azure" if using Azure OpenAI
)
# Load train files and generate dataset out of it.
trainer.load_and_generate_dataset(verbose=True)
#Fine-tune the model using the dataset
trainer.fit()
#Finally, transform a query using the fine-tuned model
transformed_query_embedding = trainer.transform('query_text')
Overview
The user has to provide text on which he want to trian the adapter model. The text can be in either .pdf or .txt files. This text is parsed into nodes, with each node representing a "chunk" of a source document. Using a language model (LLM), we generate a question from each node. This process forms query-context pairs, which are then used to train a neural network model. The default model is a simple two-layer neural network with ReLU activation and a residual layer at the end. Users also have the option to define a custom neural network as the adapter model and train it with the formulated query-context pairs.
Training Process
The training process uses the MultipleNegativesRankingLoss function, similar to the one used in training sentence_transformers. This loss function is particularly effective for training embeddings in retrieval setups where you have positive pairs (e.g., query and relevant document). During training, for each batch, the function randomly samples n-1 negative documents, making it robust for retrieval tasks.
Embedding Adapters
The core concept behind embedding adapters is as follows: Given a set of query embeddings, and corresponding sets of relevant and irrelevant document embeddings for each query, the adapter model learns a transformation that adjusts the vector space. This transformation "squeezes" and "rotates" the space, mapping it to a new space where relevant documents are closer to the query. We refer to this transformation as an 'adapter,' as it is applied after the output of the embedding model.
RAG Use Case
Consider a scenario where you have a large corpus of text, and you want to build a Q&A system on that corpus. The initial step involves splitting the text corpus into chunks, converting these chunks into embeddings, and storing them in a vector database.
When a user makes a query, the goal is to retrieve text chunks from the vector database that are relevant to the query. Vector databases utilize Approximate Nearest Neighbors (ANN) algorithms, such as HNSW, to fetch approximate nearest neighbors to the query embedding.
Instead of performing an ANN search with the original query, you perform the search with the transformed query (transformed using the trained adapter model). The results are significantly better because the transformed query embeddings are intended to be closer to the relevant text chunks.
📄License
This project is licensed under the MIT License.
🛠️ Contributions
Contributions are welcome! Feel free to open an issue or submit a pull request.
📬 Contact
If you have any questions, feel free to reach out: Email: pavankumarchowdary35@gmail.com GitHub: pavankumarchowdary35
🔔 Note:
To generate a synthetic dataset from the user-provided training files, you need to configure an LLM instance.
🌐 Using OpenAI
If you want to use the OpenAI instance, please set your OpenAI API key:
import os
os.environ["OPENAI_API_KEY"] = "your-openai-api-key"
If you want to use Azure OpenAI, set the following variables
import os
os.environ["LLM_TYPE"] = "azure"
os.environ["AZURE_OPENAI_ENGINE"] = "gpt4"
os.environ["AZURE_OPENAI_ENDPOINT"] = "https://your-azure-endpoint.com"
os.environ["AZURE_OPENAI_API_KEY"] = "your-azure-api-key"
os.environ["AZURE_OPENAI_API_VERSION"] = "Version"
Metadata
Release files for embedding-ada 0.1.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| embedding_ada-0.1.9.tar.gz | 15.5 kB | Without provenance | Details for embedding_ada-0.1.9.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| embedding_ada-0.1.9-py3-none-any.whl | Python 3 | none | any | Details for embedding_ada-0.1.9-py3-none-any.whl |
Total release size: 32.2 kB
Release files / embedding_ada-0.1.9.tar.gz
| Download URL | embedding_ada-0.1.9.tar.gz |
|---|---|
| Size | 15.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b2532ec069fde2ef1c0a89f07f1cca6c72f30158e8de9c150317495f96a9dc25
|
|
BLAKE2b-256 checksum How to use checksums |
76bb40573c18d4d6439e9d800bfd1a23198ce9db12549ae59bd23e510b29ff17
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.11.4
|
Release files / embedding_ada-0.1.9-py3-none-any.whl
| Download URL | embedding_ada-0.1.9-py3-none-any.whl |
|---|---|
| Size | 16.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
531e044bc5a39dc85def9a239c6cd50ab5e4444cd0901171291c6e682691e0d0
|
|
BLAKE2b-256 checksum How to use checksums |
efd266dbc206257b939ccfa205b14c5dff1e6ae51df42e34e29c6d842401cb16
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.11.4
|
Security for embedding-ada 0.1.9
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.