Shitao commited on
Commit
9ebc0ea
1 Parent(s): 1be37a7

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +252 -0
README.md CHANGED
@@ -1,3 +1,255 @@
1
  ---
 
 
2
  license: mit
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ tags:
3
+ - feature-extraction
4
  license: mit
5
  ---
6
+
7
+ For more details please refer to our github repo: https://github.com/FlagOpen/FlagEmbedding
8
+
9
+ # BGE-M3
10
+ In this project, we introduce BGE-M3, which is distinguished for its versatility in Multi-Functionality, Multi-Linguality, and Multi-Granularity.
11
+ - Multi-Functionality: It can simultaneously perform the three common retrieval functionalities of embedding model: dense retrieval, multi-vector retrieval, and sparse retrieval.
12
+ - Multi-Linguality: It can support more than 100 working languages.
13
+ - Multi-Granularity: It is able to process inputs of different granularities, spanning from short sentences to long documents of up to 8192 tokens.
14
+
15
+ **Some suggestions for retrieval pipeline in RAG:**
16
+ We recommend to use following pipeline: hybrid retrieval + re-ranking.
17
+ - Hybrid retrieval leverages the strengths of various methods, offering higher accuracy and stronger generalization capabilities.
18
+ A classic example: using both embedding retrieval and the BM25 algorithm.
19
+ Now, you can try to use BGE-M3, which supports both embedding and sparse retrieval.
20
+ This allows you to obtain token weights (similar to the BM25) without any additional cost when generate dense embeddings.
21
+ - As cross-encoder models, re-ranker demonstrates higher accuracy than bi-encoder embedding model.
22
+ Utilizing the re-ranking model (e.g., [bge-reranker](https://github.com/FlagOpen/FlagEmbedding/tree/master/FlagEmbedding/reranker), [cohere-reranker](https://txt.cohere.com/rerank/)) after retrieval can further filter the selected text.
23
+
24
+
25
+ ## News:
26
+ - 2/1/2024: **Thanks for the excellent tool from Vespa.** You can easily use multiple modes of BGE-M3 following this [notebook](https://github.com/vespa-engine/pyvespa/blob/master/docs/sphinx/source/examples/mother-of-all-embedding-models-cloud.ipynb)
27
+
28
+
29
+ ## Specs
30
+
31
+ - Model
32
+ | Model Name | Dimension | Sequence Length | Introduction |
33
+ |:----:|:---:|:---:|:---:|
34
+ | [BAAI/bge-m3](https://huggingface.co/BAAI/bge-m3) | 1024 | 8192 | multilingual; unified fine-tuning (dense, sparse, and colbert) from bge-m3-unsupervised|
35
+ | [BAAI/bge-m3-unsupervised](https://huggingface.co/BAAI/bge-m3-unsupervised) | 1024 | 8192 | multilingual; contrastive learning from bge-m3-retromae |
36
+ | [BAAI/bge-m3-retromae](https://huggingface.co/BAAI/bge-m3-retromae) | -- | 8192 | multilingual; extend the max_length of [xlm-roberta](https://huggingface.co/FacebookAI/xlm-roberta-large) to 8192 and further pretrained via [retromae](https://github.com/staoxiao/RetroMAE)|
37
+ | [BAAI/bge-large-en-v1.5](https://huggingface.co/BAAI/bge-large-en-v1.5) | 1024 | 512 | English model |
38
+ | [BAAI/bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | 768 | 512 | English model |
39
+ | [BAAI/bge-small-en-v1.5](https://huggingface.co/BAAI/bge-small-en-v1.5) | 384 | 512 | English model |
40
+
41
+ - Data
42
+
43
+ | Dataset | Introduction |
44
+ |:----:|:---:|
45
+ | [MLDR](https://huggingface.co/datasets/Shitao/MLDR) | Docuemtn Retrieval Dataset, covering 13 languages|
46
+
47
+
48
+
49
+ ## FAQ
50
+
51
+ **1. Introduction for different retrieval methods**
52
+
53
+ - Dense retrieval: map the text into a single embedding, e.g., [DPR](https://arxiv.org/abs/2004.04906), [BGE-v1.5](https://github.com/FlagOpen/FlagEmbedding)
54
+ - Sparse retrieval (lexical matching): a vector of size equal to the vocabulary, with the majority of positions set to zero, calculating a weight only for tokens present in the text. e.g., BM25, [unicoil](https://arxiv.org/pdf/2106.14807.pdf), and [splade](https://arxiv.org/abs/2107.05720)
55
+ - Multi-vector retrieval: use multiple vectors to represent a text, e.g., [ColBERT](https://arxiv.org/abs/2004.12832).
56
+
57
+
58
+ **2. Comparison with BGE-v1.5 and other monolingual models**
59
+
60
+ BGE-M3 is a multilingual model, and its ability in monolingual embedding retrieval may not surpass models specifically designed for single languages.
61
+ However, we still recommend trying BGE-M3 because of its versatility (support for multiple languages and long texts).
62
+ Moreover, it can simultaneously generate multiple representations, and using them together can enhance accuracy and generalization,
63
+ unlike most existing models that can only perform dense retrieval.
64
+
65
+ In the open-source community, there are many excellent models (e.g., jina-embedding, colbert, e5, etc),
66
+ and users can choose a model that suits their specific needs based on practical considerations,
67
+ such as whether to require multilingual or cross-language support, and whether to process long texts.
68
+
69
+ **3. How to use BGE-M3 in other projects?**
70
+
71
+ For embedding retrieval, you can employ the BGE-M3 model using the same approach as BGE.
72
+ The only difference is that the BGE-M3 model no longer requires adding instructions to the queries.
73
+ For sparse retrieval methods, most open-source libraries currently do not support direct utilization of the BGE-M3 model.
74
+ Contributions from the community are welcome.
75
+
76
+
77
+ **4. How to fine-tune bge-M3 model?**
78
+
79
+ You can follow the common in this [example](https://github.com/FlagOpen/FlagEmbedding/tree/master/examples/finetune)
80
+ to fine-tune the dense embedding.
81
+
82
+ Our code and data for unified fine-tuning (dense, sparse, and multi-vectors) will be released.
83
+
84
+
85
+
86
+
87
+ ## Usage
88
+
89
+ Install:
90
+ ```
91
+ git clone https://github.com/FlagOpen/FlagEmbedding.git
92
+ cd FlagEmbedding
93
+ pip install -e .
94
+ ```
95
+ or:
96
+ ```
97
+ pip install -U FlagEmbedding
98
+ ```
99
+
100
+
101
+
102
+ ### Generate Embedding for text
103
+
104
+ - Dense Embedding
105
+ ```python
106
+ from FlagEmbedding import BGEM3FlagModel
107
+
108
+ model = BGEM3FlagModel('BAAI/bge-m3',
109
+ use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
110
+
111
+ sentences_1 = ["What is BGE M3?", "Defination of BM25"]
112
+ sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
113
+ "BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
114
+
115
+ embeddings_1 = model.encode(sentences_1,
116
+ batch_size=12,
117
+ max_length=8192, # If you don't need such a long length, you can set a smaller value to speed up the encoding process.
118
+ )['dense_vecs']
119
+ embeddings_2 = model.encode(sentences_2)['dense_vecs']
120
+ similarity = embeddings_1 @ embeddings_2.T
121
+ print(similarity)
122
+ # [[0.6265, 0.3477], [0.3499, 0.678 ]]
123
+ ```
124
+ You also can use sentence-transformers and huggingface transformers to generate dense embeddings.
125
+ Refer to [baai_general_embedding](https://github.com/FlagOpen/FlagEmbedding/tree/master/FlagEmbedding/baai_general_embedding#usage) for details.
126
+
127
+
128
+ - Sparse Embedding (Lexical Weight)
129
+ ```python
130
+ from FlagEmbedding import BGEM3FlagModel
131
+
132
+ model = BGEM3FlagModel('BAAI/bge-m3', use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
133
+
134
+ sentences_1 = ["What is BGE M3?", "Defination of BM25"]
135
+ sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
136
+ "BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
137
+
138
+ output_1 = model.encode(sentences_1, return_dense=True, return_sparse=True, return_colbert_vecs=False)
139
+ output_2 = model.encode(sentences_2, return_dense=True, return_sparse=True, return_colbert_vecs=False)
140
+
141
+ # you can see the weight for each token:
142
+ print(model.convert_id_to_token(output_1['lexical_weights']))
143
+ # [{'What': 0.08356, 'is': 0.0814, 'B': 0.1296, 'GE': 0.252, 'M': 0.1702, '3': 0.2695, '?': 0.04092},
144
+ # {'De': 0.05005, 'fin': 0.1368, 'ation': 0.04498, 'of': 0.0633, 'BM': 0.2515, '25': 0.3335}]
145
+
146
+
147
+ # compute the scores via lexical mathcing
148
+ lexical_scores = model.compute_lexical_matching_score(output_1['lexical_weights'][0], output_2['lexical_weights'][0])
149
+ print(lexical_scores)
150
+ # 0.19554901123046875
151
+
152
+ print(model.compute_lexical_matching_score(output_1['lexical_weights'][0], output_1['lexical_weights'][1]))
153
+ # 0.0
154
+ ```
155
+
156
+ - Multi-Vector (ColBERT)
157
+ ```python
158
+ from FlagEmbedding import BGEM3FlagModel
159
+
160
+ model = BGEM3FlagModel('BAAI/bge-m3', use_fp16=True)
161
+
162
+ sentences_1 = ["What is BGE M3?", "Defination of BM25"]
163
+ sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
164
+ "BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
165
+
166
+ output_1 = model.encode(sentences_1, return_dense=True, return_sparse=True, return_colbert_vecs=True)
167
+ output_2 = model.encode(sentences_2, return_dense=True, return_sparse=True, return_colbert_vecs=True)
168
+
169
+ print(model.colbert_score(output_1['colbert_vecs'][0], output_2['colbert_vecs'][0]))
170
+ print(model.colbert_score(output_1['colbert_vecs'][0], output_2['colbert_vecs'][1]))
171
+ # 0.7797
172
+ # 0.4620
173
+ ```
174
+
175
+
176
+ ### Compute score for text pairs
177
+ Input a list of text pairs, you can get the scores computed by different methods.
178
+ ```python
179
+ from FlagEmbedding import BGEM3FlagModel
180
+
181
+ model = BGEM3FlagModel('BAAI/bge-m3', use_fp16=True)
182
+
183
+ sentences_1 = ["What is BGE M3?", "Defination of BM25"]
184
+ sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
185
+ "BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
186
+
187
+ sentence_pairs = [[i,j] for i in sentences_1 for j in sentences_2]
188
+
189
+ print(model.compute_score(sentence_pairs,
190
+ max_passage_length=128, # a smaller max length leads to a lower latency
191
+ weights_for_different_modes=[0.4, 0.2, 0.4])) # weights_for_different_modes(w) is used to do weighted sum: w[0]*dense_score + w[1]*sparse_score + w[2]*colbert_score
192
+
193
+ # {
194
+ # 'colbert': [0.7796499729156494, 0.4621465802192688, 0.4523794651031494, 0.7898575067520142],
195
+ # 'sparse': [0.195556640625, 0.00879669189453125, 0.0, 0.1802978515625],
196
+ # 'dense': [0.6259765625, 0.347412109375, 0.349853515625, 0.67822265625],
197
+ # 'sparse+dense': [0.482503205537796, 0.23454029858112335, 0.2332356721162796, 0.5122477412223816],
198
+ # 'colbert+sparse+dense': [0.6013619303703308, 0.3255828022956848, 0.32089319825172424, 0.6232916116714478]
199
+ # }
200
+ ```
201
+
202
+
203
+
204
+
205
+ ## Evaluation
206
+
207
+ - Multilingual (Miracl dataset)
208
+
209
+ ![avatar](./imgs/miracl.jpg)
210
+
211
+ - Cross-lingual (MKQA dataset)
212
+
213
+ ![avatar](./imgs/mkqa.jpg)
214
+
215
+ - Long Document Retrieval
216
+ - MLDR:
217
+ ![avatar](./imgs/long.jpg)
218
+ Please note that MLDR is a document retrieval dataset we constructed via LLM,
219
+ covering 13 languages, including test set, validation set, and training set.
220
+ We utilized the training set from MLDR to enhance the model's long document retrieval capabilities.
221
+ Therefore, comparing baseline with `Dense w.o.long`(fine-tuning without long document dataset) is more equitable.
222
+ Additionally, this long document retrieval dataset will be open-sourced to address the current lack of open-source multilingual long text retrieval datasets.
223
+ We believe that this data will be helpful for the open-source community in training document retrieval models.
224
+
225
+ - NarritiveQA:
226
+ ![avatar](./imgs/nqa.jpg)
227
+
228
+
229
+ ## Training
230
+ - Self-knowledge Distillation: combining multiple outputs from different
231
+ retrieval modes as reward signal to enhance the performance of single mode(especially for sparse retrieval and multi-vec(colbert) retrival)
232
+ - Efficient Batching: Improve the efficiency when fine-tuning on long text.
233
+ The small-batch strategy is simple but effective, which also can used to fine-tune large embedding model.
234
+ - MCLS: A simple method to improve the performance on long text without fine-tuning.
235
+ If you have no enough resource to fine-tuning model with long text, the method is useful.
236
+
237
+ Refer to our [report](https://github.com/FlagOpen/FlagEmbedding/blob/master/FlagEmbedding/BGE_M3/BGE_M3.pdf) for more details.
238
+
239
+ **The fine-tuning codes and datasets will be open-sourced in the near future.**
240
+
241
+
242
+
243
+ ## Acknowledgement
244
+
245
+ Thanks to the authors of open-sourced datasets, including Miracl, MKQA, NarritiveQA, etc.
246
+ Thanks to the open-sourced libraries like [Tevatron](https://github.com/texttron/tevatron), [pyserini](https://github.com/castorini/pyserini).
247
+
248
+
249
+ ## Citation
250
+
251
+ If you find this repository useful, please consider giving a star :star: and a citation
252
+
253
+ ```
254
+
255
+ ```