Views
No views yet
<s>Instruction: {{ instruction }} Query: {{ query }}</s>{{ document }}<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)<s>Query: {{ query }}</s>{{ document }}instructions.json,其他测试不使用指令。instructions.json. For other evaluations, we do not use instructions.transformers==4.37.2
flash-attn>2.3.51from transformers import AutoModelForSequenceClassification, AutoTokenizer
2import torch
3import numpy as np
4
5
6
7model_name = "openbmb/MiniCPM-Reranker"
8tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
9tokenizer.padding_side = "right"
10
11model = AutoModelForSequenceClassification.from_pretrained(model_name, trust_remote_code=True, torch_dtype=torch.float16).to("cuda")
12# You can also use the following code to use flash_attention_2
13# model = AutoModelForSequenceClassification.from_pretrained(model_name, trust_remote_code=True,attn_implementation="flash_attention_2", torch_dtype=torch.float16).to("cuda")
14
15model.eval()
16
17@torch.no_grad()
18def rerank(input_query, input_docs):
19 tokenized_inputs = tokenizer([[input_query, input_doc] for input_doc in input_docs], return_tensors="pt", padding=True, truncation=True, max_length=1024)
20
21 for k in tokenized_inputs:
22 tokenized_inputs [k] = tokenized_inputs[k].to("cuda")
23
24 outputs = model(**tokenized_inputs)
25 score = outputs.logits
26 return score.float().detach().cpu().numpy()
27
28queries = ["中国的首都是哪里?"]
29passages = [["beijing", "shanghai"]]
30
31INSTRUCTION = "Query: "
32queries = [INSTRUCTION + query for query in queries]
33
34scores = []
35for i in range(len(queries)):
36 print(queries[i])
37 scores.append(rerank(queries[i],passages[i]))
38
39print(np.array(scores)) # [[[-4.7460938][-8.8515625]]]1from sentence_transformers import CrossEncoder
2import torch
3
4#
5model_name = "openbmb/MiniCPM-Reranker"
6model = CrossEncoder(model_name,max_length=1024,trust_remote_code=True, automodel_args={"torch_dtype": torch.float16})
7# You can also use the following code to use flash_attention_2
8#model = CrossEncoder(model_name,max_length=1024,trust_remote_code=True, automodel_args={"attn_implementation":"flash_attention_2","torch_dtype": torch.float16})
9
10model.tokenizer.padding_side = "right"
11
12query = "中国的首都是哪里?"
13passages = [["beijing", "shanghai"]]
14
15INSTRUCTION = "Query: "
16query = INSTRUCTION + query
17
18sentence_pairs = [[query, doc] for doc in passages]
19
20scores = model.predict(sentence_pairs, convert_to_tensor=True).tolist()
21rankings = model.rank(query, passages, return_documents=True, convert_to_tensor=True)
22
23print(scores) # [0.0087432861328125, 0.00020503997802734375]
24for ranking in rankings:
25 print(f"Score: {ranking['score']:.4f}, Corpus: {ranking['text']}")
26
27# ID: 0, Score: 0.0087, Text: beijing
28# ID: 1, Score: 0.0002, Text: shanghaibge-large-zh-v1.5检索的top-100进行重排,英文对bge-large-en-v1.5检索的top-100进行重排。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 |
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 |