🚀 MiniCPM-Reranker
MiniCPM-Reranker is a bilingual and cross-lingual text re-ranking model jointly developed by ModelBest Inc., THUNLP (Tsinghua University Natural Language Processing Laboratory), and NEUIR (Northeastern University Information Retrieval Group). It offers the following advantages:
- Exceptional re-ranking capabilities for both Chinese and English texts.
- Outstanding cross-lingual re-ranking capabilities between Chinese and English.
MiniCPM-Reranker is trained based on MiniCPM-2B-sft-bf16 and adopts bidirectional attention in its architecture. It undergoes multi-stage training, utilizing approximately 6 million training examples, including open-source, synthetic, and proprietary data.
We also invite you to explore the RAG toolkit series:
✨ Features
- Exceptional Chinese and English re-ranking capabilities.
- Outstanding cross-lingual re-ranking capabilities between Chinese and English.
📦 Installation
Requirements
transformers==4.37.2
flash-attn>2.3.5
💻 Usage Examples
Basic Usage
MiniCPM-Reranker supports instructions in the following format:
<s>Instruction: {{ instruction }} Query: {{ query }}</s>{{ document }}
For example:
<s>Instruction: 为这个医学问题检索相关回答。Query: 咽喉癌的成因是什么?</s>(文档省略)
<s>Instruction: Given a claim about climate change, retrieve documents that support or refute the claim. Query: However the warming trend is slower than most climate models have forecast.</s>(document omitted)
It also works in instruction-free mode in the following format:
<s>Query: {{ query }}</s>{{ document }}
When running evaluation on BEIR and C-MTEB/Retrieval, we use instructions in instructions.json
. For other evaluations, we do not use instructions.
Advanced Usage
Huggingface Transformers
from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch
import numpy as np
model_name = "openbmb/MiniCPM-Reranker"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
tokenizer.padding_side = "right"
model = AutoModelForSequenceClassification.from_pretrained(model_name, trust_remote_code=True, torch_dtype=torch.float16).to("cuda")
model.eval()
@torch.no_grad()
def rerank(input_query, input_docs):
tokenized_inputs = tokenizer([[input_query, input_doc] for input_doc in input_docs], return_tensors="pt", padding=True, truncation=True, max_length=1024)
for k in tokenized_inputs:
tokenized_inputs [k] = tokenized_inputs[k].to("cuda")
outputs = model(**tokenized_inputs)
score = outputs.logits
return score.float().detach().cpu().numpy()
queries = ["中国的首都是哪里?"]
passages = [["beijing", "shanghai"]]
INSTRUCTION = "Query: "
queries = [INSTRUCTION + query for query in queries]
scores = []
for i in range(len(queries)):
print(queries[i])
scores.append(rerank(queries[i],passages[i]))
print(np.array(scores))
Sentence Transformer
from sentence_transformers import CrossEncoder
import torch
model_name = "openbmb/MiniCPM-Reranker"
model = CrossEncoder(model_name,max_length=1024,trust_remote_code=True, automodel_args={"torch_dtype": torch.float16})
model.tokenizer.padding_side = "right"
query = "中国的首都是哪里?"
passages = [["beijing", "shanghai"]]
INSTRUCTION = "Query: "
query = INSTRUCTION + query
sentence_pairs = [[query, doc] for doc in passages]
scores = model.predict(sentence_pairs, convert_to_tensor=True).tolist()
rankings = model.rank(query, passages, return_documents=True, convert_to_tensor=True)
print(scores)
for ranking in rankings:
print(f"Score: {ranking['score']:.4f}, Corpus: {ranking['text']}")
📚 Documentation
Model Information
Property |
Details |
Model Size |
2.4B |
Max Input Tokens |
1024 |
Evaluation Results
CN/EN Re-ranking Results
We re-rank top-100 docments from bge-large-zh-v1.5
in C-MTEB/Retrieval and from bge-large-en-v1.5
in BEIR.
模型 Model |
C-MTEB/Retrieval (NDCG@10) |
BEIR (NDCG@10) |
bge-large-zh-v1.5(Retriever for Chinese) |
70.46 |
- |
bge-large-en-v1.5(Retriever for English) |
- |
54.29 |
bge-reranker-v2-m3 |
71.82 |
55.36 |
bge-reranker-v2-minicpm-28 |
73.51 |
59.86 |
bge-reranker-v2-gemma |
71.74 |
60.71 |
bge-reranker-v2.5-gemma2 |
- |
63.67 |
MiniCPM-Reranker |
76.79 |
61.32 |
CN-EN Cross-lingual Re-ranking Results
We re-rank top-100 documents from bge-m3
(Dense).
模型 Model |
MKQA En-Zh_CN (Recall@20) |
NeuCLIR22 (NDCG@10) |
NeuCLIR23 (NDCG@10) |
bge-m3 (Dense)(Retriever) |
66.4 |
30.49 |
41.09 |
jina-reranker-v2-base-multilingual |
69.33 |
36.66 |
50.03 |
bge-reranker-v2-m3 |
69.75 |
40.98 |
49.67 |
gte-multilingual-reranker-base |
68.51 |
38.74 |
45.3 |
MiniCPM-Reranker |
71.73 |
43.65 |
50.59 |
📄 License
- The code in this repo is released under the Apache-2.0 License.
- The usage of MiniCPM-Reranker model weights must strictly follow MiniCPM Model License.md.
- The models and weights of MiniCPM-Reranker are completely free for academic research. After filling out a "questionnaire" for registration, MiniCPM-Reranker weights are also available for free commercial use.