spark-nlp 6.1.2rc1__py2.py3-none-any.whl → 6.1.3__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of spark-nlp might be problematic. Click here for more details.
- {spark_nlp-6.1.2rc1.dist-info → spark_nlp-6.1.3.dist-info}/METADATA +5 -5
- {spark_nlp-6.1.2rc1.dist-info → spark_nlp-6.1.3.dist-info}/RECORD +13 -10
- sparknlp/__init__.py +1 -1
- sparknlp/annotator/ner/__init__.py +1 -0
- sparknlp/annotator/ner/ner_dl_graph_checker.py +237 -0
- sparknlp/annotator/seq2seq/__init__.py +1 -0
- sparknlp/annotator/seq2seq/auto_gguf_reranker.py +329 -0
- sparknlp/base/__init__.py +1 -0
- sparknlp/base/gguf_ranking_finisher.py +234 -0
- sparknlp/internal/__init__.py +6 -1
- sparknlp/reader/reader2doc.py +35 -1
- {spark_nlp-6.1.2rc1.dist-info → spark_nlp-6.1.3.dist-info}/WHEEL +0 -0
- {spark_nlp-6.1.2rc1.dist-info → spark_nlp-6.1.3.dist-info}/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: spark-nlp
|
|
3
|
-
Version: 6.1.
|
|
3
|
+
Version: 6.1.3
|
|
4
4
|
Summary: John Snow Labs Spark NLP is a natural language processing library built on top of Apache Spark ML. It provides simple, performant & accurate NLP annotations for machine learning pipelines, that scale easily in a distributed environment.
|
|
5
5
|
Home-page: https://github.com/JohnSnowLabs/spark-nlp
|
|
6
6
|
Author: John Snow Labs
|
|
@@ -102,7 +102,7 @@ $ java -version
|
|
|
102
102
|
$ conda create -n sparknlp python=3.7 -y
|
|
103
103
|
$ conda activate sparknlp
|
|
104
104
|
# spark-nlp by default is based on pyspark 3.x
|
|
105
|
-
$ pip install spark-nlp==6.1.
|
|
105
|
+
$ pip install spark-nlp==6.1.3 pyspark==3.3.1
|
|
106
106
|
```
|
|
107
107
|
|
|
108
108
|
In Python console or Jupyter `Python3` kernel:
|
|
@@ -168,7 +168,7 @@ For a quick example of using pipelines and models take a look at our official [d
|
|
|
168
168
|
|
|
169
169
|
### Apache Spark Support
|
|
170
170
|
|
|
171
|
-
Spark NLP *6.1.
|
|
171
|
+
Spark NLP *6.1.3* has been built on top of Apache Spark 3.4 while fully supports Apache Spark 3.0.x, 3.1.x, 3.2.x, 3.3.x, 3.4.x, and 3.5.x
|
|
172
172
|
|
|
173
173
|
| Spark NLP | Apache Spark 3.5.x | Apache Spark 3.4.x | Apache Spark 3.3.x | Apache Spark 3.2.x | Apache Spark 3.1.x | Apache Spark 3.0.x | Apache Spark 2.4.x | Apache Spark 2.3.x |
|
|
174
174
|
|-----------|--------------------|--------------------|--------------------|--------------------|--------------------|--------------------|--------------------|--------------------|
|
|
@@ -198,7 +198,7 @@ Find out more about 4.x `SparkNLP` versions in our official [documentation](http
|
|
|
198
198
|
|
|
199
199
|
### Databricks Support
|
|
200
200
|
|
|
201
|
-
Spark NLP 6.1.
|
|
201
|
+
Spark NLP 6.1.3 has been tested and is compatible with the following runtimes:
|
|
202
202
|
|
|
203
203
|
| **CPU** | **GPU** |
|
|
204
204
|
|--------------------|--------------------|
|
|
@@ -216,7 +216,7 @@ We are compatible with older runtimes. For a full list check databricks support
|
|
|
216
216
|
|
|
217
217
|
### EMR Support
|
|
218
218
|
|
|
219
|
-
Spark NLP 6.1.
|
|
219
|
+
Spark NLP 6.1.3 has been tested and is compatible with the following EMR releases:
|
|
220
220
|
|
|
221
221
|
| **EMR Release** |
|
|
222
222
|
|--------------------|
|
|
@@ -3,7 +3,7 @@ com/johnsnowlabs/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,
|
|
|
3
3
|
com/johnsnowlabs/ml/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
4
|
com/johnsnowlabs/ml/ai/__init__.py,sha256=YQiK2M7U4d8y5irPy_HB8ae0mSpqS9583MH44pnKJXc,295
|
|
5
5
|
com/johnsnowlabs/nlp/__init__.py,sha256=DPIVXtONO5xXyOk-HB0-sNiHAcco17NN13zPS_6Uw8c,294
|
|
6
|
-
sparknlp/__init__.py,sha256=
|
|
6
|
+
sparknlp/__init__.py,sha256=UR0dRykX67j-Ksuzk5Xe-Mod5qCK24iBjHHa0omOp2w,13814
|
|
7
7
|
sparknlp/annotation.py,sha256=I5zOxG5vV2RfPZfqN9enT1i4mo6oBcn3Lrzs37QiOiA,5635
|
|
8
8
|
sparknlp/annotation_audio.py,sha256=iRV_InSVhgvAwSRe9NTbUH9v6OGvTM-FPCpSAKVu0mE,1917
|
|
9
9
|
sparknlp/annotation_image.py,sha256=xhCe8Ko-77XqWVuuYHFrjKqF6zPd8Z-RY_rmZXNwCXU,2547
|
|
@@ -146,11 +146,12 @@ sparknlp/annotator/matcher/date_matcher.py,sha256=FrjTVoNBq1Z7E4qSJKvfV5rC7Mlm9R
|
|
|
146
146
|
sparknlp/annotator/matcher/multi_date_matcher.py,sha256=-zCp4HugIpSN6U4-c1uN_dGn7x69xYy6SUoQz6dY34s,4475
|
|
147
147
|
sparknlp/annotator/matcher/regex_matcher.py,sha256=GYhJNjBnCvfeEMv7d2_kDnqjfHtC7Hts--Ttlkjdozs,8380
|
|
148
148
|
sparknlp/annotator/matcher/text_matcher.py,sha256=kpP1-5BEFEW9kantoHvjhcCbX1qsGm4gEDtFHATWOwA,10636
|
|
149
|
-
sparknlp/annotator/ner/__init__.py,sha256=
|
|
149
|
+
sparknlp/annotator/ner/__init__.py,sha256=cd13PX6O92BIasE8NWd3JUR6up2fRbe7chRx4lQZRcY,1006
|
|
150
150
|
sparknlp/annotator/ner/ner_approach.py,sha256=4Y4gcusVtDaQpXfBbBe8XMAZ5hw1mvdh5A7g1j_T1KY,2793
|
|
151
151
|
sparknlp/annotator/ner/ner_converter.py,sha256=ANPp_Xe0DaK4z4n-0KujBj3Xp5jIbsFXBXvmp-aCKlM,5924
|
|
152
152
|
sparknlp/annotator/ner/ner_crf.py,sha256=eFylEz3-CENW0dyc6K4jodz9Kig3tnCyfZ3s-KZMvH4,14283
|
|
153
153
|
sparknlp/annotator/ner/ner_dl.py,sha256=ght1W6-ArjLRiNHCv_bKpozkyNd8HVIb8SDGhcbp8Fg,22123
|
|
154
|
+
sparknlp/annotator/ner/ner_dl_graph_checker.py,sha256=KB_BuNj2EPiVaHJEU9N1d40j-qGCn9e2tsTYctsioyc,8227
|
|
154
155
|
sparknlp/annotator/ner/ner_overwriter.py,sha256=en5OxXIP46yTXokIE96YDP9kcHA9oxiRPgwXMo0otew,6798
|
|
155
156
|
sparknlp/annotator/ner/zero_shot_ner_model.py,sha256=DohhnkGSG-JxjW72t8AOx3GY7R_qT-LA3I0KF9TBz-Y,7501
|
|
156
157
|
sparknlp/annotator/openai/__init__.py,sha256=u6SpV_xS8UpBE95WnTl0IefOI5TrTRl7ZHuYoeTetiA,759
|
|
@@ -167,8 +168,9 @@ sparknlp/annotator/sentence/sentence_detector_dl.py,sha256=-Osj9Bm9KyZRTAWkOsK9c
|
|
|
167
168
|
sparknlp/annotator/sentiment/__init__.py,sha256=Lq3vKaZS1YATLMg0VNXSVtkWL5q5G9taGBvdrvSwnfg,766
|
|
168
169
|
sparknlp/annotator/sentiment/sentiment_detector.py,sha256=m545NGU0Xzg_PO6_qIfpli1uZj7JQcyFgqe9R6wAPFI,8154
|
|
169
170
|
sparknlp/annotator/sentiment/vivekn_sentiment.py,sha256=4rpXWDgzU6ddnbrSCp9VdLb2epCc9oZ3c6XcqxEw8nk,9655
|
|
170
|
-
sparknlp/annotator/seq2seq/__init__.py,sha256=
|
|
171
|
+
sparknlp/annotator/seq2seq/__init__.py,sha256=aDiph00Hyq7L8uDY0frtyuHtqFodBqTMbixx_nq4z1I,1841
|
|
171
172
|
sparknlp/annotator/seq2seq/auto_gguf_model.py,sha256=yhZQHMHfp88rQvLHTWyS-8imZrwqp-8RQQwnw6PmHfc,11749
|
|
173
|
+
sparknlp/annotator/seq2seq/auto_gguf_reranker.py,sha256=MS4wCm2A2YiQfkB4HVVZKuN-3A1yGzqSCF69nu7J2rQ,12640
|
|
172
174
|
sparknlp/annotator/seq2seq/auto_gguf_vision_model.py,sha256=swBek2026dW6BOX5O9P8Uq41X2GC71VGW0ADFeUIvs0,15299
|
|
173
175
|
sparknlp/annotator/seq2seq/bart_transformer.py,sha256=I1flM4yeCzEAKOdQllBC30XuedxVJ7ferkFhZ6gwEbE,18481
|
|
174
176
|
sparknlp/annotator/seq2seq/cohere_transformer.py,sha256=43LZBVazZMgJRCsN7HaYjVYfJ5hRMV95QZyxMtXq-m4,13496
|
|
@@ -200,12 +202,13 @@ sparknlp/annotator/token/regex_tokenizer.py,sha256=FG2HvFwMb1G_4grfyIQaeBpaAgKv_
|
|
|
200
202
|
sparknlp/annotator/token/tokenizer.py,sha256=Me3P3wogUKUJ7O7_2wLdPzF00vKpp_sHuiztpGWRVpU,19939
|
|
201
203
|
sparknlp/annotator/ws/__init__.py,sha256=-l8bnl8Z6lGXWOBdRIBZ6958fzTHt4o87QhhLHIFF8A,693
|
|
202
204
|
sparknlp/annotator/ws/word_segmenter.py,sha256=rrbshwn5wzXIHpCCDji6ZcsmiARpuA82_p_6TgNHfRc,16365
|
|
203
|
-
sparknlp/base/__init__.py,sha256=
|
|
205
|
+
sparknlp/base/__init__.py,sha256=ug0e79fv03D67mUqmb-i-a89vOa6EcRx49Z0fS6rT3g,1361
|
|
204
206
|
sparknlp/base/audio_assembler.py,sha256=HKa9mXvmuMUrjTihUZkppGj-WJjcUrm2BGapNuPifyI,3320
|
|
205
207
|
sparknlp/base/doc2_chunk.py,sha256=TyvbdJNkVo9favHlOEoH5JwKbjpk5ZVJ75p8Cilp9jM,6551
|
|
206
208
|
sparknlp/base/document_assembler.py,sha256=zl-SXWMTR3B0EZ8z6SWYchCwEo-61FhU6u7dHUKDIOg,6697
|
|
207
209
|
sparknlp/base/embeddings_finisher.py,sha256=5QU1Okgl2ULrPVf4ze1H0SsRCMYXWGARtUsT7dagBYA,7659
|
|
208
210
|
sparknlp/base/finisher.py,sha256=V4wkMm9Ug09q4zTQc9T9Wr-awmu2Hu-eNaJ039YgZXM,8583
|
|
211
|
+
sparknlp/base/gguf_ranking_finisher.py,sha256=tzoisuD70myfHo3t4WFtBs8i1jwdjkwXCbWknDEXOHk,8315
|
|
209
212
|
sparknlp/base/graph_finisher.py,sha256=a8fxk3ei2YQw6s0Y9Yy8oMOF1i1XUrgqaiwVE0VPt4w,4834
|
|
210
213
|
sparknlp/base/has_recursive_fit.py,sha256=P55rSHLIXhihXWS2bOC_DskcQTc3njieVD1JkjS2bcA,849
|
|
211
214
|
sparknlp/base/has_recursive_transform.py,sha256=UkGNgo4LMsjQC-Coeefg4bJcg7FoPcPiG382zEa6Ywk,841
|
|
@@ -228,7 +231,7 @@ sparknlp/common/read_as.py,sha256=imxPGwV7jr4Li_acbo0OAHHRGCBbYv-akzEGaBWEfcY,12
|
|
|
228
231
|
sparknlp/common/recursive_annotator_approach.py,sha256=vqugBw22cE3Ff7PIpRlnYFuOlchgL0nM26D8j-NdpqU,1449
|
|
229
232
|
sparknlp/common/storage.py,sha256=D91H3p8EIjNspjqAYu6ephRpCUtdcAir4_PrAbkIQWE,4842
|
|
230
233
|
sparknlp/common/utils.py,sha256=Yne6yYcwKxhOZC-U4qfYoDhWUP_6BIaAjI5X_P_df1E,1306
|
|
231
|
-
sparknlp/internal/__init__.py,sha256=
|
|
234
|
+
sparknlp/internal/__init__.py,sha256=m7Y7y-IPkB6aJuGUCM54eOueGOEt65C3ujAzN16hegQ,40995
|
|
232
235
|
sparknlp/internal/annotator_java_ml.py,sha256=UGPoThG0rGXUOXGSQnDzEDW81Mu1s5RPF29v7DFyE3c,1187
|
|
233
236
|
sparknlp/internal/annotator_transformer.py,sha256=fXmc2IWXGybqZpbEU9obmbdBYPc798y42zvSB4tqV9U,1448
|
|
234
237
|
sparknlp/internal/extended_java_wrapper.py,sha256=hwP0133-hDiDf5sBF-P3MtUsuuDj1PpQbtGZQIRwzfk,2240
|
|
@@ -247,7 +250,7 @@ sparknlp/pretrained/utils.py,sha256=T1MrvW_DaWk_jcOjVLOea0NMFE9w8fe0ZT_5urZ_nEY,
|
|
|
247
250
|
sparknlp/reader/__init__.py,sha256=-Toj3AIBki-zXPpV8ezFTI2LX1yP_rK2bhpoa8nBkTw,685
|
|
248
251
|
sparknlp/reader/enums.py,sha256=MNGug9oJ1BBLM1Pbske13kAabalDzHa2kucF5xzFpHs,770
|
|
249
252
|
sparknlp/reader/pdf_to_text.py,sha256=eWw-cwjosmcSZ9eHso0F5QQoeGBBnwsOhzhCXXvMjZA,7169
|
|
250
|
-
sparknlp/reader/reader2doc.py,sha256=
|
|
253
|
+
sparknlp/reader/reader2doc.py,sha256=8x1tvx7Hj2J4xpyRiCUvrG-kmOPBvIE8K1tJZY-e0Xw,8200
|
|
251
254
|
sparknlp/reader/reader2table.py,sha256=GC6Yz0gQ83S6XKOi329TUNQuAvLrBxysqDkDRZPvcYA,4759
|
|
252
255
|
sparknlp/reader/sparknlp_reader.py,sha256=MJs8v_ECYaV1SOabI1L_2MkVYEDVImtwgbYypO7DJSY,20623
|
|
253
256
|
sparknlp/training/__init__.py,sha256=qREi9u-5Vc2VjpL6-XZsyvu5jSEIdIhowW7_kKaqMqo,852
|
|
@@ -280,7 +283,7 @@ sparknlp/training/_tf_graph_builders_1x/ner_dl/dataset_encoder.py,sha256=R4yHFN3
|
|
|
280
283
|
sparknlp/training/_tf_graph_builders_1x/ner_dl/ner_model.py,sha256=EoCSdcIjqQ3wv13MAuuWrKV8wyVBP0SbOEW41omHlR0,23189
|
|
281
284
|
sparknlp/training/_tf_graph_builders_1x/ner_dl/ner_model_saver.py,sha256=k5CQ7gKV6HZbZMB8cKLUJuZxoZWlP_DFWdZ--aIDwsc,2356
|
|
282
285
|
sparknlp/training/_tf_graph_builders_1x/ner_dl/sentence_grouper.py,sha256=pAxjWhjazSX8Vg0MFqJiuRVw1IbnQNSs-8Xp26L4nko,870
|
|
283
|
-
spark_nlp-6.1.
|
|
284
|
-
spark_nlp-6.1.
|
|
285
|
-
spark_nlp-6.1.
|
|
286
|
-
spark_nlp-6.1.
|
|
286
|
+
spark_nlp-6.1.3.dist-info/METADATA,sha256=U4Fb5wRd8Ql6BULfRwQSE6Pa77wsLwOGwTk-s038YuI,19774
|
|
287
|
+
spark_nlp-6.1.3.dist-info/WHEEL,sha256=JNWh1Fm1UdwIQV075glCn4MVuCRs0sotJIq-J6rbxCU,109
|
|
288
|
+
spark_nlp-6.1.3.dist-info/top_level.txt,sha256=uuytur4pyMRw2H_txNY2ZkaucZHUs22QF8-R03ch_-E,13
|
|
289
|
+
spark_nlp-6.1.3.dist-info/RECORD,,
|
sparknlp/__init__.py
CHANGED
|
@@ -16,5 +16,6 @@ from sparknlp.annotator.ner.ner_approach import *
|
|
|
16
16
|
from sparknlp.annotator.ner.ner_converter import *
|
|
17
17
|
from sparknlp.annotator.ner.ner_crf import *
|
|
18
18
|
from sparknlp.annotator.ner.ner_dl import *
|
|
19
|
+
from sparknlp.annotator.ner.ner_dl_graph_checker import *
|
|
19
20
|
from sparknlp.annotator.ner.ner_overwriter import *
|
|
20
21
|
from sparknlp.annotator.ner.zero_shot_ner_model import *
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
# Copyright 2017-2025 John Snow Labs
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
"""Contains classes for NerDL."""
|
|
15
|
+
|
|
16
|
+
from sparknlp.common import *
|
|
17
|
+
import sparknlp.internal as _internal
|
|
18
|
+
from pyspark.ml.util import JavaMLWritable
|
|
19
|
+
from pyspark.ml.wrapper import JavaEstimator
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class NerDLGraphChecker(
|
|
23
|
+
JavaEstimator,
|
|
24
|
+
JavaMLWritable,
|
|
25
|
+
_internal.ParamsGettersSetters,
|
|
26
|
+
):
|
|
27
|
+
"""Checks whether a suitable NerDL graph is available for the given training dataset, before any
|
|
28
|
+
computations/training is done. This annotator is useful for custom training cases, where
|
|
29
|
+
specialized graphs are needed.
|
|
30
|
+
|
|
31
|
+
Important: This annotator should be used or positioned before any embedding or NerDLApproach
|
|
32
|
+
annotators in the pipeline and will process the whole dataset to extract the required graph parameters.
|
|
33
|
+
|
|
34
|
+
This annotator requires a dataset with at least two columns: one with tokens and one with the
|
|
35
|
+
labels. In addition, it requires the used embedding annotator in the pipeline to extract the
|
|
36
|
+
suitable embedding dimension.
|
|
37
|
+
|
|
38
|
+
For extended examples of usage, see the`Examples
|
|
39
|
+
<https://github.com/JohnSnowLabs/spark-nlp/blob/master//home/ducha/Workspace/scala/spark-nlp-feature/examples/python/training/english/dl-ner/ner_dl_graph_checker.ipynb>`__
|
|
40
|
+
and the `NerDLGraphCheckerTestSpec
|
|
41
|
+
<https://github.com/JohnSnowLabs/spark-nlp/blob/master/src/test/scala/com/johnsnowlabs/nlp/annotators/ner/dl/NerDLGraphCheckerTestSpec.scala>`__.
|
|
42
|
+
|
|
43
|
+
==================================== ======================
|
|
44
|
+
Input Annotation types Output Annotation type
|
|
45
|
+
==================================== ======================
|
|
46
|
+
``DOCUMENT, TOKEN`` `NONE`
|
|
47
|
+
==================================== ======================
|
|
48
|
+
|
|
49
|
+
Parameters
|
|
50
|
+
----------
|
|
51
|
+
inputCols
|
|
52
|
+
Column names of input annotations
|
|
53
|
+
labelColumn
|
|
54
|
+
Column name for data labels
|
|
55
|
+
embeddingsDim
|
|
56
|
+
Dimensionality of embeddings
|
|
57
|
+
|
|
58
|
+
Examples
|
|
59
|
+
--------
|
|
60
|
+
>>> import sparknlp
|
|
61
|
+
>>> from sparknlp.base import *
|
|
62
|
+
>>> from sparknlp.annotator import *
|
|
63
|
+
>>> from pyspark.ml import Pipeline
|
|
64
|
+
|
|
65
|
+
This CoNLL dataset already includes a sentence, token and label
|
|
66
|
+
column with their respective annotator types. If a custom dataset is used,
|
|
67
|
+
these need to be defined with for example:
|
|
68
|
+
|
|
69
|
+
>>> conll = CoNLL()
|
|
70
|
+
>>> trainingData = conll.readDataset(spark, "src/test/resources/conll2003/eng.train")
|
|
71
|
+
>>> embeddings = BertEmbeddings \\
|
|
72
|
+
... .pretrained() \\
|
|
73
|
+
... .setInputCols(["sentence", "token"]) \\
|
|
74
|
+
... .setOutputCol("embeddings")
|
|
75
|
+
|
|
76
|
+
This annotatorr requires the data for NerDLApproach graphs: text, tokens, labels and the embedding model
|
|
77
|
+
|
|
78
|
+
>>> nerDLGraphChecker = NerDLGraphChecker() \\
|
|
79
|
+
... .setInputCols(["sentence", "token"]) \\
|
|
80
|
+
... .setLabelColumn("label") \\
|
|
81
|
+
... .setEmbeddingsModel(embeddings)
|
|
82
|
+
>>> nerTagger = NerDLApproach() \\
|
|
83
|
+
... .setInputCols(["sentence", "token", "embeddings"]) \\
|
|
84
|
+
... .setLabelColumn("label") \\
|
|
85
|
+
... .setOutputCol("ner") \\
|
|
86
|
+
... .setMaxEpochs(1) \\
|
|
87
|
+
... .setRandomSeed(0) \\
|
|
88
|
+
... .setVerbose(0)
|
|
89
|
+
>>> pipeline = Pipeline().setStages([nerDLGraphChecker, embeddings, nerTagger])
|
|
90
|
+
|
|
91
|
+
If we now fit the model with a graph missing, then an exception is raised.
|
|
92
|
+
|
|
93
|
+
>>> pipelineModel = pipeline.fit(trainingData)
|
|
94
|
+
"""
|
|
95
|
+
|
|
96
|
+
inputCols = Param(
|
|
97
|
+
Params._dummy(),
|
|
98
|
+
"inputCols",
|
|
99
|
+
"Input columns",
|
|
100
|
+
typeConverter=TypeConverters.toListString,
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
def setInputCols(self, *value):
|
|
104
|
+
"""Sets column names of input annotations.
|
|
105
|
+
|
|
106
|
+
Parameters
|
|
107
|
+
----------
|
|
108
|
+
*value : List[str]
|
|
109
|
+
Input columns for the annotator
|
|
110
|
+
"""
|
|
111
|
+
if type(value[0]) == str or type(value[0]) == list:
|
|
112
|
+
# self.inputColsValidation(value)
|
|
113
|
+
if len(value) == 1 and type(value[0]) == list:
|
|
114
|
+
return self._set(inputCols=value[0])
|
|
115
|
+
else:
|
|
116
|
+
return self._set(inputCols=list(value))
|
|
117
|
+
else:
|
|
118
|
+
raise TypeError(
|
|
119
|
+
"InputCols datatype not supported. It must be either str or list"
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
labelColumn = Param(
|
|
123
|
+
Params._dummy(),
|
|
124
|
+
"labelColumn",
|
|
125
|
+
"Column with label per each token",
|
|
126
|
+
typeConverter=TypeConverters.toString,
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
def setLabelColumn(self, value):
|
|
130
|
+
"""Sets name of column for data labels.
|
|
131
|
+
|
|
132
|
+
Parameters
|
|
133
|
+
----------
|
|
134
|
+
value : str
|
|
135
|
+
Column for data labels
|
|
136
|
+
"""
|
|
137
|
+
return self._set(labelColumn=value)
|
|
138
|
+
|
|
139
|
+
embeddingsDim = Param(
|
|
140
|
+
Params._dummy(),
|
|
141
|
+
"embeddingsDim",
|
|
142
|
+
"Dimensionality of embeddings",
|
|
143
|
+
typeConverter=TypeConverters.toInt,
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
def setEmbeddingsDim(self, value: int):
|
|
147
|
+
"""Sets Dimensionality of embeddings
|
|
148
|
+
|
|
149
|
+
Parameters
|
|
150
|
+
----------
|
|
151
|
+
value : int
|
|
152
|
+
Dimensionality of embeddings
|
|
153
|
+
"""
|
|
154
|
+
return self._set(embeddingsDim=value)
|
|
155
|
+
|
|
156
|
+
def setEmbeddingsModel(self, model: HasEmbeddingsProperties):
|
|
157
|
+
"""
|
|
158
|
+
Get embeddingsDim from a given embeddings model, if possible.
|
|
159
|
+
Falls back to setEmbeddingsDim if dimension cannot be obtained automatically.
|
|
160
|
+
"""
|
|
161
|
+
# Try Python API first
|
|
162
|
+
if hasattr(model, "getDimension"):
|
|
163
|
+
dim = model.getDimension()
|
|
164
|
+
return self.setEmbeddingsDim(int(dim))
|
|
165
|
+
# Try JVM side if available
|
|
166
|
+
if hasattr(model, "_java_obj") and hasattr(model._java_obj, "getDimension"):
|
|
167
|
+
dim = int(model._java_obj.getDimension())
|
|
168
|
+
return self.setEmbeddingsDim(dim)
|
|
169
|
+
raise ValueError(
|
|
170
|
+
"Could not infer embeddings dimension from provided model. "
|
|
171
|
+
"Use setEmbeddingsDim(dim) explicitly."
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
inputAnnotatorTypes = [
|
|
175
|
+
AnnotatorType.DOCUMENT,
|
|
176
|
+
AnnotatorType.TOKEN,
|
|
177
|
+
]
|
|
178
|
+
|
|
179
|
+
graphFolder = Param(
|
|
180
|
+
Params._dummy(),
|
|
181
|
+
"graphFolder",
|
|
182
|
+
"Folder path that contain external graph files",
|
|
183
|
+
TypeConverters.toString,
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
def setGraphFolder(self, p):
|
|
187
|
+
"""Sets folder path that contain external graph files.
|
|
188
|
+
|
|
189
|
+
Parameters
|
|
190
|
+
----------
|
|
191
|
+
p : str
|
|
192
|
+
Folder path that contain external graph files
|
|
193
|
+
"""
|
|
194
|
+
return self._set(graphFolder=p)
|
|
195
|
+
|
|
196
|
+
@keyword_only
|
|
197
|
+
def __init__(self):
|
|
198
|
+
_internal.ParamsGettersSetters.__init__(self)
|
|
199
|
+
classname = "com.johnsnowlabs.nlp.annotators.ner.dl.NerDLGraphChecker"
|
|
200
|
+
self.__class__._java_class_name = classname
|
|
201
|
+
self._java_obj = self._new_java_obj(classname, self.uid)
|
|
202
|
+
# self._setDefault()
|
|
203
|
+
|
|
204
|
+
def _create_model(self, java_model):
|
|
205
|
+
return NerDLGraphCheckerModel()
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
class NerDLGraphCheckerModel(
|
|
209
|
+
JavaModel,
|
|
210
|
+
JavaMLWritable,
|
|
211
|
+
_internal.ParamsGettersSetters,
|
|
212
|
+
):
|
|
213
|
+
"""
|
|
214
|
+
Resulting model from NerDLGraphChecker, that does not perform any transformations, as the
|
|
215
|
+
checks are done during the ``fit`` phase. It acts as the identity.
|
|
216
|
+
|
|
217
|
+
This annotator should never be used directly.
|
|
218
|
+
"""
|
|
219
|
+
|
|
220
|
+
inputAnnotatorTypes = [
|
|
221
|
+
AnnotatorType.DOCUMENT,
|
|
222
|
+
AnnotatorType.TOKEN,
|
|
223
|
+
]
|
|
224
|
+
|
|
225
|
+
@keyword_only
|
|
226
|
+
def __init__(
|
|
227
|
+
self,
|
|
228
|
+
classname="com.johnsnowlabs.nlp.annotators.ner.dl.NerDLGraphCheckerModel",
|
|
229
|
+
java_model=None,
|
|
230
|
+
):
|
|
231
|
+
super(NerDLGraphCheckerModel, self).__init__(java_model=java_model)
|
|
232
|
+
if classname and not java_model:
|
|
233
|
+
self.__class__._java_class_name = classname
|
|
234
|
+
self._java_obj = self._new_java_obj(classname, self.uid)
|
|
235
|
+
if java_model is not None:
|
|
236
|
+
self._transfer_params_from_java()
|
|
237
|
+
# self._setDefault(lazyAnnotator=False)
|
|
@@ -32,3 +32,4 @@ from sparknlp.annotator.seq2seq.llama3_transformer import *
|
|
|
32
32
|
from sparknlp.annotator.seq2seq.cohere_transformer import *
|
|
33
33
|
from sparknlp.annotator.seq2seq.olmo_transformer import *
|
|
34
34
|
from sparknlp.annotator.seq2seq.phi4_transformer import *
|
|
35
|
+
from sparknlp.annotator.seq2seq.auto_gguf_reranker import *
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
# Copyright 2017-2023 John Snow Labs
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
"""Contains classes for the AutoGGUFReranker."""
|
|
15
|
+
from typing import List, Dict
|
|
16
|
+
|
|
17
|
+
from sparknlp.common import *
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class AutoGGUFReranker(AnnotatorModel, HasBatchedAnnotate, HasLlamaCppProperties):
|
|
21
|
+
"""
|
|
22
|
+
Annotator that uses the llama.cpp library to rerank text documents based on their relevance
|
|
23
|
+
to a given query using GGUF-format reranking models.
|
|
24
|
+
|
|
25
|
+
This annotator is specifically designed for text reranking tasks, where multiple documents
|
|
26
|
+
or text passages are ranked according to their relevance to a query. It uses specialized
|
|
27
|
+
reranking models in GGUF format that output relevance scores for each input document.
|
|
28
|
+
|
|
29
|
+
The reranker takes a query (set via :meth:`.setQuery`) and a list of documents, then returns the
|
|
30
|
+
same documents with added metadata containing relevance scores. The documents are processed
|
|
31
|
+
in batches and each receives a ``relevance_score`` in its metadata indicating how relevant
|
|
32
|
+
it is to the provided query.
|
|
33
|
+
|
|
34
|
+
For settable parameters, and their explanations, see the parameters of this class and refer to
|
|
35
|
+
the llama.cpp documentation of
|
|
36
|
+
`server.cpp <https://github.com/ggerganov/llama.cpp/tree/7d5e8777ae1d21af99d4f95be10db4870720da91/examples/server>`__
|
|
37
|
+
for more information.
|
|
38
|
+
|
|
39
|
+
If the parameters are not set, the annotator will default to use the parameters provided by
|
|
40
|
+
the model.
|
|
41
|
+
|
|
42
|
+
Pretrained models can be loaded with :meth:`.pretrained` of the companion
|
|
43
|
+
object:
|
|
44
|
+
|
|
45
|
+
>>> reranker = AutoGGUFReranker.pretrained() \\
|
|
46
|
+
... .setInputCols(["document"]) \\
|
|
47
|
+
... .setOutputCol("reranked_documents") \\
|
|
48
|
+
... .setQuery("A man is eating pasta.")
|
|
49
|
+
|
|
50
|
+
The default model is ``"bge_reranker_v2_m3_Q4_K_M"``, if no name is provided.
|
|
51
|
+
|
|
52
|
+
For extended examples of usage, see the
|
|
53
|
+
`AutoGGUFRerankerTest <https://github.com/JohnSnowLabs/spark-nlp/tree/master/src/test/scala/com/johnsnowlabs/nlp/annotators/seq2seq/AutoGGUFRerankerTest.scala>`__
|
|
54
|
+
and the
|
|
55
|
+
`example notebook <https://github.com/JohnSnowLabs/spark-nlp/tree/master/examples/python/llama.cpp/llama.cpp_in_Spark_NLP_AutoGGUFReranker.ipynb>`__.
|
|
56
|
+
|
|
57
|
+
For available pretrained models please see the `Models Hub <https://sparknlp.org/models>`__.
|
|
58
|
+
|
|
59
|
+
====================== ======================
|
|
60
|
+
Input Annotation types Output Annotation type
|
|
61
|
+
====================== ======================
|
|
62
|
+
``DOCUMENT`` ``DOCUMENT``
|
|
63
|
+
====================== ======================
|
|
64
|
+
|
|
65
|
+
Parameters
|
|
66
|
+
----------
|
|
67
|
+
query
|
|
68
|
+
The query to be used for reranking. If not set, the input text will be used as the query.
|
|
69
|
+
nThreads
|
|
70
|
+
Set the number of threads to use during generation
|
|
71
|
+
nThreadsDraft
|
|
72
|
+
Set the number of threads to use during draft generation
|
|
73
|
+
nThreadsBatch
|
|
74
|
+
Set the number of threads to use during batch and prompt processing
|
|
75
|
+
nThreadsBatchDraft
|
|
76
|
+
Set the number of threads to use during batch and prompt processing
|
|
77
|
+
nCtx
|
|
78
|
+
Set the size of the prompt context
|
|
79
|
+
nBatch
|
|
80
|
+
Set the logical batch size for prompt processing (must be >=32 to use BLAS)
|
|
81
|
+
nUbatch
|
|
82
|
+
Set the physical batch size for prompt processing (must be >=32 to use BLAS)
|
|
83
|
+
nGpuLayers
|
|
84
|
+
Set the number of layers to store in VRAM (-1 - use default)
|
|
85
|
+
nGpuLayersDraft
|
|
86
|
+
Set the number of layers to store in VRAM for the draft model (-1 - use default)
|
|
87
|
+
gpuSplitMode
|
|
88
|
+
Set how to split the model across GPUs
|
|
89
|
+
mainGpu
|
|
90
|
+
Set the main GPU that is used for scratch and small tensors.
|
|
91
|
+
tensorSplit
|
|
92
|
+
Set how split tensors should be distributed across GPUs
|
|
93
|
+
grpAttnN
|
|
94
|
+
Set the group-attention factor
|
|
95
|
+
grpAttnW
|
|
96
|
+
Set the group-attention width
|
|
97
|
+
ropeFreqBase
|
|
98
|
+
Set the RoPE base frequency, used by NTK-aware scaling
|
|
99
|
+
ropeFreqScale
|
|
100
|
+
Set the RoPE frequency scaling factor, expands context by a factor of 1/N
|
|
101
|
+
yarnExtFactor
|
|
102
|
+
Set the YaRN extrapolation mix factor
|
|
103
|
+
yarnAttnFactor
|
|
104
|
+
Set the YaRN scale sqrt(t) or attention magnitude
|
|
105
|
+
yarnBetaFast
|
|
106
|
+
Set the YaRN low correction dim or beta
|
|
107
|
+
yarnBetaSlow
|
|
108
|
+
Set the YaRN high correction dim or alpha
|
|
109
|
+
yarnOrigCtx
|
|
110
|
+
Set the YaRN original context size of model
|
|
111
|
+
defragmentationThreshold
|
|
112
|
+
Set the KV cache defragmentation threshold
|
|
113
|
+
numaStrategy
|
|
114
|
+
Set optimization strategies that help on some NUMA systems (if available)
|
|
115
|
+
ropeScalingType
|
|
116
|
+
Set the RoPE frequency scaling method, defaults to linear unless specified by the model
|
|
117
|
+
poolingType
|
|
118
|
+
Set the pooling type for embeddings, use model default if unspecified
|
|
119
|
+
modelDraft
|
|
120
|
+
Set the draft model for speculative decoding
|
|
121
|
+
modelAlias
|
|
122
|
+
Set a model alias
|
|
123
|
+
lookupCacheStaticFilePath
|
|
124
|
+
Set path to static lookup cache to use for lookup decoding (not updated by generation)
|
|
125
|
+
lookupCacheDynamicFilePath
|
|
126
|
+
Set path to dynamic lookup cache to use for lookup decoding (updated by generation)
|
|
127
|
+
flashAttention
|
|
128
|
+
Whether to enable Flash Attention
|
|
129
|
+
inputPrefixBos
|
|
130
|
+
Whether to add prefix BOS to user inputs, preceding the `--in-prefix` string
|
|
131
|
+
useMmap
|
|
132
|
+
Whether to use memory-map model (faster load but may increase pageouts if not using mlock)
|
|
133
|
+
useMlock
|
|
134
|
+
Whether to force the system to keep model in RAM rather than swapping or compressing
|
|
135
|
+
noKvOffload
|
|
136
|
+
Whether to disable KV offload
|
|
137
|
+
systemPrompt
|
|
138
|
+
Set a system prompt to use
|
|
139
|
+
chatTemplate
|
|
140
|
+
The chat template to use
|
|
141
|
+
inputPrefix
|
|
142
|
+
Set the prompt to start generation with
|
|
143
|
+
inputSuffix
|
|
144
|
+
Set a suffix for infilling
|
|
145
|
+
cachePrompt
|
|
146
|
+
Whether to remember the prompt to avoid reprocessing it
|
|
147
|
+
nPredict
|
|
148
|
+
Set the number of tokens to predict
|
|
149
|
+
topK
|
|
150
|
+
Set top-k sampling
|
|
151
|
+
topP
|
|
152
|
+
Set top-p sampling
|
|
153
|
+
minP
|
|
154
|
+
Set min-p sampling
|
|
155
|
+
tfsZ
|
|
156
|
+
Set tail free sampling, parameter z
|
|
157
|
+
typicalP
|
|
158
|
+
Set locally typical sampling, parameter p
|
|
159
|
+
temperature
|
|
160
|
+
Set the temperature
|
|
161
|
+
dynatempRange
|
|
162
|
+
Set the dynamic temperature range
|
|
163
|
+
dynatempExponent
|
|
164
|
+
Set the dynamic temperature exponent
|
|
165
|
+
repeatLastN
|
|
166
|
+
Set the last n tokens to consider for penalties
|
|
167
|
+
repeatPenalty
|
|
168
|
+
Set the penalty of repeated sequences of tokens
|
|
169
|
+
frequencyPenalty
|
|
170
|
+
Set the repetition alpha frequency penalty
|
|
171
|
+
presencePenalty
|
|
172
|
+
Set the repetition alpha presence penalty
|
|
173
|
+
miroStat
|
|
174
|
+
Set MiroStat sampling strategies.
|
|
175
|
+
mirostatTau
|
|
176
|
+
Set the MiroStat target entropy, parameter tau
|
|
177
|
+
mirostatEta
|
|
178
|
+
Set the MiroStat learning rate, parameter eta
|
|
179
|
+
penalizeNl
|
|
180
|
+
Whether to penalize newline tokens
|
|
181
|
+
nKeep
|
|
182
|
+
Set the number of tokens to keep from the initial prompt
|
|
183
|
+
seed
|
|
184
|
+
Set the RNG seed
|
|
185
|
+
nProbs
|
|
186
|
+
Set the amount top tokens probabilities to output if greater than 0.
|
|
187
|
+
minKeep
|
|
188
|
+
Set the amount of tokens the samplers should return at least (0 = disabled)
|
|
189
|
+
grammar
|
|
190
|
+
Set BNF-like grammar to constrain generations
|
|
191
|
+
penaltyPrompt
|
|
192
|
+
Override which part of the prompt is penalized for repetition.
|
|
193
|
+
ignoreEos
|
|
194
|
+
Set whether to ignore end of stream token and continue generating (implies --logit-bias 2-inf)
|
|
195
|
+
disableTokenIds
|
|
196
|
+
Set the token ids to disable in the completion
|
|
197
|
+
stopStrings
|
|
198
|
+
Set strings upon seeing which token generation is stopped
|
|
199
|
+
samplers
|
|
200
|
+
Set which samplers to use for token generation in the given order
|
|
201
|
+
useChatTemplate
|
|
202
|
+
Set whether or not generate should apply a chat template
|
|
203
|
+
|
|
204
|
+
Notes
|
|
205
|
+
-----
|
|
206
|
+
This annotator is designed for reranking tasks and requires setting a query using ``setQuery``.
|
|
207
|
+
The query represents the search intent against which documents will be ranked. Each input
|
|
208
|
+
document receives a relevance score in the output metadata.
|
|
209
|
+
|
|
210
|
+
To use GPU inference with this annotator, make sure to use the Spark NLP GPU package and set
|
|
211
|
+
the number of GPU layers with the `setNGpuLayers` method.
|
|
212
|
+
|
|
213
|
+
When using larger models, we recommend adjusting GPU usage with `setNCtx` and `setNGpuLayers`
|
|
214
|
+
according to your hardware to avoid out-of-memory errors.
|
|
215
|
+
|
|
216
|
+
Examples
|
|
217
|
+
--------
|
|
218
|
+
>>> import sparknlp
|
|
219
|
+
>>> from sparknlp.base import *
|
|
220
|
+
>>> from sparknlp.annotator import *
|
|
221
|
+
>>> from pyspark.ml import Pipeline
|
|
222
|
+
>>> document = DocumentAssembler() \\
|
|
223
|
+
... .setInputCol("text") \\
|
|
224
|
+
... .setOutputCol("document")
|
|
225
|
+
>>> reranker = AutoGGUFReranker.pretrained() \\
|
|
226
|
+
... .setInputCols(["document"]) \\
|
|
227
|
+
... .setOutputCol("reranked_documents") \\
|
|
228
|
+
... .setBatchSize(4) \\
|
|
229
|
+
... .setQuery("A man is eating pasta.")
|
|
230
|
+
>>> pipeline = Pipeline().setStages([document, reranker])
|
|
231
|
+
>>> data = spark.createDataFrame([
|
|
232
|
+
... ["A man is eating food."],
|
|
233
|
+
... ["A man is eating a piece of bread."],
|
|
234
|
+
... ["The girl is carrying a baby."],
|
|
235
|
+
... ["A man is riding a horse."]
|
|
236
|
+
... ]).toDF("text")
|
|
237
|
+
>>> result = pipeline.fit(data).transform(data)
|
|
238
|
+
>>> result.select("reranked_documents").show(truncate = False)
|
|
239
|
+
# Each document will have a relevance_score in metadata showing how relevant it is to the query
|
|
240
|
+
"""
|
|
241
|
+
|
|
242
|
+
name = "AutoGGUFReranker"
|
|
243
|
+
inputAnnotatorTypes = [AnnotatorType.DOCUMENT]
|
|
244
|
+
outputAnnotatorType = AnnotatorType.DOCUMENT
|
|
245
|
+
|
|
246
|
+
query = Param(Params._dummy(), "query",
|
|
247
|
+
"The query to be used for reranking. If not set, the input text will be used as the query.",
|
|
248
|
+
typeConverter=TypeConverters.toString)
|
|
249
|
+
@keyword_only
|
|
250
|
+
def __init__(self, classname="com.johnsnowlabs.nlp.annotators.seq2seq.AutoGGUFReranker", java_model=None):
|
|
251
|
+
super(AutoGGUFReranker, self).__init__(
|
|
252
|
+
classname=classname,
|
|
253
|
+
java_model=java_model
|
|
254
|
+
)
|
|
255
|
+
self._setDefault(
|
|
256
|
+
useChatTemplate=True,
|
|
257
|
+
nCtx=4096,
|
|
258
|
+
nBatch=512,
|
|
259
|
+
nGpuLayers=99,
|
|
260
|
+
systemPrompt="You are a helpful assistant.",
|
|
261
|
+
query=""
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
def setQuery(self, value: str):
|
|
265
|
+
"""Set the query to be used for reranking.
|
|
266
|
+
|
|
267
|
+
Parameters
|
|
268
|
+
----------
|
|
269
|
+
value : str
|
|
270
|
+
The query text that documents will be ranked against.
|
|
271
|
+
|
|
272
|
+
Returns
|
|
273
|
+
-------
|
|
274
|
+
AutoGGUFReranker
|
|
275
|
+
This instance for method chaining.
|
|
276
|
+
"""
|
|
277
|
+
return self._set(query=value)
|
|
278
|
+
|
|
279
|
+
def getQuery(self):
|
|
280
|
+
"""Get the current query used for reranking.
|
|
281
|
+
|
|
282
|
+
Returns
|
|
283
|
+
-------
|
|
284
|
+
str
|
|
285
|
+
The current query string.
|
|
286
|
+
"""
|
|
287
|
+
return self._call_java("getQuery")
|
|
288
|
+
|
|
289
|
+
@staticmethod
|
|
290
|
+
def loadSavedModel(folder, spark_session):
|
|
291
|
+
"""Loads a locally saved model.
|
|
292
|
+
|
|
293
|
+
Parameters
|
|
294
|
+
----------
|
|
295
|
+
folder : str
|
|
296
|
+
Folder of the saved model
|
|
297
|
+
spark_session : pyspark.sql.SparkSession
|
|
298
|
+
The current SparkSession
|
|
299
|
+
|
|
300
|
+
Returns
|
|
301
|
+
-------
|
|
302
|
+
AutoGGUFReranker
|
|
303
|
+
The restored model
|
|
304
|
+
"""
|
|
305
|
+
from sparknlp.internal import _AutoGGUFRerankerLoader
|
|
306
|
+
jModel = _AutoGGUFRerankerLoader(folder, spark_session._jsparkSession)._java_obj
|
|
307
|
+
return AutoGGUFReranker(java_model=jModel)
|
|
308
|
+
|
|
309
|
+
@staticmethod
|
|
310
|
+
def pretrained(name="bge_reranker_v2_m3_Q4_K_M", lang="en", remote_loc=None):
|
|
311
|
+
"""Downloads and loads a pretrained model.
|
|
312
|
+
|
|
313
|
+
Parameters
|
|
314
|
+
----------
|
|
315
|
+
name : str, optional
|
|
316
|
+
Name of the pretrained model, by default "bge_reranker_v2_m3_Q4_K_M"
|
|
317
|
+
lang : str, optional
|
|
318
|
+
Language of the pretrained model, by default "en"
|
|
319
|
+
remote_loc : str, optional
|
|
320
|
+
Optional remote address of the resource, by default None. Will use
|
|
321
|
+
Spark NLPs repositories otherwise.
|
|
322
|
+
|
|
323
|
+
Returns
|
|
324
|
+
-------
|
|
325
|
+
AutoGGUFReranker
|
|
326
|
+
The restored model
|
|
327
|
+
"""
|
|
328
|
+
from sparknlp.pretrained import ResourceDownloader
|
|
329
|
+
return ResourceDownloader.downloadModel(AutoGGUFReranker, name, lang, remote_loc)
|
sparknlp/base/__init__.py
CHANGED
|
@@ -17,6 +17,7 @@ from sparknlp.base.document_assembler import *
|
|
|
17
17
|
from sparknlp.base.multi_document_assembler import *
|
|
18
18
|
from sparknlp.base.embeddings_finisher import *
|
|
19
19
|
from sparknlp.base.finisher import *
|
|
20
|
+
from sparknlp.base.gguf_ranking_finisher import *
|
|
20
21
|
from sparknlp.base.graph_finisher import *
|
|
21
22
|
from sparknlp.base.has_recursive_fit import *
|
|
22
23
|
from sparknlp.base.has_recursive_transform import *
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
# Copyright 2017-2024 John Snow Labs
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
"""Contains classes for the GGUFRankingFinisher."""
|
|
15
|
+
|
|
16
|
+
from pyspark import keyword_only
|
|
17
|
+
from pyspark.ml.param import TypeConverters, Params, Param
|
|
18
|
+
from sparknlp.internal import AnnotatorTransformer
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class GGUFRankingFinisher(AnnotatorTransformer):
|
|
22
|
+
"""Finisher for AutoGGUFReranker outputs that provides ranking capabilities
|
|
23
|
+
including top-k selection, sorting by relevance score, and score normalization.
|
|
24
|
+
|
|
25
|
+
This finisher processes the output of AutoGGUFReranker, which contains documents with
|
|
26
|
+
relevance scores in their metadata. It provides several options for post-processing:
|
|
27
|
+
|
|
28
|
+
- Top-k selection: Select only the top k documents by relevance score
|
|
29
|
+
- Score thresholding: Filter documents by minimum relevance score
|
|
30
|
+
- Min-max scaling: Normalize relevance scores to 0-1 range
|
|
31
|
+
- Sorting: Sort documents by relevance score in descending order
|
|
32
|
+
- Ranking: Add rank information to document metadata
|
|
33
|
+
|
|
34
|
+
The finisher preserves the document annotation structure while adding ranking information
|
|
35
|
+
to the metadata and optionally filtering/sorting the documents.
|
|
36
|
+
|
|
37
|
+
For extended examples of usage, see the `Examples
|
|
38
|
+
<https://github.com/JohnSnowLabs/spark-nlp/blob/master/examples/python/finisher/gguf_ranking_finisher_example.py>`__.
|
|
39
|
+
|
|
40
|
+
====================== ======================
|
|
41
|
+
Input Annotation types Output Annotation type
|
|
42
|
+
====================== ======================
|
|
43
|
+
``DOCUMENT`` ``DOCUMENT``
|
|
44
|
+
====================== ======================
|
|
45
|
+
|
|
46
|
+
Parameters
|
|
47
|
+
----------
|
|
48
|
+
inputCols
|
|
49
|
+
Name of input annotation columns containing reranked documents
|
|
50
|
+
outputCol
|
|
51
|
+
Name of output annotation column containing ranked documents, by default "ranked_documents"
|
|
52
|
+
topK
|
|
53
|
+
Maximum number of top documents to return based on relevance score (-1 for no limit), by default -1
|
|
54
|
+
minRelevanceScore
|
|
55
|
+
Minimum relevance score threshold for filtering documents, by default Double.MinValue
|
|
56
|
+
minMaxScaling
|
|
57
|
+
Whether to apply min-max scaling to normalize relevance scores to 0-1 range, by default False
|
|
58
|
+
|
|
59
|
+
Examples
|
|
60
|
+
--------
|
|
61
|
+
>>> import sparknlp
|
|
62
|
+
>>> from sparknlp.base import *
|
|
63
|
+
>>> from sparknlp.annotator import *
|
|
64
|
+
>>> from pyspark.ml import Pipeline
|
|
65
|
+
>>> documentAssembler = DocumentAssembler() \\
|
|
66
|
+
... .setInputCol("text") \\
|
|
67
|
+
... .setOutputCol("document")
|
|
68
|
+
>>> reranker = AutoGGUFReranker.pretrained() \\
|
|
69
|
+
... .setInputCols("document") \\
|
|
70
|
+
... .setOutputCol("reranked_documents") \\
|
|
71
|
+
... .setQuery("A man is eating pasta.")
|
|
72
|
+
>>> finisher = GGUFRankingFinisher() \\
|
|
73
|
+
... .setInputCols("reranked_documents") \\
|
|
74
|
+
... .setOutputCol("ranked_documents") \\
|
|
75
|
+
... .setTopK(3) \\
|
|
76
|
+
... .setMinMaxScaling(True)
|
|
77
|
+
>>> pipeline = Pipeline().setStages([documentAssembler, reranker, finisher])
|
|
78
|
+
>>> data = spark.createDataFrame([
|
|
79
|
+
... ("A man is eating food.",),
|
|
80
|
+
... ("A man is eating a piece of bread.",),
|
|
81
|
+
... ("The girl is carrying a baby.",),
|
|
82
|
+
... ("A man is riding a horse.",)
|
|
83
|
+
... ], ["text"])
|
|
84
|
+
>>> result = pipeline.fit(data).transform(data)
|
|
85
|
+
>>> result.select("ranked_documents").show(truncate=False)
|
|
86
|
+
# Documents will be sorted by relevance with rank information in metadata
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
name = "GGUFRankingFinisher"
|
|
90
|
+
|
|
91
|
+
inputCols = Param(Params._dummy(),
|
|
92
|
+
"inputCols",
|
|
93
|
+
"Name of input annotation columns containing reranked documents",
|
|
94
|
+
typeConverter=TypeConverters.toListString)
|
|
95
|
+
|
|
96
|
+
outputCol = Param(Params._dummy(),
|
|
97
|
+
"outputCol",
|
|
98
|
+
"Name of output annotation column containing ranked documents",
|
|
99
|
+
typeConverter=TypeConverters.toListString)
|
|
100
|
+
|
|
101
|
+
topK = Param(Params._dummy(),
|
|
102
|
+
"topK",
|
|
103
|
+
"Maximum number of top documents to return based on relevance score (-1 for no limit)",
|
|
104
|
+
typeConverter=TypeConverters.toInt)
|
|
105
|
+
|
|
106
|
+
minRelevanceScore = Param(Params._dummy(),
|
|
107
|
+
"minRelevanceScore",
|
|
108
|
+
"Minimum relevance score threshold for filtering documents",
|
|
109
|
+
typeConverter=TypeConverters.toFloat)
|
|
110
|
+
|
|
111
|
+
minMaxScaling = Param(Params._dummy(),
|
|
112
|
+
"minMaxScaling",
|
|
113
|
+
"Whether to apply min-max scaling to normalize relevance scores to 0-1 range",
|
|
114
|
+
typeConverter=TypeConverters.toBoolean)
|
|
115
|
+
|
|
116
|
+
@keyword_only
|
|
117
|
+
def __init__(self):
|
|
118
|
+
super(GGUFRankingFinisher, self).__init__(
|
|
119
|
+
classname="com.johnsnowlabs.nlp.finisher.GGUFRankingFinisher")
|
|
120
|
+
self._setDefault(
|
|
121
|
+
topK=-1,
|
|
122
|
+
minRelevanceScore=float('-inf'), # Equivalent to Double.MinValue
|
|
123
|
+
minMaxScaling=False,
|
|
124
|
+
outputCol=["ranked_documents"]
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
@keyword_only
|
|
128
|
+
def setParams(self):
|
|
129
|
+
kwargs = self._input_kwargs
|
|
130
|
+
return self._set(**kwargs)
|
|
131
|
+
|
|
132
|
+
def setInputCols(self, *value):
|
|
133
|
+
"""Sets input annotation column names.
|
|
134
|
+
|
|
135
|
+
Parameters
|
|
136
|
+
----------
|
|
137
|
+
value : List[str]
|
|
138
|
+
Input annotation column names containing reranked documents
|
|
139
|
+
"""
|
|
140
|
+
if len(value) == 1 and isinstance(value[0], list):
|
|
141
|
+
return self._set(inputCols=value[0])
|
|
142
|
+
else:
|
|
143
|
+
return self._set(inputCols=list(value))
|
|
144
|
+
|
|
145
|
+
def getInputCols(self):
|
|
146
|
+
"""Gets input annotation column names.
|
|
147
|
+
|
|
148
|
+
Returns
|
|
149
|
+
-------
|
|
150
|
+
List[str]
|
|
151
|
+
Input annotation column names
|
|
152
|
+
"""
|
|
153
|
+
return self.getOrDefault(self.inputCols)
|
|
154
|
+
|
|
155
|
+
def setOutputCol(self, value):
|
|
156
|
+
"""Sets output annotation column name.
|
|
157
|
+
|
|
158
|
+
Parameters
|
|
159
|
+
----------
|
|
160
|
+
value : str
|
|
161
|
+
Output annotation column name
|
|
162
|
+
"""
|
|
163
|
+
return self._set(outputCol=[value])
|
|
164
|
+
|
|
165
|
+
def getOutputCol(self):
|
|
166
|
+
"""Gets output annotation column name.
|
|
167
|
+
|
|
168
|
+
Returns
|
|
169
|
+
-------
|
|
170
|
+
str
|
|
171
|
+
Output annotation column name
|
|
172
|
+
"""
|
|
173
|
+
output_cols = self.getOrDefault(self.outputCol)
|
|
174
|
+
return output_cols[0] if output_cols else "ranked_documents"
|
|
175
|
+
|
|
176
|
+
def setTopK(self, value):
|
|
177
|
+
"""Sets maximum number of top documents to return.
|
|
178
|
+
|
|
179
|
+
Parameters
|
|
180
|
+
----------
|
|
181
|
+
value : int
|
|
182
|
+
Maximum number of top documents to return (-1 for no limit)
|
|
183
|
+
"""
|
|
184
|
+
return self._set(topK=value)
|
|
185
|
+
|
|
186
|
+
def getTopK(self):
|
|
187
|
+
"""Gets maximum number of top documents to return.
|
|
188
|
+
|
|
189
|
+
Returns
|
|
190
|
+
-------
|
|
191
|
+
int
|
|
192
|
+
Maximum number of top documents to return
|
|
193
|
+
"""
|
|
194
|
+
return self.getOrDefault(self.topK)
|
|
195
|
+
|
|
196
|
+
def setMinRelevanceScore(self, value):
|
|
197
|
+
"""Sets minimum relevance score threshold.
|
|
198
|
+
|
|
199
|
+
Parameters
|
|
200
|
+
----------
|
|
201
|
+
value : float
|
|
202
|
+
Minimum relevance score threshold
|
|
203
|
+
"""
|
|
204
|
+
return self._set(minRelevanceScore=value)
|
|
205
|
+
|
|
206
|
+
def getMinRelevanceScore(self):
|
|
207
|
+
"""Gets minimum relevance score threshold.
|
|
208
|
+
|
|
209
|
+
Returns
|
|
210
|
+
-------
|
|
211
|
+
float
|
|
212
|
+
Minimum relevance score threshold
|
|
213
|
+
"""
|
|
214
|
+
return self.getOrDefault(self.minRelevanceScore)
|
|
215
|
+
|
|
216
|
+
def setMinMaxScaling(self, value):
|
|
217
|
+
"""Sets whether to apply min-max scaling.
|
|
218
|
+
|
|
219
|
+
Parameters
|
|
220
|
+
----------
|
|
221
|
+
value : bool
|
|
222
|
+
Whether to apply min-max scaling to normalize scores
|
|
223
|
+
"""
|
|
224
|
+
return self._set(minMaxScaling=value)
|
|
225
|
+
|
|
226
|
+
def getMinMaxScaling(self):
|
|
227
|
+
"""Gets whether to apply min-max scaling.
|
|
228
|
+
|
|
229
|
+
Returns
|
|
230
|
+
-------
|
|
231
|
+
bool
|
|
232
|
+
Whether min-max scaling is enabled
|
|
233
|
+
"""
|
|
234
|
+
return self.getOrDefault(self.minMaxScaling)
|
sparknlp/internal/__init__.py
CHANGED
|
@@ -1191,4 +1191,9 @@ class _Phi4Loader(ExtendedJavaWrapper):
|
|
|
1191
1191
|
path,
|
|
1192
1192
|
jspark,
|
|
1193
1193
|
use_openvino,
|
|
1194
|
-
)
|
|
1194
|
+
)
|
|
1195
|
+
|
|
1196
|
+
class _AutoGGUFRerankerLoader(ExtendedJavaWrapper):
|
|
1197
|
+
def __init__(self, path, jspark):
|
|
1198
|
+
super(_AutoGGUFRerankerLoader, self).__init__(
|
|
1199
|
+
"com.johnsnowlabs.nlp.annotators.seq2seq.AutoGGUFReranker.loadSavedModel", path, jspark)
|
sparknlp/reader/reader2doc.py
CHANGED
|
@@ -122,6 +122,20 @@ class Reader2Doc(
|
|
|
122
122
|
typeConverter=TypeConverters.toString
|
|
123
123
|
)
|
|
124
124
|
|
|
125
|
+
outputAsDocument = Param(
|
|
126
|
+
Params._dummy(),
|
|
127
|
+
"outputAsDocument",
|
|
128
|
+
"Whether to return all sentences joined into a single document",
|
|
129
|
+
typeConverter=TypeConverters.toBoolean
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
excludeNonText = Param(
|
|
133
|
+
Params._dummy(),
|
|
134
|
+
"excludeNonText",
|
|
135
|
+
"Whether to exclude non-text content from the output. Default is False.",
|
|
136
|
+
typeConverter=TypeConverters.toBoolean
|
|
137
|
+
)
|
|
138
|
+
|
|
125
139
|
@keyword_only
|
|
126
140
|
def __init__(self):
|
|
127
141
|
super(Reader2Doc, self).__init__(classname="com.johnsnowlabs.reader.Reader2Doc")
|
|
@@ -182,7 +196,7 @@ class Reader2Doc(
|
|
|
182
196
|
def setFlattenOutput(self, value):
|
|
183
197
|
"""Sets whether to flatten the output to plain text with minimal metadata.
|
|
184
198
|
|
|
185
|
-
|
|
199
|
+
ParametersF
|
|
186
200
|
----------
|
|
187
201
|
value : bool
|
|
188
202
|
If true, output is flattened to plain text with minimal metadata
|
|
@@ -208,3 +222,23 @@ class Reader2Doc(
|
|
|
208
222
|
Output format for the table content. Options are 'plain-text' or 'html-table'. Default is 'json-table'.
|
|
209
223
|
"""
|
|
210
224
|
return self._set(outputFormat=value)
|
|
225
|
+
|
|
226
|
+
def setOutputAsDocument(self, value):
|
|
227
|
+
"""Sets whether to return all sentences joined into a single document.
|
|
228
|
+
|
|
229
|
+
Parameters
|
|
230
|
+
----------
|
|
231
|
+
value : bool
|
|
232
|
+
Whether to return all sentences joined into a single document
|
|
233
|
+
"""
|
|
234
|
+
return self._set(outputAsDocument=value)
|
|
235
|
+
|
|
236
|
+
def setExcludeNonText(self, value):
|
|
237
|
+
"""Sets whether to exclude non-text content from the output.
|
|
238
|
+
|
|
239
|
+
Parameters
|
|
240
|
+
----------
|
|
241
|
+
value : bool
|
|
242
|
+
Whether to exclude non-text content from the output. Default is False.
|
|
243
|
+
"""
|
|
244
|
+
return self._set(excludeNonText=value)
|
|
File without changes
|
|
File without changes
|