"Reranker is designed in cross-encoder architecture that takes the query and text at the same time and directly output their score of similarity. It is more capable of scoring the query-text relevance, but with the tradeoff of slower speed. Thus, a complete retrieval system usually contains retrievers in the first stage to do a large scope retrieval, and then followed by rerankers to rerank the results more precisely.\n",
"Note: Steps 1-4 are identical to the tutorial of [evaluation](https://github.com/FlagOpen/FlagEmbedding/tree/master/Tutorials/4_Evaluation). We suggest to first go through that if you are not familiar with retrieval."
"| [BAAI/bge-reranker-v2-m3](https://huggingface.co/BAAI/bge-reranker-v2-m3) | Multilingual | 568M | a lightweight cross-encoder model, possesses strong multilingual capabilities, easy to deploy, with fast inference. | XLM-RoBERTa-Large |\n",
"| [BAAI/bge-reranker-v2-gemma](https://huggingface.co/BAAI/bge-reranker-v2-gemma) | Multilingual | 2.51B | a cross-encoder model which is suitable for multilingual contexts, performs well in both English proficiency and multilingual capabilities. | Gemma2-2B |\n",
"| [BAAI/bge-reranker-v2-minicpm-layerwise](https://huggingface.co/BAAI/bge-reranker-v2-minicpm-layerwise) | Multilingual | 2.72B | a cross-encoder model which is suitable for multilingual contexts, performs well in both English and Chinese proficiency, allows freedom to select layers for output, facilitating accelerated inference. | MiniCPM |\n",
"| [BAAI/bge-reranker-v2.5-gemma2-lightweight](https://huggingface.co/BAAI/bge-reranker-v2.5-gemma2-lightweight) | Multilingual | 9.24B | a cross-encoder model which is suitable for multilingual contexts, performs well in both English and Chinese proficiency, allows freedom to select layers, compress ratio and compress layers for output, facilitating accelerated inference. | Gemma2-9B |\n",
"| [BAAI/bge-reranker-large](https://huggingface.co/BAAI/bge-reranker-large) | Chinese and English | 560M | a cross-encoder model which is more accurate but less efficient | XLM-RoBERTa-Large |\n",
"| [BAAI/bge-reranker-base](https://huggingface.co/BAAI/bge-reranker-base) | Chinese and English | 278M | a cross-encoder model which is more accurate but less efficient | XLM-RoBERTa-Base |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, let's use a small example to see how reranker works:"
"# Setting use_fp16 to True speeds up computation with a slight performance degradation\n",
"\n",
"# use the compute_score() function to calculate scores for each input sentence pair\n",
"scores = reranker.compute_score([\n",
" ['what is panda?', 'Today is a sunny day'], \n",
" ['what is panda?', 'The tiger (Panthera tigris) is a member of the genus Panthera and the largest living cat species native to Asia.'],\n",
" ['what is panda?', 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.']\n",
" ])\n",
"print(scores)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, let's use the reranker to rerank our previously retrieved results:"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"new_ids, new_scores, new_text = [], [], []\n",
"for i in range(len(queries)):\n",
" # get the new scores of the previously retrieved results\n",
" new_score = reranker.compute_score([[queries[i], text] for text in res_text[i]])\n",
" # sort the lists of ids and scores by the new scores\n",
" new_id = [tup[1] for tup in sorted(list(zip(new_score, res_ids[i])), reverse=True)]\n",
"For details of these metrics, please check out the tutorial of [evaluation](https://github.com/FlagOpen/FlagEmbedding/tree/master/Tutorials/4_Evaluation)."