模型概述
模型特點
模型能力
使用案例
🚀 BGE-M3
BGE-M3是一個具備多功能性、多語言性和多粒度性的文本嵌入模型。它能同時執行密集檢索、多向量檢索和稀疏檢索三種常見的檢索功能,支持超100種工作語言,還能處理從短句到長達8192個標記的長文檔等不同粒度的輸入。
更多詳情請參考我們的GitHub倉庫:FlagEmbedding
🚀 快速開始
安裝
git clone https://github.com/FlagOpen/FlagEmbedding.git
cd FlagEmbedding
pip install -e .
或者:
pip install -U FlagEmbedding
生成文本嵌入
密集嵌入
from FlagEmbedding import BGEM3FlagModel
model = BGEM3FlagModel('BAAI/bge-m3',
use_fp16=True) # 設置use_fp16為True可在輕微降低性能的情況下加快計算速度
sentences_1 = ["What is BGE M3?", "Defination of BM25"]
sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
"BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
embeddings_1 = model.encode(sentences_1,
batch_size=12,
max_length=8192, # 如果不需要這麼長的長度,可以設置較小的值以加快編碼過程
)['dense_vecs']
embeddings_2 = model.encode(sentences_2)['dense_vecs']
similarity = embeddings_1 @ embeddings_2.T
print(similarity)
# [[0.6265, 0.3477], [0.3499, 0.678 ]]
你也可以使用sentence-transformers
和huggingface transformers
來生成密集嵌入。詳情請參考baai_general_embedding。
稀疏嵌入(詞法權重)
from FlagEmbedding import BGEM3FlagModel
model = BGEM3FlagModel('BAAI/bge-m3', use_fp16=True) # 設置use_fp16為True可在輕微降低性能的情況下加快計算速度
sentences_1 = ["What is BGE M3?", "Defination of BM25"]
sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
"BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
output_1 = model.encode(sentences_1, return_dense=True, return_sparse=True, return_colbert_vecs=False)
output_2 = model.encode(sentences_2, return_dense=True, return_sparse=True, return_colbert_vecs=False)
# 你可以查看每個標記的權重:
print(model.convert_id_to_token(output_1['lexical_weights']))
# [{'What': 0.08356, 'is': 0.0814, 'B': 0.1296, 'GE': 0.252, 'M': 0.1702, '3': 0.2695, '?': 0.04092},
# {'De': 0.05005, 'fin': 0.1368, 'ation': 0.04498, 'of': 0.0633, 'BM': 0.2515, '25': 0.3335}]
# 通過詞法匹配計算分數
lexical_scores = model.compute_lexical_matching_score(output_1['lexical_weights'][0], output_2['lexical_weights'][0])
print(lexical_scores)
# 0.19554901123046875
print(model.compute_lexical_matching_score(output_1['lexical_weights'][0], output_1['lexical_weights'][1]))
# 0.0
多向量(ColBERT)
from FlagEmbedding import BGEM3FlagModel
model = BGEM3FlagModel('BAAI/bge-m3', use_fp16=True)
sentences_1 = ["What is BGE M3?", "Defination of BM25"]
sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
"BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
output_1 = model.encode(sentences_1, return_dense=True, return_sparse=True, return_colbert_vecs=True)
output_2 = model.encode(sentences_2, return_dense=True, return_sparse=True, return_colbert_vecs=True)
print(model.colbert_score(output_1['colbert_vecs'][0], output_2['colbert_vecs'][0]))
print(model.colbert_score(output_1['colbert_vecs'][0], output_2['colbert_vecs'][1]))
# 0.7797
# 0.4620
計算文本對的分數
輸入一個文本對列表,你可以得到通過不同方法計算的分數。
from FlagEmbedding import BGEM3FlagModel
model = BGEM3FlagModel('BAAI/bge-m3', use_fp16=True)
sentences_1 = ["What is BGE M3?", "Defination of BM25"]
sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
"BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
sentence_pairs = [[i,j] for i in sentences_1 for j in sentences_2]
print(model.compute_score(sentence_pairs,
max_passage_length=128, # 較小的最大長度會導致較低的延遲
weights_for_different_modes=[0.4, 0.2, 0.4])) # weights_for_different_modes(w)用於進行加權求和:w[0]*dense_score + w[1]*sparse_score + w[2]*colbert_score
# {
# 'colbert': [0.7796499729156494, 0.4621465802192688, 0.4523794651031494, 0.7898575067520142],
# 'sparse': [0.195556640625, 0.00879669189453125, 0.0, 0.1802978515625],
# 'dense': [0.6259765625, 0.347412109375, 0.349853515625, 0.67822265625],
# 'sparse+dense': [0.482503205537796, 0.23454029858112335, 0.2332356721162796, 0.5122477412223816],
# 'colbert+sparse+dense': [0.6013619303703308, 0.3255828022956848, 0.32089319825172424, 0.6232916116714478]
# }
✨ 主要特性
- 多功能性:能夠同時執行嵌入模型的三種常見檢索功能,即密集檢索、多向量檢索和稀疏檢索。
- 多語言性:支持超過100種工作語言。
- 多粒度性:能夠處理不同粒度的輸入,從短句到長達8192個標記的長文檔。
📦 安裝指南
git clone https://github.com/FlagOpen/FlagEmbedding.git
cd FlagEmbedding
pip install -e .
或者:
pip install -U FlagEmbedding
📚 詳細文檔
RAG檢索管道的建議
我們建議使用以下管道:混合檢索 + 重排。
- 混合檢索:利用各種方法的優勢,提供更高的準確性和更強的泛化能力。一個經典的例子是同時使用嵌入檢索和BM25算法。現在,你可以嘗試使用BGE-M3,它支持嵌入和稀疏檢索。這允許你在生成密集嵌入時無需任何額外成本即可獲得標記權重(類似於BM25)。要使用混合檢索,你可以參考Vespa和Milvus。
- 重排:作為交叉編碼器模型,重排器比雙編碼器嵌入模型具有更高的準確性。在檢索後使用重排模型(例如,bge-reranker,bge-reranker-v2)可以進一步篩選所選文本。
新聞
- 2024/7/1:我們更新了BGE-M3的MIRACL評估結果。要復現新結果,你可以參考:bge-m3_miracl_2cr。我們還在arXiv上更新了我們的論文。
詳情
之前的測試結果較低是因為我們錯誤地從搜索結果中刪除了與查詢具有相同ID的段落。糾正這個錯誤後,BGE-M3在MIRACL上的整體性能高於之前的結果,但實驗結論保持不變。其他結果不受此錯誤影響。要復現之前較低的結果,你需要在使用pyserini.search.faiss
或pyserini.search.lucene
搜索段落時添加--remove-query
參數。
規格
模型
模型名稱 | 維度 | 序列長度 | 介紹 |
---|---|---|---|
BAAI/bge-m3 | 1024 | 8192 | 多語言;從bge-m3-unsupervised進行統一微調(密集、稀疏和colbert) |
BAAI/bge-m3-unsupervised | 1024 | 8192 | 多語言;從bge-m3-retromae進行對比學習 |
BAAI/bge-m3-retromae | -- | 8192 | 多語言;將xlm-roberta的最大長度擴展到8192,並通過retromae進一步預訓練 |
BAAI/bge-large-en-v1.5 | 1024 | 512 | 英語模型 |
BAAI/bge-base-en-v1.5 | 768 | 512 | 英語模型 |
BAAI/bge-small-en-v1.5 | 384 | 512 | 英語模型 |
數據
數據集 | 介紹 |
---|---|
MLDR | 文檔檢索數據集,涵蓋13種語言 |
bge-m3-data | bge-m3使用的微調數據 |
FAQ
1. 不同檢索方法的介紹
- 密集檢索:將文本映射到單個嵌入,例如DPR、BGE-v1.5。
- 稀疏檢索(詞法匹配):一個大小等於詞彙表的向量,大多數位置設置為零,僅為文本中存在的標記計算權重。例如BM25、unicoil和splade。
- 多向量檢索:使用多個向量表示一個文本,例如ColBERT。
2. 如何在其他項目中使用BGE-M3?
對於嵌入檢索,你可以使用與BGE相同的方法來使用BGE-M3模型。唯一的區別是BGE-M3模型不再需要在查詢中添加指令。
3. 如何微調bge-M3模型?
你可以按照這個示例中的常見方法來微調密集嵌入。
如果你想微調m3的所有嵌入函數(密集、稀疏和colbert),你可以參考統一微調示例。
🔧 技術細節
評估
開源社區的基準測試
BGE-M3模型在這個基準測試中表現出色(OAI是OpenAI的縮寫)。更多詳情,請參考文章和GitHub倉庫。
我們的結果
-
多語言(Miracl數據集)
-
跨語言(MKQA數據集)
-
長文檔檢索
- MLDR:
請注意,MLDR是我們通過大語言模型構建的一個文檔檢索數據集,涵蓋13種語言,包括測試集、驗證集和訓練集。我們利用MLDR的訓練集來增強模型的長文檔檢索能力。因此,將基線與
Dense w.o.long
(不使用長文檔數據集進行微調)進行比較更為公平。此外,這個長文檔檢索數據集將開源,以解決當前開源多語言長文本檢索數據集的缺乏問題。我們相信這些數據將有助於開源社區訓練文檔檢索模型。 - NarritiveQA:
- MLDR:
-
與BM25的比較 我們使用Pyserini實現了BM25,測試結果可以通過這個腳本復現。我們使用兩種不同的分詞器測試了BM25:一種使用Lucene Analyzer,另一種使用與M3相同的分詞器(即xlm-roberta的分詞器)。結果表明,BM25仍然是一個有競爭力的基線,特別是在長文檔檢索方面。
訓練
- 自我知識蒸餾:將不同檢索模式的多個輸出組合作為獎勵信號,以提高單模式的性能(特別是稀疏檢索和多向量(colbert)檢索)。
- 高效批處理:提高在長文本上微調時的效率。小批量策略簡單但有效,也可用於微調大型嵌入模型。
- MCLS:一種在不進行微調的情況下提高長文本性能的簡單方法。如果你沒有足夠的資源對長文本進行模型微調,這種方法很有用。
更多詳情請參考我們的報告。
📄 許可證
本項目採用MIT許可證。
致謝
感謝Miracl、MKQA、NarritiveQA等開源數據集的作者。感謝Tevatron、Pyserini等開源庫。
引用
如果你覺得這個倉庫有用,請考慮給它加星 :star: 並引用:
@misc{bge-m3,
title={BGE M3-Embedding: Multi-Lingual, Multi-Functionality, Multi-Granularity Text Embeddings Through Self-Knowledge Distillation},
author={Jianlv Chen and Shitao Xiao and Peitian Zhang and Kun Luo and Defu Lian and Zheng Liu},
year={2024},
eprint={2402.03216},
archivePrefix={arXiv},
primaryClass={cs.CL}
}







