Are you the author?
Views
1
GPU deploy isn't supported for this model type.
BAAI/bge-reranker-base and BAAI/bge-reranker-large, which are more powerful than embedding model. We recommend to use/fine-tune them to re-rank top-k documents returned by embedding models.bge-*-v1.5 embedding model to alleviate the issue of the similarity distribution, and enhance its retrieval ability without instruction.bge-large-*(short for BAAI General Embedding) Models, rank 1st on MTEB and C-MTEB benchmark! :tada: :tada:bge is short for BAAI general embedding.| Model | Language | Description | query instruction for retrieval [1] | |
|---|---|---|---|---|
| BAAI/bge-m3 | Multilingual | Inference Fine-tune | Multi-Functionality(dense retrieval, sparse retrieval, multi-vector(colbert)), Multi-Linguality, and Multi-Granularity(8192 tokens) | |
| BAAI/llm-embedder | English | Inference Fine-tune | a unified embedding model to support diverse retrieval augmentation needs for LLMs | See README |
| BAAI/bge-reranker-large | Chinese and English | Inference Fine-tune | a cross-encoder model which is more accurate but less efficient [2] | |
| BAAI/bge-reranker-base | Chinese and English | Inference Fine-tune | a cross-encoder model which is more accurate but less efficient [2] | |
| BAAI/bge-large-en-v1.5 | English | Inference Fine-tune | version 1.5 with more reasonable similarity distribution | Represent this sentence for searching relevant passages: |
| BAAI/bge-base-en-v1.5 | English | Inference Fine-tune | version 1.5 with more reasonable similarity distribution | Represent this sentence for searching relevant passages: |
| BAAI/bge-small-en-v1.5 | English | Inference Fine-tune | version 1.5 with more reasonable similarity distribution | Represent this sentence for searching relevant passages: |
| BAAI/bge-large-zh-v1.5 | Chinese | Inference Fine-tune | version 1.5 with more reasonable similarity distribution | 为这个句子生成表示以用于检索相关文章: |
| BAAI/bge-base-zh-v1.5 | Chinese | Inference Fine-tune | version 1.5 with more reasonable similarity distribution | 为这个句子生成表示以用于检索相关文章: |
| BAAI/bge-small-zh-v1.5 | Chinese | Inference Fine-tune | version 1.5 with more reasonable similarity distribution | 为这个句子生成表示以用于检索相关文章: |
| BAAI/bge-large-en | English | Inference Fine-tune | :trophy: rank 1st in MTEB leaderboard | Represent this sentence for searching relevant passages: |
| BAAI/bge-base-en | English | Inference Fine-tune | a base-scale model but with similar ability to bge-large-en | Represent this sentence for searching relevant passages: |
| BAAI/bge-small-en | English | Inference Fine-tune | a small-scale model but with competitive performance | Represent this sentence for searching relevant passages: |
| BAAI/bge-large-zh | Chinese | Inference Fine-tune | :trophy: rank 1st in C-MTEB benchmark | 为这个句子生成表示以用于检索相关文章: |
| BAAI/bge-base-zh | Chinese | Inference Fine-tune | a base-scale model but with similar ability to bge-large-zh | 为这个句子生成表示以用于检索相关文章: |
| BAAI/bge-small-zh | Chinese | Inference Fine-tune | a small-scale model but with competitive performance | 为这个句子生成表示以用于检索相关文章: |
bge-*-v1.5, we improve its retrieval ability when not using instruction.
No instruction only has a slight degradation in retrieval performance compared with using instruction.
So you can generate embedding without instruction in all cases for convenience.bge models with
FlagEmbedding, Sentence-Transformers, Langchain, or Huggingface Transformers.pip install -U FlagEmbedding1from FlagEmbedding import FlagModel
2sentences_1 = ["样例数据-1", "样例数据-2"]
3sentences_2 = ["样例数据-3", "样例数据-4"]
4model = FlagModel('BAAI/bge-large-zh-v1.5',
5 query_instruction_for_retrieval="为这个句子生成表示以用于检索相关文章:",
6 use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
7embeddings_1 = model.encode(sentences_1)
8embeddings_2 = model.encode(sentences_2)
9similarity = embeddings_1 @ embeddings_2.T
10print(similarity)
11
12# for s2p(short query to long passage) retrieval task, suggest to use encode_queries() which will automatically add the instruction to each query
13# corpus in retrieval task can still use encode() or encode_corpus(), since they don't need instruction
14queries = ['query_1', 'query_2']
15passages = ["样例文档-1", "样例文档-2"]
16q_embeddings = model.encode_queries(queries)
17p_embeddings = model.encode(passages)
18scores = q_embeddings @ p_embeddings.Tquery_instruction_for_retrieval, see Model List.os.environ["CUDA_VISIBLE_DEVICES"] to select specific GPUs.
You also can set os.environ["CUDA_VISIBLE_DEVICES"]="" to make all GPUs unavailable.bge models with sentence-transformers:pip install -U sentence-transformers1from sentence_transformers import SentenceTransformer
2sentences_1 = ["样例数据-1", "样例数据-2"]
3sentences_2 = ["样例数据-3", "样例数据-4"]
4model = SentenceTransformer('BAAI/bge-large-zh-v1.5')
5embeddings_1 = model.encode(sentences_1, normalize_embeddings=True)
6embeddings_2 = model.encode(sentences_2, normalize_embeddings=True)
7similarity = embeddings_1 @ embeddings_2.T
8print(similarity)1from sentence_transformers import SentenceTransformer
2queries = ['query_1', 'query_2']
3passages = ["样例文档-1", "样例文档-2"]
4instruction = "为这个句子生成表示以用于检索相关文章:"
5
6model = SentenceTransformer('BAAI/bge-large-zh-v1.5')
7q_embeddings = model.encode([instruction+q for q in queries], normalize_embeddings=True)
8p_embeddings = model.encode(passages, normalize_embeddings=True)
9scores = q_embeddings @ p_embeddings.Tbge in langchain like this:1from langchain.embeddings import HuggingFaceBgeEmbeddings
2model_name = "BAAI/bge-large-en-v1.5"
3model_kwargs = {'device': 'cuda'}
4encode_kwargs = {'normalize_embeddings': True} # set True to compute cosine similarity
5model = HuggingFaceBgeEmbeddings(
6 model_name=model_name,
7 model_kwargs=model_kwargs,
8 encode_kwargs=encode_kwargs,
9 query_instruction="为这个句子生成表示以用于检索相关文章:"
10)
11model.query_instruction = "为这个句子生成表示以用于检索相关文章:"1from transformers import AutoTokenizer, AutoModel
2import torch
3# Sentences we want sentence embeddings for
4sentences = ["样例数据-1", "样例数据-2"]
5
6# Load model from HuggingFace Hub
7tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-large-zh-v1.5')
8model = AutoModel.from_pretrained('BAAI/bge-large-zh-v1.5')
9model.eval()
10
11# Tokenize sentences
12encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
13# for s2p(short query to long passage) retrieval task, add an instruction to query (not add instruction for passages)
14# encoded_input = tokenizer([instruction + q for q in queries], padding=True, truncation=True, return_tensors='pt')
15
16# Compute token embeddings
17with torch.no_grad():
18 model_output = model(**encoded_input)
19 # Perform pooling. In this case, cls pooling.
20 sentence_embeddings = model_output[0][:, 0]
21# normalize embeddings
22sentence_embeddings = torch.nn.functional.normalize(sentence_embeddings, p=2, dim=1)
23print("Sentence embeddings:", sentence_embeddings)pip install -U FlagEmbedding1from FlagEmbedding import FlagReranker
2reranker = FlagReranker('BAAI/bge-reranker-large', use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
3
4score = reranker.compute_score(['query', 'passage'])
5print(score)
6
7scores = 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.']])
8print(scores)1import torch
2from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
4tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-reranker-large')
5model = AutoModelForSequenceClassification.from_pretrained('BAAI/bge-reranker-large')
6model.eval()
7
8pairs = [['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.']]
9with torch.no_grad():
10 inputs = tokenizer(pairs, padding=True, truncation=True, return_tensors='pt', max_length=512)
11 scores = model(**inputs, return_dict=True).logits.view(-1, ).float()
12 print(scores)1from optimum.onnxruntime import ORTModelForFeatureExtraction # type: ignore
2
3import torch
4from transformers import AutoModel, AutoTokenizer
5
6tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-small-en-v1.5')
7model = AutoModel.from_pretrained('BAAI/bge-small-en-v1.5')
8model_ort = ORTModelForFeatureExtraction.from_pretrained('BAAI/bge-small-en-v1.5', file_name="onnx/model.onnx")
9
10# Sentences we want sentence embeddings for
11sentences = ["样例数据-1", "样例数据-2"]
12
13# Tokenize sentences
14encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
15# for s2p(short query to long passage) retrieval task, add an instruction to query (not add instruction for passages)
16# encoded_input = tokenizer([instruction + q for q in queries], padding=True, truncation=True, return_tensors='pt')
17
18model_output_ort = model_ort(**encoded_input)
19# Compute token embeddings
20with torch.no_grad():
21 model_output = model(**encoded_input)
22
23# model_output and model_output_ort are identical
24device="cuda", engine="torch" with flash attention on gpu, and device="cpu", engine="optimum" for onnx inference.1import asyncio
2from infinity_emb import AsyncEmbeddingEngine, EngineArgs
3
4sentences = ["Embed this is sentence via Infinity.", "Paris is in France."]
5engine = AsyncEmbeddingEngine.from_args(
6 EngineArgs(model_name_or_path = "BAAI/bge-small-en-v1.5", device="cpu", engine="optimum" # or engine="torch"
7))
8
9async def main():
10 async with engine:
11 embeddings, usage = await engine.embed(sentences=sentences)
12asyncio.run(main())baai-general-embedding models achieve state-of-the-art performance on both MTEB and C-MTEB leaderboard!
For more details and evaluation tools see our scripts.| Model Name | Dimension | Sequence Length | Average (56) | Retrieval (15) | Clustering (11) | Pair Classification (3) | Reranking (4) | STS (10) | Summarization (1) | Classification (12) |
|---|---|---|---|---|---|---|---|---|---|---|
| BAAI/bge-large-en-v1.5 | 1024 | 512 | 64.23 | 54.29 | 46.08 | 87.12 | 60.03 | 83.11 | 31.61 | 75.97 |
| BAAI/bge-base-en-v1.5 | 768 | 512 | 63.55 | 53.25 | 45.77 | 86.55 | 58.86 | 82.4 | 31.07 | 75.53 |
| BAAI/bge-small-en-v1.5 | 384 | 512 | 62.17 | 51.68 | 43.82 | 84.92 | 58.36 | 81.59 | 30.12 | 74.14 |
| bge-large-en | 1024 | 512 | 63.98 | 53.9 | 46.98 | 85.8 | 59.48 | 81.56 | 32.06 | 76.21 |
| bge-base-en | 768 | 512 | 63.36 | 53.0 | 46.32 | 85.86 | 58.7 | 81.84 | 29.27 | 75.27 |
| gte-large | 1024 | 512 | 63.13 | 52.22 | 46.84 | 85.00 | 59.13 | 83.35 | 31.66 | 73.33 |
| gte-base | 768 | 512 | 62.39 | 51.14 | 46.2 | 84.57 | 58.61 | 82.3 | 31.17 | 73.01 |
| e5-large-v2 | 1024 | 512 | 62.25 | 50.56 | 44.49 | 86.03 | 56.61 | 82.05 | 30.19 | 75.24 |
| bge-small-en | 384 | 512 | 62.11 | 51.82 | 44.31 | 83.78 | 57.97 | 80.72 | 30.53 | 74.37 |
| instructor-xl | 768 | 512 | 61.79 | 49.26 | 44.74 | 86.62 | 57.29 | 83.06 | 32.32 | 61.79 |
| e5-base-v2 | 768 | 512 | 61.5 | 50.29 | 43.80 | 85.73 | 55.91 | 81.05 | 30.28 | 73.84 |
| gte-small | 384 | 512 | 61.36 | 49.46 | 44.89 | 83.54 | 57.7 | 82.07 | 30.42 | 72.31 |
| text-embedding-ada-002 | 1536 | 8192 | 60.99 | 49.25 | 45.9 | 84.89 | 56.32 | 80.97 | 30.8 | 70.93 |
| e5-small-v2 | 384 | 512 | 59.93 | 49.04 | 39.92 | 84.67 | 54.32 | 80.39 | 31.16 | 72.94 |
| sentence-t5-xxl | 768 | 512 | 59.51 | 42.24 | 43.72 | 85.06 | 56.42 | 82.63 | 30.08 | 73.42 |
| all-mpnet-base-v2 | 768 | 514 | 57.78 | 43.81 | 43.69 | 83.04 | 59.36 | 80.28 | 27.49 | 65.07 |
| sgpt-bloom-7b1-msmarco | 4096 | 2048 | 57.59 | 48.22 | 38.93 | 81.9 | 55.65 | 77.74 | 33.6 | 66.19 |
| Model | Embedding dimension | Avg | Retrieval | STS | PairClassification | Classification | Reranking | Clustering |
|---|---|---|---|---|---|---|---|---|
| BAAI/bge-large-zh-v1.5 | 1024 | 64.53 | 70.46 | 56.25 | 81.6 | 69.13 | 65.84 | 48.99 |
| BAAI/bge-base-zh-v1.5 | 768 | 63.13 | 69.49 | 53.72 | 79.75 | 68.07 | 65.39 | 47.53 |
| BAAI/bge-small-zh-v1.5 | 512 | 57.82 | 61.77 | 49.11 | 70.41 | 63.96 | 60.92 | 44.18 |
| BAAI/bge-large-zh | 1024 | 64.20 | 71.53 | 54.98 | 78.94 | 68.32 | 65.11 | 48.39 |
| bge-large-zh-noinstruct | 1024 | 63.53 | 70.55 | 53 | 76.77 | 68.58 | 64.91 | 50.01 |
| BAAI/bge-base-zh | 768 | 62.96 | 69.53 | 54.12 | 77.5 | 67.07 | 64.91 | 47.63 |
| multilingual-e5-large | 1024 | 58.79 | 63.66 | 48.44 | 69.89 | 67.34 | 56.00 | 48.23 |
| BAAI/bge-small-zh | 512 | 58.27 | 63.07 | 49.45 | 70.35 | 63.64 | 61.48 | 45.09 |
| m3e-base | 768 | 57.10 | 56.91 | 50.47 | 63.99 | 67.52 | 59.34 | 47.68 |
| m3e-large | 1024 | 57.05 | 54.75 | 50.42 | 64.3 | 68.2 | 59.66 | 48.88 |
| multilingual-e5-base | 768 | 55.48 | 61.63 | 46.49 | 67.07 | 65.35 | 54.35 | 40.68 |
| multilingual-e5-small | 384 | 55.38 | 59.95 | 45.27 | 66.45 | 65.85 | 53.86 | 45.26 |
| text-embedding-ada-002(OpenAI) | 1536 | 53.02 | 52.0 | 43.35 | 69.56 | 64.31 | 54.28 | 45.68 |
| luotuo | 1024 | 49.37 | 44.4 | 42.78 | 66.62 | 61 | 49.25 | 44.39 |
| text2vec-base | 768 | 47.63 | 38.79 | 43.41 | 67.41 | 62.19 | 49.45 | 37.66 |
| text2vec-large | 1024 | 47.36 | 41.94 | 44.97 | 70.86 | 60.66 | 49.16 | 30.02 |
| Model | T2Reranking | T2RerankingZh2En* | T2RerankingEn2Zh* | MMarcoReranking | CMedQAv1 | CMedQAv2 | Avg |
|---|---|---|---|---|---|---|---|
| text2vec-base-multilingual | 64.66 | 62.94 | 62.51 | 14.37 | 48.46 | 48.6 | 50.26 |
| multilingual-e5-small | 65.62 | 60.94 | 56.41 | 29.91 | 67.26 | 66.54 | 57.78 |
| multilingual-e5-large | 64.55 | 61.61 | 54.28 | 28.6 | 67.42 | 67.92 | 57.4 |
| multilingual-e5-base | 64.21 | 62.13 | 54.68 | 29.5 | 66.23 | 66.98 | 57.29 |
| m3e-base | 66.03 | 62.74 | 56.07 | 17.51 | 77.05 | 76.76 | 59.36 |
| m3e-large | 66.13 | 62.72 | 56.1 | 16.46 | 77.76 | 78.27 | 59.57 |
| bge-base-zh-v1.5 | 66.49 | 63.25 | 57.02 | 29.74 | 80.47 | 84.88 | 63.64 |
| bge-large-zh-v1.5 | 65.74 | 63.39 | 57.03 | 28.74 | 83.45 | 85.44 | 63.97 |
| BAAI/bge-reranker-base | 67.28 | 63.95 | 60.45 | 35.46 | 81.26 | 84.1 | 65.42 |
| BAAI/bge-reranker-large | 67.6 | 64.03 | 61.44 | 37.16 | 82.15 | 84.18 | 66.09 |
@misc{bge_embedding,
title={C-Pack: Packaged Resources To Advance General Chinese Embedding},
author={Shitao Xiao and Zheng Liu and Peitian Zhang and Niklas Muennighoff},
year={2023},
eprint={2309.07597},
archivePrefix={arXiv},
primaryClass={cs.CL}
}