BGE-M3 详解:密集检索、多向量检索与稀疏检索三合一嵌入模型

BGE-M3 是一款同时支持密集检索、多向量检索与稀疏检索的多功能、多语言、多粒度文本嵌入模型。本文梳理三种检索方式的原理与权重计算示例,并给出混合检索加重排序的 RAG 实践方案。

BGE-M3 具有以下特性

  • 多功能性:能够同时执行嵌入模型的三种常见检索功能:密集检索、多向量检索和稀疏检索。
  • 多语言性:支持超过100种工作语言。
  • 多粒度:能够处理不同粒度的输入,从短句到长达8192个token的长文档。

RAG中的检索方法:混合检索 + 重排序

不同检索方法介绍

  • 密集检索:将文本映射到单一嵌入向量中,例如,DPRBGE-v1.5
  • 稀疏检索(词汇匹配):一个与词汇表大小相等的向量,其中大多数位置设为零,仅对文本中存在的令牌计算权重。例如,BM25,unicoil,和splade
  • 多向量检索:使用多个向量来表示一个文本,例如,ColBERT

    稠密检索

稠密检索使用低维、密集的向量表示文本数据,将文本嵌入到连续的向量空间中,能够捕捉语义相似性,适合处理自然语言处理(NLP)任务中的模糊查询和复杂语义关系。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from FlagEmbedding import BGEM3FlagModel

model = BGEM3FlagModel('BAAI/bge-m3',
use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation

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, # If you don't need such a long length, you can set a smaller value to speed up the encoding process.
)['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 ]]

稀疏检索

稀疏检索使用高维、稀疏的向量表示文本,其中大部分特征值为零,其计算效率高,易于解释,适合处理短文本和关键词匹配。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from FlagEmbedding import BGEM3FlagModel

model = BGEM3FlagModel('BAAI/bge-m3', use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation

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)

# you can see the weight for each token:
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}]


# compute the scores via lexical mathcing
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

多向量检索

多向量检索是一种混合方法,结合了稠密和稀疏检索的优点,使用多个向量来表示一个文档或查询。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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

加权语义相似度

计算三种检索的加权平均值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from FlagEmbedding import BGEM3FlagModel

# 填写模型路径
# VAR_PLACEHOLDER
model = BGEM3FlagModel('models--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]

# 计算混合相似度
# w[0]*dense_score + w[1]*sparse_score + w[2]*colbert_score
print(model.compute_score(sentence_pairs,
max_passage_length=128,
weights_for_different_modes=[0.4, 0.2, 0.4]))
本文结束 感谢您的阅读