Commit
·
b53842a
1
Parent(s):
1d58a77
Add support to Sentence Transformers
Browse files- README.md +55 -0
- config_sentence_transformers.json +14 -0
- document_1_SpladePooling/config.json +5 -0
- modules.json +8 -0
- query_0_IDF/config.json +3 -0
- query_0_IDF/model.safetensors +3 -0
- query_0_IDF/special_tokens_map.json +37 -0
- query_0_IDF/tokenizer.json +0 -0
- query_0_IDF/tokenizer_config.json +63 -0
- query_0_IDF/vocab.txt +0 -0
- router_config.json +20 -0
README.md
CHANGED
|
@@ -9,6 +9,14 @@ tags:
|
|
| 9 |
- passage-retrieval
|
| 10 |
- document-expansion
|
| 11 |
- bag-of-words
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
---
|
| 13 |
|
| 14 |
# opensearch-neural-sparse-encoding-doc-v3-distill
|
|
@@ -37,6 +45,53 @@ The training datasets includes MS MARCO, eli5_question_answer, squad_pairs, Wiki
|
|
| 37 |
|
| 38 |
OpenSearch neural sparse feature supports learned sparse retrieval with lucene inverted index. Link: https://opensearch.org/docs/latest/query-dsl/specialized/neural-sparse/. The indexing and search can be performed with OpenSearch high-level API.
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
## Usage (HuggingFace)
|
| 42 |
This model is supposed to run inside OpenSearch cluster. But you can also use it outside the cluster, with HuggingFace models API.
|
|
|
|
| 9 |
- passage-retrieval
|
| 10 |
- document-expansion
|
| 11 |
- bag-of-words
|
| 12 |
+
- sentence-transformers
|
| 13 |
+
- sparse-encoder
|
| 14 |
+
- sparse
|
| 15 |
+
- asymmetric
|
| 16 |
+
- inference-free
|
| 17 |
+
- splade
|
| 18 |
+
pipeline_tag: feature-extraction
|
| 19 |
+
library_name: sentence-transformers
|
| 20 |
---
|
| 21 |
|
| 22 |
# opensearch-neural-sparse-encoding-doc-v3-distill
|
|
|
|
| 45 |
|
| 46 |
OpenSearch neural sparse feature supports learned sparse retrieval with lucene inverted index. Link: https://opensearch.org/docs/latest/query-dsl/specialized/neural-sparse/. The indexing and search can be performed with OpenSearch high-level API.
|
| 47 |
|
| 48 |
+
## Usage (Sentence Transformers)
|
| 49 |
+
|
| 50 |
+
First install the Sentence Transformers library:
|
| 51 |
+
|
| 52 |
+
```bash
|
| 53 |
+
pip install -U sentence-transformers
|
| 54 |
+
```
|
| 55 |
+
|
| 56 |
+
Then you can load this model and run inference.
|
| 57 |
+
|
| 58 |
+
```python
|
| 59 |
+
from sentence_transformers.sparse_encoder import SparseEncoder
|
| 60 |
+
|
| 61 |
+
# Download from the 🤗 Hub
|
| 62 |
+
model = SparseEncoder("opensearch-project/opensearch-neural-sparse-encoding-doc-v3-distill")
|
| 63 |
+
|
| 64 |
+
query = "What's the weather in ny now?"
|
| 65 |
+
document = "Currently New York is rainy."
|
| 66 |
+
|
| 67 |
+
query_embed = model.encode_query(query)
|
| 68 |
+
document_embed = model.encode_document(document)
|
| 69 |
+
|
| 70 |
+
sim = model.similarity(query_embed, document_embed)
|
| 71 |
+
print(f"Similarity: {sim}")
|
| 72 |
+
# Similarity: tensor([[11.1105]])
|
| 73 |
+
|
| 74 |
+
# Visualize top tokens for each text
|
| 75 |
+
top_k = 8
|
| 76 |
+
print(f"\nTop tokens {top_k} for each text:")
|
| 77 |
+
|
| 78 |
+
decoded_query = model.decode(query_embed, top_k=top_k)
|
| 79 |
+
decoded_document = model.decode(document_embed)
|
| 80 |
+
|
| 81 |
+
for i in range(top_k):
|
| 82 |
+
query_token, query_score = decoded_query[i]
|
| 83 |
+
doc_score = next((score for token, score in decoded_document if token == query_token), 0)
|
| 84 |
+
if doc_score != 0:
|
| 85 |
+
print(f"Token: {query_token}, Query score: {query_score:.4f}, Document score: {doc_score:.4f}")
|
| 86 |
+
|
| 87 |
+
# Top tokens 8 for each text:
|
| 88 |
+
# Token: ny, Query score: 5.7729, Document score: 0.8049
|
| 89 |
+
# Token: weather, Query score: 4.5684, Document score: 0.9710
|
| 90 |
+
# Token: now, Query score: 3.5895, Document score: 0.4720
|
| 91 |
+
# Token: ?, Query score: 3.3313, Document score: 0.0286
|
| 92 |
+
# Token: what, Query score: 2.7699, Document score: 0.0787
|
| 93 |
+
# Token: in, Query score: 0.4989, Document score: 0.0417
|
| 94 |
+
```
|
| 95 |
|
| 96 |
## Usage (HuggingFace)
|
| 97 |
This model is supposed to run inside OpenSearch cluster. But you can also use it outside the cluster, with HuggingFace models API.
|
config_sentence_transformers.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model_type": "SparseEncoder",
|
| 3 |
+
"__version__": {
|
| 4 |
+
"sentence_transformers": "5.0.0",
|
| 5 |
+
"transformers": "4.50.3",
|
| 6 |
+
"pytorch": "2.6.0+cu124"
|
| 7 |
+
},
|
| 8 |
+
"prompts": {
|
| 9 |
+
"query": "",
|
| 10 |
+
"document": ""
|
| 11 |
+
},
|
| 12 |
+
"default_prompt_name": null,
|
| 13 |
+
"similarity_fn_name": "dot"
|
| 14 |
+
}
|
document_1_SpladePooling/config.json
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"pooling_strategy": "max",
|
| 3 |
+
"activation_function": "log1p_relu",
|
| 4 |
+
"word_embedding_dimension": null
|
| 5 |
+
}
|
modules.json
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"idx": 0,
|
| 4 |
+
"name": "0",
|
| 5 |
+
"path": "",
|
| 6 |
+
"type": "sentence_transformers.models.Router"
|
| 7 |
+
}
|
| 8 |
+
]
|
query_0_IDF/config.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"frozen": true
|
| 3 |
+
}
|
query_0_IDF/model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:711ec64837a7962d2ae106996079782b7ee87860089a0b2348bf7cb840f252d3
|
| 3 |
+
size 122168
|
query_0_IDF/special_tokens_map.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cls_token": {
|
| 3 |
+
"content": "[CLS]",
|
| 4 |
+
"lstrip": false,
|
| 5 |
+
"normalized": false,
|
| 6 |
+
"rstrip": false,
|
| 7 |
+
"single_word": false
|
| 8 |
+
},
|
| 9 |
+
"mask_token": {
|
| 10 |
+
"content": "[MASK]",
|
| 11 |
+
"lstrip": false,
|
| 12 |
+
"normalized": false,
|
| 13 |
+
"rstrip": false,
|
| 14 |
+
"single_word": false
|
| 15 |
+
},
|
| 16 |
+
"pad_token": {
|
| 17 |
+
"content": "[PAD]",
|
| 18 |
+
"lstrip": false,
|
| 19 |
+
"normalized": false,
|
| 20 |
+
"rstrip": false,
|
| 21 |
+
"single_word": false
|
| 22 |
+
},
|
| 23 |
+
"sep_token": {
|
| 24 |
+
"content": "[SEP]",
|
| 25 |
+
"lstrip": false,
|
| 26 |
+
"normalized": false,
|
| 27 |
+
"rstrip": false,
|
| 28 |
+
"single_word": false
|
| 29 |
+
},
|
| 30 |
+
"unk_token": {
|
| 31 |
+
"content": "[UNK]",
|
| 32 |
+
"lstrip": false,
|
| 33 |
+
"normalized": false,
|
| 34 |
+
"rstrip": false,
|
| 35 |
+
"single_word": false
|
| 36 |
+
}
|
| 37 |
+
}
|
query_0_IDF/tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
query_0_IDF/tokenizer_config.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"added_tokens_decoder": {
|
| 3 |
+
"0": {
|
| 4 |
+
"content": "[PAD]",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false,
|
| 9 |
+
"special": true
|
| 10 |
+
},
|
| 11 |
+
"100": {
|
| 12 |
+
"content": "[UNK]",
|
| 13 |
+
"lstrip": false,
|
| 14 |
+
"normalized": false,
|
| 15 |
+
"rstrip": false,
|
| 16 |
+
"single_word": false,
|
| 17 |
+
"special": true
|
| 18 |
+
},
|
| 19 |
+
"101": {
|
| 20 |
+
"content": "[CLS]",
|
| 21 |
+
"lstrip": false,
|
| 22 |
+
"normalized": false,
|
| 23 |
+
"rstrip": false,
|
| 24 |
+
"single_word": false,
|
| 25 |
+
"special": true
|
| 26 |
+
},
|
| 27 |
+
"102": {
|
| 28 |
+
"content": "[SEP]",
|
| 29 |
+
"lstrip": false,
|
| 30 |
+
"normalized": false,
|
| 31 |
+
"rstrip": false,
|
| 32 |
+
"single_word": false,
|
| 33 |
+
"special": true
|
| 34 |
+
},
|
| 35 |
+
"103": {
|
| 36 |
+
"content": "[MASK]",
|
| 37 |
+
"lstrip": false,
|
| 38 |
+
"normalized": false,
|
| 39 |
+
"rstrip": false,
|
| 40 |
+
"single_word": false,
|
| 41 |
+
"special": true
|
| 42 |
+
}
|
| 43 |
+
},
|
| 44 |
+
"clean_up_tokenization_spaces": true,
|
| 45 |
+
"cls_token": "[CLS]",
|
| 46 |
+
"do_lower_case": true,
|
| 47 |
+
"extra_special_tokens": {},
|
| 48 |
+
"mask_token": "[MASK]",
|
| 49 |
+
"max_length": 512,
|
| 50 |
+
"model_max_length": 512,
|
| 51 |
+
"pad_to_multiple_of": null,
|
| 52 |
+
"pad_token": "[PAD]",
|
| 53 |
+
"pad_token_type_id": 0,
|
| 54 |
+
"padding_side": "right",
|
| 55 |
+
"sep_token": "[SEP]",
|
| 56 |
+
"stride": 0,
|
| 57 |
+
"strip_accents": null,
|
| 58 |
+
"tokenize_chinese_chars": true,
|
| 59 |
+
"tokenizer_class": "DistilBertTokenizer",
|
| 60 |
+
"truncation_side": "right",
|
| 61 |
+
"truncation_strategy": "longest_first",
|
| 62 |
+
"unk_token": "[UNK]"
|
| 63 |
+
}
|
query_0_IDF/vocab.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
router_config.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"types": {
|
| 3 |
+
"query_0_IDF": "sentence_transformers.sparse_encoder.models.IDF.IDF",
|
| 4 |
+
"": "sentence_transformers.sparse_encoder.models.MLMTransformer.MLMTransformer",
|
| 5 |
+
"document_1_SpladePooling": "sentence_transformers.sparse_encoder.models.SpladePooling.SpladePooling"
|
| 6 |
+
},
|
| 7 |
+
"structure": {
|
| 8 |
+
"query": [
|
| 9 |
+
"query_0_IDF"
|
| 10 |
+
],
|
| 11 |
+
"document": [
|
| 12 |
+
"",
|
| 13 |
+
"document_1_SpladePooling"
|
| 14 |
+
]
|
| 15 |
+
},
|
| 16 |
+
"parameters": {
|
| 17 |
+
"default_route": "document",
|
| 18 |
+
"allow_empty_key": true
|
| 19 |
+
}
|
| 20 |
+
}
|