This repo provides a hands-on demonstration of using BAAl's (Beijing Academy of Artificial Intelligence) BGE-Reranker-v2-m3 model from Hugging Face for sequence reranking tasks. It includes two Jupyter notebooks:
- BGE_Reranker_Local.ipynb: For running the model locally using PyTorch and Hugging Face Transformers.
- BGE_Reranker_AzureML.ipynb: For deploying and running the model in Azure Machine Learning inference environment.
Note
Model weights are stored on the HuggingFace site and can be accessed at this model card page.
- Prerequisites
- Part 1: Configuring Environment
- Part 2: BGE Reranker - Local use
- Part 3: BGE Reranker - Azure AI use
- Acknowledgements
- Python 3.11+;
- GPU recommended for faster inference.
- Install necessary Python packages:
pip install -r requirements.txtThis section covers the using of the BGE Reranker v2 m3 model locally with the BGE_Reranker_Local.ipynb notebook.
Key steps include:
- Loading the Model: Import necessary libraries and load the BAAI/bge-reranker-v2-m3 model and tokenizer.
- Creating a Reranking Function: Define a Python function rerank that takes a query and returns documents sorted by relevance score.
- Testing: Demonstrate the rerank function with simple and more complex examples to show how the model's capability.
Sample reranking query results:
Query: What are the benefits of regular exercise?
Ranked Documents (most to least relevant):
1. Score: 6.7963
Document: Regular exercise improves cardiovascular health, boosts mood, and helps maintain a healthy weight.
2. Score: 1.7936
Document: Exercise has been shown to reduce the risk of chronic diseases such as diabetes and heart disease.
3. Score: 0.7743
Document: Physical activity strengthens muscles and bones, and can improve sleep quality.
4. Score: -9.6014
Document: Coffee contains caffeine which can improve alertness and concentration.
5. Score: -11.0410
Document: The capital of France is Paris, which is known for its beautiful architecture.With BGE_Reranker_AzureML.ipynb you can deploy and run the model on Azure Machine Learning online endpoint.
Key steps include:
- Connecting to Azure ML: Authenticating and establishing a connection to your Azure Machine Learning workspace.
- Finding the BGE Reranker Model: Identifying the baai-bge-reranker-v2-m3 model within the Hugging Face registry in Azure ML.
- Deploying to Online Endpoint: Creating a managed online endpoint and deploying the BGE Reranker model to it.
- Testing the Deployed Model: Sending sample query and text data to the deployed endpoint and processing the reranker's response.
- Cleaning up Resources (Optional): Providing instructions for deleting the deployed endpoint and associated resources to avoid unnecessary costs.
Sample reranking query results:
================================================================================
PROCESSED RERANKING RESULTS:
================================================================================
Query: What is Deep Learning?
================================================================================
Rank Score Text
================================================================================
1 0.999879 Deep learning is a subset of machine learning that uses neural network...
2 0.013742 Machine learning is a method of data analysis that automates analytica...
3 0.010945 Artificial intelligence is the simulation of human intelligence proces...
4 0.000142 Python is a high-level programming language widely used for web develo...
================================================================================- BAAI for developing the BGE Reranker v2 m3 model;
- HuggingFace for hosting the model and providing the Transformers library;
- Microsoft Azure for cloud AI inference services.