Views
No views yet
Instruction: {{ instruction }} Query: {{ query }}Instruction: 为这个医学问题检索相关回答。Query: 咽喉癌的成因是什么?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.Query: {{ query }}instructions.json,其他测试不使用指令。文档侧直接输入文档原文。instructions.json. For other evaluations, we do not use instructions. On the document side, we directly use the bare document as the input.transformers==4.37.21
2from transformers import AutoModel, AutoTokenizer
3import torch
4import torch.nn.functional as F
5
6model_name = "openbmb/MiniCPM-Embedding"
7tokenizer = AutoTokenizer.from_pretrained(model_name)
8model = AutoModel.from_pretrained(model_name, trust_remote_code=True, torch_dtype=torch.float16).to("cuda")
9# You can also use the following line to enable the Flash Attention 2 implementation
10# model = AutoModel.from_pretrained(model_name, trust_remote_code=True, attn_implementation="flash_attention_2", torch_dtype=torch.float16).to("cuda")
11model.eval()
12
13# 由于在 `model.forward` 中缩放了最终隐层表示,此处的 mean pooling 实际上起到了 weighted mean pooling 的作用
14# As we scale hidden states in `model.forward`, mean pooling here actually works as weighted mean pooling
15def mean_pooling(hidden, attention_mask):
16 s = torch.sum(hidden * attention_mask.unsqueeze(-1).float(), dim=1)
17 d = attention_mask.sum(dim=1, keepdim=True).float()
18 reps = s / d
19 return reps
20
21@torch.no_grad()
22def encode(input_texts):
23 batch_dict = tokenizer(input_texts, max_length=512, padding=True, truncation=True, return_tensors='pt', return_attention_mask=True).to("cuda")
24
25 outputs = model(**batch_dict)
26 attention_mask = batch_dict["attention_mask"]
27 hidden = outputs.last_hidden_state
28
29 reps = mean_pooling(hidden, attention_mask)
30 embeddings = F.normalize(reps, p=2, dim=1).detach().cpu().numpy()
31 return embeddings
32
33queries = ["中国的首都是哪里?"]
34passages = ["beijing", "shanghai"]
35
36
37INSTRUCTION = "Query: "
38queries = [INSTRUCTION + query for query in queries]
39
40embeddings_query = encode(queries)
41embeddings_doc = encode(passages)
42
43scores = (embeddings_query @ embeddings_doc.T)
44print(scores.tolist()) # [[0.3535913825035095, 0.18596848845481873]]1import torch
2from sentence_transformers import SentenceTransformer
3
4model_name = "openbmb/MiniCPM-Embedding"
5model = SentenceTransformer(model_name, trust_remote_code=True, model_kwargs={ "torch_dtype": torch.float16})
6# You can also use the following line to enable the Flash Attention 2 implementation
7# model = SentenceTransformer(model_name, trust_remote_code=True, attn_implementation="flash_attention_2", model_kwargs={ "torch_dtype": torch.float16})
8
9queries = ["中国的首都是哪里?"]
10passages = ["beijing", "shanghai"]
11
12INSTRUCTION = "Query: "
13
14embeddings_query = model.encode(queries, prompt=INSTRUCTION)
15embeddings_doc = model.encode(passages)
16
17scores = (embeddings_query @ embeddings_doc.T)
18print(scores.tolist()) # [[0.35365450382232666, 0.18592746555805206]]| 模型 Model | C-MTEB/Retrieval (NDCG@10) | BEIR (NDCG@10) |
|---|---|---|
| bge-large-zh-v1.5 | 70.46 | - |
| gte-large-zh | 72.49 | - |
| Zhihui_LLM_Embedding | 76.74 | |
| bge-large-en-v1.5 | - | 54.29 |
| gte-en-large-v1.5 | - | 57.91 |
| NV-Retriever-v1 | - | 60.9 |
| bge-en-icl | - | 62.16 |
| NV-Embed-v2 | - | 62.65 |
| me5-large | 63.66 | 51.43 |
| bge-m3(Dense) | 65.43 | 48.82 |
| gte-multilingual-base(Dense) | 71.95 | 51.08 |
| gte-Qwen2-1.5B-instruct | 71.86 | 58.29 |
| gte-Qwen2-7B-instruct | 76.03 | 60.25 |
| bge-multilingual-gemma2 | 73.73 | 59.24 |
| MiniCPM-Embedding | 76.76 | 58.56 |
| MiniCPM-Embedding+MiniCPM-Reranker | 77.08 | 61.61 |
| 模型 Model | MKQA En-Zh_CN (Recall@20) | NeuCLIR22 (NDCG@10) | NeuCLIR23 (NDCG@10) |
|---|---|---|---|
| me5-large | 44.3 | 9.01 | 25.33 |
| bge-m3(Dense) | 66.4 | 30.49 | 41.09 |
| gte-multilingual-base(Dense) | 68.2 | 39.46 | 45.86 |
| gte-Qwen2-1.5B-instruct | 68.52 | 49.11 | 45.05 |
| gte-Qwen2-7B-instruct | 68.27 | 49.14 | 49.6 |
| MiniCPM-Embedding | 72.95 | 52.65 | 49.95 |
| MiniCPM-Embedding+MiniCPM-Reranker | 74.33 | 53.21 | 54.12 |