zhichao-geng commited on
Commit
708f7e6
·
verified ·
1 Parent(s): aa4aea2

sentence_transformers_support (#4)

Browse files

- Add support to Sentence Transformers (0751606b0bcd53aeafa62c25eff889bd37c1c111)
- updates Readme (32032e2e5b36c3e3e27b707c4dc3e5815ac203e3)

1_SpladePooling/config.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "pooling_strategy": "max",
3
+ "activation_function": "relu",
4
+ "word_embedding_dimension": null
5
+ }
README.md CHANGED
@@ -10,6 +10,12 @@ tags:
10
  - query-expansion
11
  - document-expansion
12
  - bag-of-words
 
 
 
 
 
 
13
  ---
14
 
15
  # opensearch-neural-sparse-encoding-v1
@@ -39,6 +45,64 @@ This model is trained on MS MARCO dataset.
39
 
40
  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.
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  ## Usage (HuggingFace)
43
  This model is supposed to run inside OpenSearch cluster. But you can also use it outside the cluster, with HuggingFace models API.
44
 
 
10
  - query-expansion
11
  - document-expansion
12
  - bag-of-words
13
+ - sentence-transformers
14
+ - sparse-encoder
15
+ - sparse
16
+ - splade
17
+ pipeline_tag: feature-extraction
18
+ library_name: sentence-transformers
19
  ---
20
 
21
  # opensearch-neural-sparse-encoding-v1
 
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-v1")
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([[22.3299]])
73
+
74
+ decoded_query = model.decode(query_embed)
75
+ decoded_document = model.decode(document_embed)
76
+
77
+ for i in range(len(decoded_query)):
78
+ query_token, query_score = decoded_query[i]
79
+ doc_score = next((score for token, score in decoded_document if token == query_token), 0)
80
+ if doc_score != 0:
81
+ print(f"Token: {query_token}, Query score: {query_score:.4f}, Document score: {doc_score:.4f}")
82
+
83
+ # Token: ny, Query score: 2.9262, Document score: 2.1335
84
+ # Token: weather, Query score: 2.5206, Document score: 1.5277
85
+ # Token: york, Query score: 2.0373, Document score: 2.3489
86
+ # Token: cool, Query score: 1.5786, Document score: 0.8752
87
+ # Token: current, Query score: 1.4636, Document score: 1.5132
88
+ # Token: season, Query score: 0.7761, Document score: 0.8860
89
+ # Token: 2020, Query score: 0.7560, Document score: 0.6726
90
+ # Token: summer, Query score: 0.7222, Document score: 0.6292
91
+ # Token: nina, Query score: 0.6888, Document score: 0.6419
92
+ # Token: storm, Query score: 0.6451, Document score: 0.8200
93
+ # Token: brooklyn, Query score: 0.4698, Document score: 0.7635
94
+ # Token: julian, Query score: 0.4562, Document score: 0.1208
95
+ # Token: wow, Query score: 0.3484, Document score: 0.3903
96
+ # Token: usa, Query score: 0.3439, Document score: 0.4160
97
+ # Token: manhattan, Query score: 0.2751, Document score: 0.8260
98
+ # Token: fog, Query score: 0.2013, Document score: 0.7735
99
+ # Token: mood, Query score: 0.1989, Document score: 0.2961
100
+ # Token: climate, Query score: 0.1653, Document score: 0.3437
101
+ # Token: nature, Query score: 0.1191, Document score: 0.1533
102
+ # Token: temperature, Query score: 0.0665, Document score: 0.0599
103
+ # Token: windy, Query score: 0.0552, Document score: 0.3396
104
+ ```
105
+
106
  ## Usage (HuggingFace)
107
  This model is supposed to run inside OpenSearch cluster. But you can also use it outside the cluster, with HuggingFace models API.
108
 
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
+ }
modules.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "idx": 0,
4
+ "name": "0",
5
+ "path": "",
6
+ "type": "sentence_transformers.sparse_encoder.models.MLMTransformer"
7
+ },
8
+ {
9
+ "idx": 1,
10
+ "name": "1",
11
+ "path": "1_SpladePooling",
12
+ "type": "sentence_transformers.sparse_encoder.models.SpladePooling"
13
+ }
14
+ ]
sentence_bert_config.json ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ {
2
+ "max_seq_length": 512,
3
+ "do_lower_case": false
4
+ }