Shitao commited on
Commit
64754cb
1 Parent(s): aad27e8

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +175 -23
README.md CHANGED
@@ -7,50 +7,202 @@ tags:
7
 
8
  ---
9
 
10
- # {MODEL_NAME}
 
 
 
 
11
 
12
- This is a [sentence-transformers](https://www.SBERT.net) model: It maps sentences & paragraphs to a 1024 dimensional dense vector space and can be used for tasks like clustering or semantic search.
 
 
 
 
 
 
 
13
 
14
- <!--- Describe your model here -->
15
 
16
- ## Usage (Sentence-Transformers)
17
 
18
- Using this model becomes easy when you have [sentence-transformers](https://www.SBERT.net) installed:
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  ```
21
- pip install -U sentence-transformers
 
 
22
  ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- Then you can use the model like this:
 
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  ```python
27
- from sentence_transformers import SentenceTransformer
28
- sentences = ["This is an example sentence", "Each sentence is converted"]
 
 
 
 
 
29
 
30
- model = SentenceTransformer('{MODEL_NAME}')
31
- embeddings = model.encode(sentences)
32
- print(embeddings)
 
 
 
 
33
  ```
34
 
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- ## Evaluation Results
38
 
39
- <!--- Describe how your model was evaluated -->
40
 
41
- For an automated evaluation of this model, see the *Sentence Embeddings Benchmark*: [https://seb.sbert.net](https://seb.sbert.net?model_name={MODEL_NAME})
42
 
 
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
- ## Full Model Architecture
46
  ```
47
- SentenceTransformer(
48
- (0): Transformer({'max_seq_length': 8192, 'do_lower_case': False}) with Transformer model: XLMRobertaModel
49
- (1): Pooling({'word_embedding_dimension': 1024, 'pooling_mode_cls_token': True, 'pooling_mode_mean_tokens': False, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False})
50
- (2): Normalize()
51
- )
52
  ```
53
 
54
- ## Citing & Authors
55
 
56
- <!--- Describe where people can find more information -->
 
7
 
8
  ---
9
 
10
+ # BGE-M3
11
+ In this project, we introduce BGE-M3, which is distinguished for its versatility in Multi-Functionality, Multi-Linguality, and Multi-Granularity.
12
+ - Multi-Functionality: It can simultaneously perform the three common retrieval functionalities of embedding model: dense retrieval, multi-vector retrieval, and sparse retrieval.
13
+ - Multi-Linguality: It can support more than 100 working languages.
14
+ - Multi-Granularity: It is able to process inputs of different granularities, spanning from short sentences to long documents of up to 8192 tokens.
15
 
16
+ **Some suggestions for retrieval pipeline in RAG:**
17
+ We recommend to use following pipeline: hybrid retrieval + re-ranking.
18
+ - Hybrid retrieval leverages the strengths of various methods, offering higher accuracy and stronger generalization capabilities.
19
+ A classic example: using both embedding retrieval and the BM25 algorithm.
20
+ Now, you can try to use BGE-M3, which supports both embedding and sparse retrieval.
21
+ This allows you to obtain token weights (similar to the BM25) without any additional cost when generate dense embeddings.
22
+ - As cross-encoder models, re-ranker demonstrates higher accuracy than bi-encoder embedding model.
23
+ 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.
24
 
 
25
 
26
+ ## FAQ
27
 
28
+ **1. Introduction for different retrieval methods**
29
 
30
+ - 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)
31
+ - 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)
32
+ - Multi-vector retrieval: use multiple vectors to represent a text, e.g., [ColBERT](https://arxiv.org/abs/2004.12832).
33
+
34
+ **2. How to use BGE-M3 in other projects?**
35
+
36
+ For embedding retrieval, you can employ the BGE-M3 model using the same approach as BGE.
37
+ The only difference is that the BGE-M3 model no longer requires adding instructions to the queries.
38
+ For sparse retrieval methods, most open-source libraries currently do not support direct utilization of the BGE-M3 model.
39
+ Contributions from the community are welcome.
40
+
41
+
42
+ **3. How to fine-tune bge-M3 model?**
43
+
44
+ You can follow the common in this [example](https://github.com/FlagOpen/FlagEmbedding/tree/master/examples/finetune)
45
+ to fine-tune the dense embedding.
46
+
47
+ Our code and data for unified fine-tuning (dense, sparse, and multi-vectors) will be released.
48
+
49
+
50
+
51
+
52
+ ## Usage
53
+
54
+ Install:
55
  ```
56
+ git clone https://github.com/FlagOpen/FlagEmbedding.git
57
+ cd FlagEmbedding
58
+ pip install -e .
59
  ```
60
+ or:
61
+ ```
62
+ pip install -U FlagEmbedding
63
+ ```
64
+
65
+
66
+
67
+ ### Generate Embedding for text
68
+
69
+ - Dense Embedding
70
+ ```python
71
+ from FlagEmbedding import BGEM3FlagModel
72
+
73
+ model = BGEM3FlagModel('BAAI/bge-m3', use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
74
+
75
+ sentences_1 = ["What is BGE M3?", "Defination of BM25"]
76
+ sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
77
+ "BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
78
+
79
+ embeddings_1 = model.encode(sentences_1)['dense_vecs']
80
+ embeddings_2 = model.encode(sentences_2)['dense_vecs']
81
+ similarity = embeddings_1 @ embeddings_2.T
82
+ print(similarity)
83
+ # [[0.6265, 0.3477], [0.3499, 0.678 ]]
84
+ ```
85
+ You also can use sentence-transformers and huggingface transformers to generate dense embeddings.
86
+ Refer to [baai_general_embedding](https://github.com/FlagOpen/FlagEmbedding/tree/master/FlagEmbedding/baai_general_embedding#usage) for details.
87
+
88
+
89
+ - Sparse Embedding (Lexical Weight)
90
+ ```python
91
+ from FlagEmbedding import BGEM3FlagModel
92
+
93
+ model = BGEM3FlagModel('BAAI/bge-m3', use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
94
+
95
+ sentences_1 = ["What is BGE M3?", "Defination of BM25"]
96
+ sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
97
+ "BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
98
 
99
+ output_1 = model.encode(sentences_1, return_dense=True, return_sparse=True, return_colbert_vecs=False)
100
+ output_2 = model.encode(sentences_2, return_dense=True, return_sparse=True, return_colbert_vecs=False)
101
 
102
+ # you can see the weight for each token:
103
+ print(model.convert_id_to_token(output_1['lexical_weights']))
104
+ # [{'What': 0.08356, 'is': 0.0814, 'B': 0.1296, 'GE': 0.252, 'M': 0.1702, '3': 0.2695, '?': 0.04092},
105
+ # {'De': 0.05005, 'fin': 0.1368, 'ation': 0.04498, 'of': 0.0633, 'BM': 0.2515, '25': 0.3335}]
106
+
107
+
108
+ # compute the scores via lexical mathcing
109
+ lexical_scores = model.compute_lexical_matching_score(output_1['lexical_weights'][0], output_2['lexical_weights'][0])
110
+ print(lexical_scores)
111
+ # 0.19554901123046875
112
+
113
+ print(model.compute_lexical_matching_score(output_1['lexical_weights'][0], output_1['lexical_weights'][1]))
114
+ # 0.0
115
+ ```
116
+
117
+ - Multi-Vector (ColBERT)
118
  ```python
119
+ from FlagEmbedding import BGEM3FlagModel
120
+
121
+ model = BGEM3FlagModel('BAAI/bge-m3', use_fp16=True)
122
+
123
+ sentences_1 = ["What is BGE M3?", "Defination of BM25"]
124
+ sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
125
+ "BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
126
 
127
+ output_1 = model.encode(sentences_1, return_dense=True, return_sparse=True, return_colbert_vecs=True)
128
+ output_2 = model.encode(sentences_2, return_dense=True, return_sparse=True, return_colbert_vecs=True)
129
+
130
+ print(model.colbert_score(output_1['colbert_vecs'][0], output_2['colbert_vecs'][0]))
131
+ print(model.colbert_score(output_1['colbert_vecs'][0], output_2['colbert_vecs'][1]))
132
+ # 0.7797
133
+ # 0.4620
134
  ```
135
 
136
 
137
+ ### Compute score for text pairs
138
+ Input a list of text pairs, you can get the scores computed by different methods.
139
+ ```python
140
+ from FlagEmbedding import BGEM3FlagModel
141
+
142
+ model = BGEM3FlagModel('BAAI/bge-m3', use_fp16=True)
143
+
144
+ sentences_1 = ["What is BGE M3?", "Defination of BM25"]
145
+ sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
146
+ "BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
147
+
148
+ sentence_pairs = [[i,j] for i in sentences_1 for j in sentences_2]
149
+ print(model.compute_score(sentence_pairs))
150
+ # {
151
+ # 'colbert': [0.7796499729156494, 0.4621465802192688, 0.4523794651031494, 0.7898575067520142],
152
+ # 'sparse': [0.05865478515625, 0.0026397705078125, 0.0, 0.0540771484375],
153
+ # 'dense': [0.6259765625, 0.347412109375, 0.349853515625, 0.67822265625],
154
+ # 'sparse+dense': [0.5266395211219788, 0.2692706882953644, 0.2691181004047394, 0.563307523727417],
155
+ # 'colbert+sparse+dense': [0.6366440653800964, 0.3531297743320465, 0.3487969636917114, 0.6618075370788574]
156
+ # }
157
+ ```
158
+
159
+
160
+
161
+
162
+ ## Evaluation
163
 
164
+ - Multilingual (Miracl dataset)
165
 
166
+ ![avatar](./imgs/miracl.jpg)
167
 
168
+ - Cross-lingual (MKQA dataset)
169
 
170
+ ![avatar](./imgs/mkqa.jpg)
171
 
172
+ - Long Document Retrieval
173
+
174
+ ![avatar](./imgs/long.jpg)
175
+
176
+
177
+ ## Training
178
+ - Self-knowledge Distillation: combining multiple outputs from different
179
+ retrieval modes as reward signal to enhance the performance of single mode(especially for sparse retrieval and multi-vec(colbert) retrival)
180
+ - Efficient Batching: Improve the efficiency when fine-tuning on long text.
181
+ The small-batch strategy is simple but effective, which also can used to fine-tune large embedding model.
182
+ - MCLS: A simple method to improve the performance on long text without fine-tuning.
183
+ If you have no enough resource to fine-tuning model with long text, the method is useful.
184
+
185
+ Refer to our [report]() for more details.
186
+
187
+ **The fine-tuning codes and datasets will be open-sourced in the near future.**
188
+
189
+ ## Models
190
+
191
+ We release two versions:
192
+ - [BAAI/bge-m3-unsupervised](https://huggingface.co/BAAI/bge-m3-unsupervised): the model after contrastive learning in a large-scale dataset
193
+ - [BAAI/bge-m3](https://huggingface.co/BAAI/bge-m3): the final model fine-tuned from BAAI/bge-m3-unsupervised
194
+
195
+ ## Acknowledgement
196
+
197
+ Thanks the authors of open-sourced datasets, including Miracl, MKQA, NarritiveQA, etc.
198
+
199
+ ## Citation
200
+
201
+ If you find this repository useful, please consider giving a star :star: and citation
202
 
 
203
  ```
204
+
 
 
 
 
205
  ```
206
 
 
207
 
208
+