BAAI / bge-reranker-v2-gemma

huggingface.co
Total runs: 15.6K
24-hour runs: -186
7-day runs: 764
30-day runs: 1.5K
Model's Last Updated: March 19 2024
text-classification

Introduction of bge-reranker-v2-gemma

Model Details of bge-reranker-v2-gemma

Reranker

More details please refer to our Github: FlagEmbedding .

Different from embedding model, reranker uses question and document as input and directly output similarity instead of embedding. You can get a relevance score by inputting query and passage to the reranker. And the score can be mapped to a float value in [0,1] by sigmoid function.

Model List
Model Base model Language layerwise feature
BAAI/bge-reranker-base xlm-roberta-base Chinese and English - Lightweight reranker model, easy to deploy, with fast inference.
BAAI/bge-reranker-large xlm-roberta-large Chinese and English - Lightweight reranker model, easy to deploy, with fast inference.
BAAI/bge-reranker-v2-m3 bge-m3 Multilingual - Lightweight reranker model, possesses strong multilingual capabilities, easy to deploy, with fast inference.
BAAI/bge-reranker-v2-gemma gemma-2b Multilingual - Suitable for multilingual contexts, performs well in both English proficiency and multilingual capabilities.
BAAI/bge-reranker-v2-minicpm-layerwise MiniCPM-2B-dpo-bf16 Multilingual 8-40 Suitable for multilingual contexts, performs well in both English and Chinese proficiency, allows freedom to select layers for output, facilitating accelerated inference.

You can select the model according your senario and resource.

Usage
Using FlagEmbedding
pip install -U FlagEmbedding
For normal reranker (bge-reranker-base / bge-reranker-large / bge-reranker-v2-m3 )

Get relevance scores (higher scores indicate more relevance):

from FlagEmbedding import FlagReranker
reranker = FlagReranker('BAAI/bge-reranker-v2-m3', use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation

score = reranker.compute_score(['query', 'passage'])
print(score) # -5.65234375

# You can map the scores into 0-1 by set "normalize=True", which will apply sigmoid function to the score
score = reranker.compute_score(['query', 'passage'], normalize=True)
print(score) # 0.003497010252573502

scores = reranker.compute_score([['what is panda?', 'hi'], ['what is panda?', 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.']])
print(scores) # [-8.1875, 5.26171875]

# You can map the scores into 0-1 by set "normalize=True", which will apply sigmoid function to the score
scores = reranker.compute_score([['what is panda?', 'hi'], ['what is panda?', 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.']], normalize=True)
print(scores) # [0.00027803096387751553, 0.9948403768236574]
For LLM-based reranker
from FlagEmbedding import FlagLLMReranker
reranker = FlagLLMReranker('BAAI/bge-reranker-v2-gemma', use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
# reranker = FlagLLMReranker('BAAI/bge-reranker-v2-gemma', use_bf16=True) # You can also set use_bf16=True to speed up computation with a slight performance degradation

score = reranker.compute_score(['query', 'passage'])
print(score)

scores = reranker.compute_score([['what is panda?', 'hi'], ['what is panda?', 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.']])
print(scores)
For LLM-based layerwise reranker
from FlagEmbedding import LayerWiseFlagLLMReranker
reranker = LayerWiseFlagLLMReranker('BAAI/bge-reranker-v2-minicpm-layerwise', use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
# reranker = LayerWiseFlagLLMReranker('BAAI/bge-reranker-v2-minicpm-layerwise', use_bf16=True) # You can also set use_bf16=True to speed up computation with a slight performance degradation

score = reranker.compute_score(['query', 'passage'], cutoff_layers=[28]) # Adjusting 'cutoff_layers' to pick which layers are used for computing the score.
print(score)

scores = reranker.compute_score([['what is panda?', 'hi'], ['what is panda?', 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.']], cutoff_layers=[28])
print(scores)
Using Huggingface transformers
For normal reranker (bge-reranker-base / bge-reranker-large / bge-reranker-v2-m3 )

Get relevance scores (higher scores indicate more relevance):

import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-reranker-v2-m3')
model = AutoModelForSequenceClassification.from_pretrained('BAAI/bge-reranker-v2-m3')
model.eval()

pairs = [['what is panda?', 'hi'], ['what is panda?', 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.']]
with torch.no_grad():
    inputs = tokenizer(pairs, padding=True, truncation=True, return_tensors='pt', max_length=512)
    scores = model(**inputs, return_dict=True).logits.view(-1, ).float()
    print(scores)
For LLM-based reranker
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

def get_inputs(pairs, tokenizer, prompt=None, max_length=1024):
    if prompt is None:
        prompt = "Given a query A and a passage B, determine whether the passage contains an answer to the query by providing a prediction of either 'Yes' or 'No'."
    sep = "\n"
    prompt_inputs = tokenizer(prompt,
                              return_tensors=None,
                              add_special_tokens=False)['input_ids']
    sep_inputs = tokenizer(sep,
                           return_tensors=None,
                           add_special_tokens=False)['input_ids']
    inputs = []
    for query, passage in pairs:
        query_inputs = tokenizer(f'A: {query}',
                                 return_tensors=None,
                                 add_special_tokens=False,
                                 max_length=max_length * 3 // 4,
                                 truncation=True)
        passage_inputs = tokenizer(f'B: {passage}',
                                   return_tensors=None,
                                   add_special_tokens=False,
                                   max_length=max_length,
                                   truncation=True)
        item = tokenizer.prepare_for_model(
            [tokenizer.bos_token_id] + query_inputs['input_ids'],
            sep_inputs + passage_inputs['input_ids'],
            truncation='only_second',
            max_length=max_length,
            padding=False,
            return_attention_mask=False,
            return_token_type_ids=False,
            add_special_tokens=False
        )
        item['input_ids'] = item['input_ids'] + sep_inputs + prompt_inputs
        item['attention_mask'] = [1] * len(item['input_ids'])
        inputs.append(item)
    return tokenizer.pad(
            inputs,
            padding=True,
            max_length=max_length + len(sep_inputs) + len(prompt_inputs),
            pad_to_multiple_of=8,
            return_tensors='pt',
    )

tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-reranker-v2-gemma')
model = AutoModelForCausalLM.from_pretrained('BAAI/bge-reranker-v2-gemma')
yes_loc = tokenizer('Yes', add_special_tokens=False)['input_ids'][0]
model.eval()

pairs = [['what is panda?', 'hi'], ['what is panda?', 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.']]
with torch.no_grad():
    inputs = get_inputs(pairs, tokenizer)
    scores = model(**inputs, return_dict=True).logits[:, -1, yes_loc].view(-1, ).float()
    print(scores)
For LLM-based layerwise reranker
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

def get_inputs(pairs, tokenizer, prompt=None, max_length=1024):
    if prompt is None:
        prompt = "Given a query A and a passage B, determine whether the passage contains an answer to the query by providing a prediction of either 'Yes' or 'No'."
    sep = "\n"
    prompt_inputs = tokenizer(prompt,
                              return_tensors=None,
                              add_special_tokens=False)['input_ids']
    sep_inputs = tokenizer(sep,
                           return_tensors=None,
                           add_special_tokens=False)['input_ids']
    inputs = []
    for query, passage in pairs:
        query_inputs = tokenizer(f'A: {query}',
                                 return_tensors=None,
                                 add_special_tokens=False,
                                 max_length=max_length * 3 // 4,
                                 truncation=True)
        passage_inputs = tokenizer(f'B: {passage}',
                                   return_tensors=None,
                                   add_special_tokens=False,
                                   max_length=max_length,
                                   truncation=True)
        item = tokenizer.prepare_for_model(
            [tokenizer.bos_token_id] + query_inputs['input_ids'],
            sep_inputs + passage_inputs['input_ids'],
            truncation='only_second',
            max_length=max_length,
            padding=False,
            return_attention_mask=False,
            return_token_type_ids=False,
            add_special_tokens=False
        )
        item['input_ids'] = item['input_ids'] + sep_inputs + prompt_inputs
        item['attention_mask'] = [1] * len(item['input_ids'])
        inputs.append(item)
    return tokenizer.pad(
            inputs,
            padding=True,
            max_length=max_length + len(sep_inputs) + len(prompt_inputs),
            pad_to_multiple_of=8,
            return_tensors='pt',
    )

tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-reranker-v2-minicpm-layerwise', trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained('BAAI/bge-reranker-v2-minicpm-layerwise', trust_remote_code=True, torch_dtype=torch.bfloat16)
model = model.to('cuda')
model.eval()

pairs = [['what is panda?', 'hi'], ['what is panda?', 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.']]
with torch.no_grad():
    inputs = get_inputs(pairs, tokenizer).to(model.device)
    all_scores = model(**inputs, return_dict=True, cutoff_layers=[28])
    all_scores = [scores[:, -1].view(-1, ).float() for scores in all_scores[0]]
    print(all_scores)
Fine-tune
Data Format

Train data should be a json file, where each line is a dict like this:

{"query": str, "pos": List[str], "neg":List[str], "prompt": str}

query is the query, and pos is a list of positive texts, neg is a list of negative texts, prompt indicates the relationship between query and texts. If you have no negative texts for a query, you can random sample some from the entire corpus as the negatives.

See toy_finetune_data.jsonl for a toy data file.

Train

You can fine-tune the reranker with the following code:

For llm-based reranker

torchrun --nproc_per_node {number of gpus} \
-m FlagEmbedding.llm_reranker.finetune_for_instruction.run \
--output_dir {path to save model} \
--model_name_or_path google/gemma-2b \
--train_data ./toy_finetune_data.jsonl \
--learning_rate 2e-4 \
--num_train_epochs 1 \
--per_device_train_batch_size 1 \
--gradient_accumulation_steps 16 \
--dataloader_drop_last True \
--query_max_len 512 \
--passage_max_len 512 \
--train_group_size 16 \
--logging_steps 1 \
--save_steps 2000 \
--save_total_limit 50 \
--ddp_find_unused_parameters False \
--gradient_checkpointing \
--deepspeed stage1.json \
--warmup_ratio 0.1 \
--bf16 \
--use_lora True \
--lora_rank 32 \
--lora_alpha 64 \
--use_flash_attn True \
--target_modules q_proj k_proj v_proj o_proj

For llm-based layerwise reranker

torchrun --nproc_per_node {number of gpus} \
-m FlagEmbedding.llm_reranker.finetune_for_layerwise.run \
--output_dir {path to save model} \
--model_name_or_path openbmb/MiniCPM-2B-dpo-bf16 \
--train_data ./toy_finetune_data.jsonl \
--learning_rate 2e-4 \
--num_train_epochs 1 \
--per_device_train_batch_size 1 \
--gradient_accumulation_steps 16 \
--dataloader_drop_last True \
--query_max_len 512 \
--passage_max_len 512 \
--train_group_size 16 \
--logging_steps 1 \
--save_steps 2000 \
--save_total_limit 50 \
--ddp_find_unused_parameters False \
--gradient_checkpointing \
--deepspeed stage1.json \
--warmup_ratio 0.1 \
--bf16 \
--use_lora True \
--lora_rank 32 \
--lora_alpha 64 \
--use_flash_attn True \
--target_modules q_proj k_proj v_proj o_proj \
--start_layer 8 \
--head_multi True \
--head_type simple \
--lora_extra_parameters linear_head

Our rerankers are initialized from google/gemma-2b (for llm-based reranker) and openbmb/MiniCPM-2B-dpo-bf16 (for llm-based layerwise reranker), and we train it on a mixture of multilingual datasets:

Evaluation
  • llama-index.

image-20240317193909373

  • BEIR.

rereank the top 100 results from bge-en-v1.5 large.

image-20240317174633333

rereank the top 100 results from e5 mistral 7b instruct.

image-20240317172949713

  • CMTEB-retrieval.
    It rereank the top 100 results from bge-zh-v1.5 large.

image-20240317173026235

  • miracl (multi-language).
    It rereank the top 100 results from bge-m3.

image-20240317173117639

Citation

If you find this repository useful, please consider giving a star and citation

@misc{li2023making,
      title={Making Large Language Models A Better Foundation For Dense Retrieval}, 
      author={Chaofan Li and Zheng Liu and Shitao Xiao and Yingxia Shao},
      year={2023},
      eprint={2312.15503},
      archivePrefix={arXiv},
      primaryClass={cs.CL}
}
@misc{chen2024bge,
      title={BGE M3-Embedding: Multi-Lingual, Multi-Functionality, Multi-Granularity Text Embeddings Through Self-Knowledge Distillation}, 
      author={Jianlv Chen and Shitao Xiao and Peitian Zhang and Kun Luo and Defu Lian and Zheng Liu},
      year={2024},
      eprint={2402.03216},
      archivePrefix={arXiv},
      primaryClass={cs.CL}
}

Runs of BAAI bge-reranker-v2-gemma on huggingface.co

15.6K
Total runs
-186
24-hour runs
-783
3-day runs
764
7-day runs
1.5K
30-day runs

More Information About bge-reranker-v2-gemma huggingface.co Model

More bge-reranker-v2-gemma license Visit here:

https://choosealicense.com/licenses/apache-2.0

bge-reranker-v2-gemma huggingface.co

bge-reranker-v2-gemma huggingface.co is an AI model on huggingface.co that provides bge-reranker-v2-gemma's model effect (), which can be used instantly with this BAAI bge-reranker-v2-gemma model. huggingface.co supports a free trial of the bge-reranker-v2-gemma model, and also provides paid use of the bge-reranker-v2-gemma. Support call bge-reranker-v2-gemma model through api, including Node.js, Python, http.

bge-reranker-v2-gemma huggingface.co Url

https://huggingface.co/BAAI/bge-reranker-v2-gemma

BAAI bge-reranker-v2-gemma online free

bge-reranker-v2-gemma huggingface.co is an online trial and call api platform, which integrates bge-reranker-v2-gemma's modeling effects, including api services, and provides a free online trial of bge-reranker-v2-gemma, you can try bge-reranker-v2-gemma online for free by clicking the link below.

BAAI bge-reranker-v2-gemma online free url in huggingface.co:

https://huggingface.co/BAAI/bge-reranker-v2-gemma

bge-reranker-v2-gemma install

bge-reranker-v2-gemma is an open source model from GitHub that offers a free installation service, and any user can find bge-reranker-v2-gemma on GitHub to install. At the same time, huggingface.co provides the effect of bge-reranker-v2-gemma install, users can directly use bge-reranker-v2-gemma installed effect in huggingface.co for debugging and trial. It also supports api for free installation.

bge-reranker-v2-gemma install url in huggingface.co:

https://huggingface.co/BAAI/bge-reranker-v2-gemma

Url of bge-reranker-v2-gemma

bge-reranker-v2-gemma huggingface.co Url

Provider of bge-reranker-v2-gemma huggingface.co

BAAI
ORGANIZATIONS

Other API from BAAI

huggingface.co

Total runs: 5.8M
Run Growth: -12.8M
Growth Rate: -221.78%
Updated: February 22 2024
huggingface.co

Total runs: 3.1M
Run Growth: -416.9M
Growth Rate: -13622.29%
Updated: February 21 2024
huggingface.co

Total runs: 3.1M
Run Growth: -3.8K
Growth Rate: -0.12%
Updated: July 03 2024
huggingface.co

Total runs: 3.0M
Run Growth: -83.7K
Growth Rate: -2.76%
Updated: February 21 2024
huggingface.co

Total runs: 771.0K
Run Growth: 370.0K
Growth Rate: 47.60%
Updated: October 12 2023
huggingface.co

Total runs: 753.2K
Run Growth: 91.8K
Growth Rate: 12.29%
Updated: December 13 2023
huggingface.co

Total runs: 302.1K
Run Growth: -109.7K
Growth Rate: -37.05%
Updated: November 14 2023
huggingface.co

Total runs: 47.5K
Run Growth: 36.1K
Growth Rate: 76.48%
Updated: October 12 2023
huggingface.co

Total runs: 34.7K
Run Growth: -842
Growth Rate: -2.44%
Updated: April 17 2024
huggingface.co

Total runs: 29.5K
Run Growth: 14.9K
Growth Rate: 41.22%
Updated: September 25 2024
huggingface.co

Total runs: 16.3K
Run Growth: 7.5K
Growth Rate: 49.79%
Updated: February 22 2024
huggingface.co

Total runs: 11.1K
Run Growth: -34.6K
Growth Rate: -299.10%
Updated: December 26 2022
huggingface.co

Total runs: 9.0K
Run Growth: 5.4K
Growth Rate: 62.69%
Updated: October 12 2023
huggingface.co

Total runs: 4.5K
Run Growth: 692
Growth Rate: 15.97%
Updated: October 12 2023
huggingface.co

Total runs: 3.9K
Run Growth: 3.2K
Growth Rate: 81.47%
Updated: October 27 2023
huggingface.co

Total runs: 3.7K
Run Growth: 2.8K
Growth Rate: 77.30%
Updated: August 15 2024
huggingface.co

Total runs: 3.5K
Run Growth: 3.1K
Growth Rate: 91.22%
Updated: March 07 2024
huggingface.co

Total runs: 3.2K
Run Growth: 2.7K
Growth Rate: 90.32%
Updated: September 21 2023
huggingface.co

Total runs: 2.8K
Run Growth: 1.3K
Growth Rate: 47.73%
Updated: November 28 2024
huggingface.co

Total runs: 2.6K
Run Growth: 2.6K
Growth Rate: 98.50%
Updated: July 02 2024
huggingface.co

Total runs: 2.2K
Run Growth: 92
Growth Rate: 4.04%
Updated: August 15 2024
huggingface.co

Total runs: 2.1K
Run Growth: 1.3K
Growth Rate: 60.15%
Updated: April 19 2024
huggingface.co

Total runs: 2.0K
Run Growth: 230
Growth Rate: 12.47%
Updated: May 13 2024
huggingface.co

Total runs: 2.0K
Run Growth: -2.4K
Growth Rate: -123.09%
Updated: February 07 2024
huggingface.co

Total runs: 705
Run Growth: 146
Growth Rate: 22.85%
Updated: September 18 2023
huggingface.co

Total runs: 703
Run Growth: -65
Growth Rate: -8.98%
Updated: June 07 2024
huggingface.co

Total runs: 655
Run Growth: 14
Growth Rate: 2.07%
Updated: August 15 2024
huggingface.co

Total runs: 502
Run Growth: -906
Growth Rate: -180.48%
Updated: April 02 2024
huggingface.co

Total runs: 492
Run Growth: 424
Growth Rate: 86.35%
Updated: February 07 2024
huggingface.co

Total runs: 477
Run Growth: -945
Growth Rate: -197.29%
Updated: October 12 2023
huggingface.co

Total runs: 402
Run Growth: -1
Growth Rate: -0.25%
Updated: June 24 2024
huggingface.co

Total runs: 271
Run Growth: 176
Growth Rate: 76.19%
Updated: June 21 2024
huggingface.co

Total runs: 190
Run Growth: 143
Growth Rate: 75.26%
Updated: December 31 2022
huggingface.co

Total runs: 174
Run Growth: 77
Growth Rate: 44.00%
Updated: December 21 2023
huggingface.co

Total runs: 135
Run Growth: -51
Growth Rate: -38.35%
Updated: December 21 2023
huggingface.co

Total runs: 117
Run Growth: -38
Growth Rate: -31.40%
Updated: August 23 2023
huggingface.co

Total runs: 104
Run Growth: -343
Growth Rate: -285.83%
Updated: August 23 2023
huggingface.co

Total runs: 77
Run Growth: 13
Growth Rate: 16.67%
Updated: April 18 2023
huggingface.co

Total runs: 61
Run Growth: -90
Growth Rate: -147.54%
Updated: June 24 2024
huggingface.co

Total runs: 52
Run Growth: -35
Growth Rate: -60.34%
Updated: August 15 2024
huggingface.co

Total runs: 42
Run Growth: -87
Growth Rate: -133.85%
Updated: May 13 2024
huggingface.co

Total runs: 34
Run Growth: 4
Growth Rate: 9.30%
Updated: December 25 2023
huggingface.co

Total runs: 31
Run Growth: -40
Growth Rate: -133.33%
Updated: August 28 2024
huggingface.co

Total runs: 28
Run Growth: -3
Growth Rate: -10.71%
Updated: July 24 2023
huggingface.co

Total runs: 25
Run Growth: -83
Growth Rate: -332.00%
Updated: August 15 2024
huggingface.co

Total runs: 25
Run Growth: -5
Growth Rate: -20.83%
Updated: May 31 2024
huggingface.co

Total runs: 22
Run Growth: -22
Growth Rate: -100.00%
Updated: July 05 2024
huggingface.co

Total runs: 16
Run Growth: -26
Growth Rate: -162.50%
Updated: June 17 2024
huggingface.co

Total runs: 13
Run Growth: 8
Growth Rate: 66.67%
Updated: October 09 2023
huggingface.co

Total runs: 9
Run Growth: -12
Growth Rate: -150.00%
Updated: June 17 2024
huggingface.co

Total runs: 9
Run Growth: 3
Growth Rate: 33.33%
Updated: November 28 2023
huggingface.co

Total runs: 7
Run Growth: -2
Growth Rate: -28.57%
Updated: May 13 2024
huggingface.co

Total runs: 0
Run Growth: 0
Growth Rate: 0.00%
Updated: July 25 2024
huggingface.co

Total runs: 0
Run Growth: 0
Growth Rate: 0.00%
Updated: December 07 2022