Integrate with Sentence Transformers (+ third parties like LangChain/Haystack/LlamaIndex, etc.) (#1)
Browse files- Integrate with Sentence Transformers (508b240524f06e133dc4312b9b14646fe09d2caf)
- 1_Pooling/config.json +10 -0
- README.md +33 -0
- config_sentence_transformers.json +12 -0
- modules.json +14 -0
- sentence_bert_config.json +4 -0
1_Pooling/config.json
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"word_embedding_dimension": 3584,
|
3 |
+
"pooling_mode_cls_token": false,
|
4 |
+
"pooling_mode_mean_tokens": false,
|
5 |
+
"pooling_mode_max_tokens": false,
|
6 |
+
"pooling_mode_mean_sqrt_len_tokens": false,
|
7 |
+
"pooling_mode_weightedmean_tokens": false,
|
8 |
+
"pooling_mode_lasttoken": true,
|
9 |
+
"include_prompt": true
|
10 |
+
}
|
README.md
CHANGED
@@ -2,6 +2,7 @@
|
|
2 |
tags:
|
3 |
- feature-extraction
|
4 |
- sentence-similarity
|
|
|
5 |
- transformers
|
6 |
- mteb
|
7 |
license: gemma
|
@@ -8835,6 +8836,38 @@ print(similarity)
|
|
8835 |
By default, FlagLLMModel will use all available GPUs when encoding. Please set `os.environ["CUDA_VISIBLE_DEVICES"]` to select specific GPUs.
|
8836 |
You also can set `os.environ["CUDA_VISIBLE_DEVICES"]=""` to make all GPUs unavailable.
|
8837 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8838 |
|
8839 |
### Using HuggingFace Transformers
|
8840 |
```python
|
|
|
2 |
tags:
|
3 |
- feature-extraction
|
4 |
- sentence-similarity
|
5 |
+
- sentence-transformers
|
6 |
- transformers
|
7 |
- mteb
|
8 |
license: gemma
|
|
|
8836 |
By default, FlagLLMModel will use all available GPUs when encoding. Please set `os.environ["CUDA_VISIBLE_DEVICES"]` to select specific GPUs.
|
8837 |
You also can set `os.environ["CUDA_VISIBLE_DEVICES"]=""` to make all GPUs unavailable.
|
8838 |
|
8839 |
+
### Using Sentence Transformers
|
8840 |
+
|
8841 |
+
```python
|
8842 |
+
from sentence_transformers import SentenceTransformer
|
8843 |
+
import torch
|
8844 |
+
|
8845 |
+
# Load the model, optionally in float16 precision for faster inference
|
8846 |
+
model = SentenceTransformer("BAAI/bge-multilingual-gemma2", model_kwargs={"torch_dtype": torch.float16})
|
8847 |
+
|
8848 |
+
# Prepare a prompt given an instruction
|
8849 |
+
instruction = 'Given a web search query, retrieve relevant passages that answer the query.'
|
8850 |
+
prompt = f'<instruct>{instruction}\n<query>'
|
8851 |
+
# Prepare queries and documents
|
8852 |
+
queries = [
|
8853 |
+
'how much protein should a female eat',
|
8854 |
+
'summit define',
|
8855 |
+
]
|
8856 |
+
documents = [
|
8857 |
+
"As a general guideline, the CDC's average requirement of protein for women ages 19 to 70 is 46 grams per day. But, as you can see from this chart, you'll need to increase that if you're expecting or training for a marathon. Check out the chart below to see how much protein you should be eating each day.",
|
8858 |
+
"Definition of summit for English Language Learners. : 1 the highest point of a mountain : the top of a mountain. : 2 the highest level. : 3 a meeting or series of meetings between the leaders of two or more governments."
|
8859 |
+
]
|
8860 |
+
|
8861 |
+
# Compute the query and document embeddings
|
8862 |
+
query_embeddings = model.encode(queries, prompt=prompt)
|
8863 |
+
document_embeddings = model.encode(documents)
|
8864 |
+
|
8865 |
+
# Compute the cosine similarity between the query and document embeddings
|
8866 |
+
similarities = model.similarity(query_embeddings, document_embeddings)
|
8867 |
+
print(similarities)
|
8868 |
+
# tensor([[ 0.5591, 0.0164],
|
8869 |
+
# [-0.0026, 0.4993]], dtype=torch.float16)
|
8870 |
+
```
|
8871 |
|
8872 |
### Using HuggingFace Transformers
|
8873 |
```python
|
config_sentence_transformers.json
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"__version__": {
|
3 |
+
"sentence_transformers": "3.0.1",
|
4 |
+
"transformers": "4.42.3",
|
5 |
+
"pytorch": "2.3.1+cu121"
|
6 |
+
},
|
7 |
+
"prompts": {
|
8 |
+
"web_search_query": "<instruct>Given a web search query, retrieve relevant passages that answer the query.\n<query>"
|
9 |
+
},
|
10 |
+
"default_prompt_name": null,
|
11 |
+
"similarity_fn_name": "cosine"
|
12 |
+
}
|
modules.json
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[
|
2 |
+
{
|
3 |
+
"idx": 0,
|
4 |
+
"name": "0",
|
5 |
+
"path": "",
|
6 |
+
"type": "sentence_transformers.models.Transformer"
|
7 |
+
},
|
8 |
+
{
|
9 |
+
"idx": 1,
|
10 |
+
"name": "1",
|
11 |
+
"path": "1_Pooling",
|
12 |
+
"type": "sentence_transformers.models.Pooling"
|
13 |
+
}
|
14 |
+
]
|
sentence_bert_config.json
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"max_seq_length": 8192,
|
3 |
+
"do_lower_case": false
|
4 |
+
}
|