update
Browse files
README.md
CHANGED
@@ -2616,6 +2616,7 @@ language:
|
|
2616 |
<a href="#evaluation">Evaluation</a> |
|
2617 |
<a href="#train">Train</a> |
|
2618 |
<a href="#contact">Contact</a> |
|
|
|
2619 |
<a href="#license">License</a>
|
2620 |
<p>
|
2621 |
</h4>
|
@@ -2629,6 +2630,7 @@ FlagEmbedding can map any text to a low-dimensional dense vector which can be us
|
|
2629 |
And it also can be used in vector databases for LLMs.
|
2630 |
|
2631 |
************* 🌟**Updates**🌟 *************
|
|
|
2632 |
- 09/12/2023: New Release:
|
2633 |
- **New reranker model**: release cross-encoder models `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.
|
2634 |
- **update embedding model**: release `bge-*-v1.5` embedding model to alleviate the issue of the similarity distribution, and enhance its retrieval ability without instruction.
|
@@ -2663,10 +2665,9 @@ And it also can be used in vector databases for LLMs.
|
|
2663 |
|
2664 |
\*: If you need to search the relevant passages to a query, we suggest to add the instruction to the query; in other cases, no instruction is needed, just use the original query directly. In all cases, **no instruction** needs to be added to passages.
|
2665 |
|
2666 |
-
\**: Different embedding model, reranker
|
2667 |
For examples, use bge embedding model to retrieve top 100 relevant documents, and then use bge reranker to re-rank the top 100 document to get the final top-3 results.
|
2668 |
|
2669 |
-
|
2670 |
## Frequently asked questions
|
2671 |
|
2672 |
<details>
|
@@ -2729,7 +2730,9 @@ If it doesn't work for you, you can see [FlagEmbedding](https://github.com/FlagO
|
|
2729 |
from FlagEmbedding import FlagModel
|
2730 |
sentences_1 = ["样例数据-1", "样例数据-2"]
|
2731 |
sentences_2 = ["样例数据-3", "样例数据-4"]
|
2732 |
-
model = FlagModel('BAAI/bge-large-zh',
|
|
|
|
|
2733 |
embeddings_1 = model.encode(sentences_1)
|
2734 |
embeddings_2 = model.encode(sentences_2)
|
2735 |
similarity = embeddings_1 @ embeddings_2.T
|
@@ -2760,7 +2763,7 @@ pip install -U sentence-transformers
|
|
2760 |
from sentence_transformers import SentenceTransformer
|
2761 |
sentences_1 = ["样例数据-1", "样例数据-2"]
|
2762 |
sentences_2 = ["样例数据-3", "样例数据-4"]
|
2763 |
-
model = SentenceTransformer('BAAI/bge-large-zh')
|
2764 |
embeddings_1 = model.encode(sentences_1, normalize_embeddings=True)
|
2765 |
embeddings_2 = model.encode(sentences_2, normalize_embeddings=True)
|
2766 |
similarity = embeddings_1 @ embeddings_2.T
|
@@ -2775,7 +2778,7 @@ queries = ['query_1', 'query_2']
|
|
2775 |
passages = ["样例文档-1", "样例文档-2"]
|
2776 |
instruction = "为这个句子生成表示以用于检索相关文章:"
|
2777 |
|
2778 |
-
model = SentenceTransformer('BAAI/bge-large-zh')
|
2779 |
q_embeddings = model.encode([instruction+q for q in queries], normalize_embeddings=True)
|
2780 |
p_embeddings = model.encode(passages, normalize_embeddings=True)
|
2781 |
scores = q_embeddings @ p_embeddings.T
|
@@ -2786,7 +2789,7 @@ scores = q_embeddings @ p_embeddings.T
|
|
2786 |
You can use `bge` in langchain like this:
|
2787 |
```python
|
2788 |
from langchain.embeddings import HuggingFaceBgeEmbeddings
|
2789 |
-
model_name = "BAAI/bge-
|
2790 |
model_kwargs = {'device': 'cuda'}
|
2791 |
encode_kwargs = {'normalize_embeddings': True} # set True to compute cosine similarity
|
2792 |
model = HuggingFaceBgeEmbeddings(
|
@@ -2810,8 +2813,8 @@ import torch
|
|
2810 |
sentences = ["样例数据-1", "样例数据-2"]
|
2811 |
|
2812 |
# Load model from HuggingFace Hub
|
2813 |
-
tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-large-zh')
|
2814 |
-
model = AutoModel.from_pretrained('BAAI/bge-large-zh')
|
2815 |
model.eval()
|
2816 |
|
2817 |
# Tokenize sentences
|
@@ -2831,6 +2834,7 @@ print("Sentence embeddings:", sentence_embeddings)
|
|
2831 |
|
2832 |
### Usage for Reranker
|
2833 |
|
|
|
2834 |
You can get a relevance score by inputting query and passage to the reranker.
|
2835 |
The reranker is optimized based cross-entropy loss, so the relevance score is not bounded to a specific range.
|
2836 |
|
@@ -2840,10 +2844,10 @@ The reranker is optimized based cross-entropy loss, so the relevance score is no
|
|
2840 |
pip install -U FlagEmbedding
|
2841 |
```
|
2842 |
|
2843 |
-
Get relevance
|
2844 |
```python
|
2845 |
from FlagEmbedding import FlagReranker
|
2846 |
-
reranker = FlagReranker('BAAI/bge-reranker-
|
2847 |
|
2848 |
score = reranker.compute_score(['query', 'passage'])
|
2849 |
print(score)
|
@@ -2857,10 +2861,10 @@ print(scores)
|
|
2857 |
|
2858 |
```python
|
2859 |
import torch
|
2860 |
-
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
2861 |
|
2862 |
-
tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-reranker-
|
2863 |
-
model = AutoModelForSequenceClassification.from_pretrained('BAAI/bge-reranker-
|
2864 |
model.eval()
|
2865 |
|
2866 |
pairs = [['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.']]
|
@@ -2926,7 +2930,7 @@ Please refer to [C_MTEB](https://github.com/FlagOpen/FlagEmbedding/blob/master/C
|
|
2926 |
- **Reranking**:
|
2927 |
See [C_MTEB](https://github.com/FlagOpen/FlagEmbedding/blob/master/C_MTEB/) for evaluation script.
|
2928 |
|
2929 |
-
| Model | T2Reranking | T2RerankingZh2En\* | T2RerankingEn2Zh\* |
|
2930 |
|:-------------------------------|:--------:|:--------:|:--------:|:--------:|:--------:|:--------:|:--------:|
|
2931 |
| text2vec-base-multilingual | 64.66 | 62.94 | 62.51 | 14.37 | 48.46 | 48.6 | 50.26 |
|
2932 |
| multilingual-e5-small | 65.62 | 60.94 | 56.41 | 29.91 | 67.26 | 66.54 | 57.78 |
|
@@ -2939,13 +2943,13 @@ See [C_MTEB](https://github.com/FlagOpen/FlagEmbedding/blob/master/C_MTEB/) for
|
|
2939 |
| [BAAI/bge-reranker-base](https://huggingface.co/BAAI/bge-reranker-base) | 67.28 | 63.95 | 60.45 | 35.46 | 81.26 | 84.1 | 65.42 |
|
2940 |
| [BAAI/bge-reranker-large](https://huggingface.co/BAAI/bge-reranker-large) | 67.6 | 64.03 | 61.44 | 37.16 | 82.15 | 84.18 | 66.09 |
|
2941 |
|
2942 |
-
\* : T2RerankingZh2En and T2RerankingEn2Zh are cross-language retrieval
|
2943 |
|
2944 |
## Train
|
2945 |
|
2946 |
### BAAI Embedding
|
2947 |
|
2948 |
-
We pre-train the models using retromae and train them on large-scale pairs data using contrastive learning.
|
2949 |
**You can fine-tune the embedding model on your data following our [examples](https://github.com/FlagOpen/FlagEmbedding/tree/master/examples/finetune).**
|
2950 |
We also provide a [pre-train example](https://github.com/FlagOpen/FlagEmbedding/tree/master/examples/pretrain).
|
2951 |
Note that the goal of pre-training is to reconstruct the text, and the pre-trained model cannot be used for similarity calculation directly, it needs to be fine-tuned.
|
@@ -2968,6 +2972,20 @@ If you have any question or suggestion related to this project, feel free to ope
|
|
2968 |
You also can email Shitao Xiao([email protected]) and Zheng Liu([email protected]).
|
2969 |
|
2970 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2971 |
## License
|
2972 |
FlagEmbedding is licensed under the [MIT License](https://github.com/FlagOpen/FlagEmbedding/blob/master/LICENSE). The released models can be used for commercial purposes free of charge.
|
2973 |
|
|
|
2616 |
<a href="#evaluation">Evaluation</a> |
|
2617 |
<a href="#train">Train</a> |
|
2618 |
<a href="#contact">Contact</a> |
|
2619 |
+
<a href="#citation">Citation</a> |
|
2620 |
<a href="#license">License</a>
|
2621 |
<p>
|
2622 |
</h4>
|
|
|
2630 |
And it also can be used in vector databases for LLMs.
|
2631 |
|
2632 |
************* 🌟**Updates**🌟 *************
|
2633 |
+
- 09/15/2023: Release [paper](https://arxiv.org/pdf/2309.07597.pdf) and [dataset](https://data.baai.ac.cn/details/BAAI-MTP).
|
2634 |
- 09/12/2023: New Release:
|
2635 |
- **New reranker model**: release cross-encoder models `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.
|
2636 |
- **update embedding model**: release `bge-*-v1.5` embedding model to alleviate the issue of the similarity distribution, and enhance its retrieval ability without instruction.
|
|
|
2665 |
|
2666 |
\*: If you need to search the relevant passages to a query, we suggest to add the instruction to the query; in other cases, no instruction is needed, just use the original query directly. In all cases, **no instruction** needs to be added to passages.
|
2667 |
|
2668 |
+
\**: Different from embedding model, reranker uses question and document as input and directly output similarity instead of embedding. To balance the accuracy and time cost, cross-encoder is widely used to re-rank top-k documents retrieved by other simple models.
|
2669 |
For examples, use bge embedding model to retrieve top 100 relevant documents, and then use bge reranker to re-rank the top 100 document to get the final top-3 results.
|
2670 |
|
|
|
2671 |
## Frequently asked questions
|
2672 |
|
2673 |
<details>
|
|
|
2730 |
from FlagEmbedding import FlagModel
|
2731 |
sentences_1 = ["样例数据-1", "样例数据-2"]
|
2732 |
sentences_2 = ["样例数据-3", "样例数据-4"]
|
2733 |
+
model = FlagModel('BAAI/bge-large-zh-v1.5',
|
2734 |
+
query_instruction_for_retrieval="为这个句子生成表示以用于检索相关文章:",
|
2735 |
+
use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
|
2736 |
embeddings_1 = model.encode(sentences_1)
|
2737 |
embeddings_2 = model.encode(sentences_2)
|
2738 |
similarity = embeddings_1 @ embeddings_2.T
|
|
|
2763 |
from sentence_transformers import SentenceTransformer
|
2764 |
sentences_1 = ["样例数据-1", "样例数据-2"]
|
2765 |
sentences_2 = ["样例数据-3", "样例数据-4"]
|
2766 |
+
model = SentenceTransformer('BAAI/bge-large-zh-v1.5')
|
2767 |
embeddings_1 = model.encode(sentences_1, normalize_embeddings=True)
|
2768 |
embeddings_2 = model.encode(sentences_2, normalize_embeddings=True)
|
2769 |
similarity = embeddings_1 @ embeddings_2.T
|
|
|
2778 |
passages = ["样例文档-1", "样例文档-2"]
|
2779 |
instruction = "为这个句子生成表示以用于检索相关文章:"
|
2780 |
|
2781 |
+
model = SentenceTransformer('BAAI/bge-large-zh-v1.5')
|
2782 |
q_embeddings = model.encode([instruction+q for q in queries], normalize_embeddings=True)
|
2783 |
p_embeddings = model.encode(passages, normalize_embeddings=True)
|
2784 |
scores = q_embeddings @ p_embeddings.T
|
|
|
2789 |
You can use `bge` in langchain like this:
|
2790 |
```python
|
2791 |
from langchain.embeddings import HuggingFaceBgeEmbeddings
|
2792 |
+
model_name = "BAAI/bge-large-en-v1.5"
|
2793 |
model_kwargs = {'device': 'cuda'}
|
2794 |
encode_kwargs = {'normalize_embeddings': True} # set True to compute cosine similarity
|
2795 |
model = HuggingFaceBgeEmbeddings(
|
|
|
2813 |
sentences = ["样例数据-1", "样例数据-2"]
|
2814 |
|
2815 |
# Load model from HuggingFace Hub
|
2816 |
+
tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-large-zh-v1.5')
|
2817 |
+
model = AutoModel.from_pretrained('BAAI/bge-large-zh-v1.5')
|
2818 |
model.eval()
|
2819 |
|
2820 |
# Tokenize sentences
|
|
|
2834 |
|
2835 |
### Usage for Reranker
|
2836 |
|
2837 |
+
Different from embedding model, reranker uses question and document as input and directly output similarity instead of embedding.
|
2838 |
You can get a relevance score by inputting query and passage to the reranker.
|
2839 |
The reranker is optimized based cross-entropy loss, so the relevance score is not bounded to a specific range.
|
2840 |
|
|
|
2844 |
pip install -U FlagEmbedding
|
2845 |
```
|
2846 |
|
2847 |
+
Get relevance scores (higher scores indicate more relevance):
|
2848 |
```python
|
2849 |
from FlagEmbedding import FlagReranker
|
2850 |
+
reranker = FlagReranker('BAAI/bge-reranker-large', use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
|
2851 |
|
2852 |
score = reranker.compute_score(['query', 'passage'])
|
2853 |
print(score)
|
|
|
2861 |
|
2862 |
```python
|
2863 |
import torch
|
2864 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
2865 |
|
2866 |
+
tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-reranker-large')
|
2867 |
+
model = AutoModelForSequenceClassification.from_pretrained('BAAI/bge-reranker-large')
|
2868 |
model.eval()
|
2869 |
|
2870 |
pairs = [['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.']]
|
|
|
2930 |
- **Reranking**:
|
2931 |
See [C_MTEB](https://github.com/FlagOpen/FlagEmbedding/blob/master/C_MTEB/) for evaluation script.
|
2932 |
|
2933 |
+
| Model | T2Reranking | T2RerankingZh2En\* | T2RerankingEn2Zh\* | MMarcoReranking | CMedQAv1 | CMedQAv2 | Avg |
|
2934 |
|:-------------------------------|:--------:|:--------:|:--------:|:--------:|:--------:|:--------:|:--------:|
|
2935 |
| text2vec-base-multilingual | 64.66 | 62.94 | 62.51 | 14.37 | 48.46 | 48.6 | 50.26 |
|
2936 |
| multilingual-e5-small | 65.62 | 60.94 | 56.41 | 29.91 | 67.26 | 66.54 | 57.78 |
|
|
|
2943 |
| [BAAI/bge-reranker-base](https://huggingface.co/BAAI/bge-reranker-base) | 67.28 | 63.95 | 60.45 | 35.46 | 81.26 | 84.1 | 65.42 |
|
2944 |
| [BAAI/bge-reranker-large](https://huggingface.co/BAAI/bge-reranker-large) | 67.6 | 64.03 | 61.44 | 37.16 | 82.15 | 84.18 | 66.09 |
|
2945 |
|
2946 |
+
\* : T2RerankingZh2En and T2RerankingEn2Zh are cross-language retrieval tasks
|
2947 |
|
2948 |
## Train
|
2949 |
|
2950 |
### BAAI Embedding
|
2951 |
|
2952 |
+
We pre-train the models using [retromae](https://github.com/staoxiao/RetroMAE) and train them on large-scale pairs data using contrastive learning.
|
2953 |
**You can fine-tune the embedding model on your data following our [examples](https://github.com/FlagOpen/FlagEmbedding/tree/master/examples/finetune).**
|
2954 |
We also provide a [pre-train example](https://github.com/FlagOpen/FlagEmbedding/tree/master/examples/pretrain).
|
2955 |
Note that the goal of pre-training is to reconstruct the text, and the pre-trained model cannot be used for similarity calculation directly, it needs to be fine-tuned.
|
|
|
2972 |
You also can email Shitao Xiao([email protected]) and Zheng Liu([email protected]).
|
2973 |
|
2974 |
|
2975 |
+
## Citation
|
2976 |
+
|
2977 |
+
If you find our work helpful, please cite us:
|
2978 |
+
```
|
2979 |
+
@misc{bge_embedding,
|
2980 |
+
title={C-Pack: Packaged Resources To Advance General Chinese Embedding},
|
2981 |
+
author={Shitao Xiao and Zheng Liu and Peitian Zhang and Niklas Muennighoff},
|
2982 |
+
year={2023},
|
2983 |
+
eprint={2309.07597},
|
2984 |
+
archivePrefix={arXiv},
|
2985 |
+
primaryClass={cs.CL}
|
2986 |
+
}
|
2987 |
+
```
|
2988 |
+
|
2989 |
## License
|
2990 |
FlagEmbedding is licensed under the [MIT License](https://github.com/FlagOpen/FlagEmbedding/blob/master/LICENSE). The released models can be used for commercial purposes free of charge.
|
2991 |
|