-
Notifications
You must be signed in to change notification settings - Fork 717
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* SPARKNLP-1036: Onnx Example notebooks (#14234) * SPARKNLP-1036: Fix dev python kernel names * SPARKNLP-1036: Bump transformers version * SPARKNLP-1036: Fix Colab buttons * SPARKNLP-1036: Pin onnx version for compatibility * SPARKNLP-1036: Upgrade Spark version * SPARKNLP-1036: Minor Fixes * SPARKNLP-1036: Clean Metadata * SPARKNLP-1036: Add/Adjust Documentation - Note for supported Spark Version of Annotators - added missing Documentation for BGEEmbeddings * Fixies (#14307) * refactor OpenAIEmbeddings in Scala * refactor OpenAIEmbeddings in Python * add pytest.mark.slow and improve doc --------- Co-authored-by: Devin Ha <33089471+DevinTDHa@users.noreply.github.com> Co-authored-by: Lev <agsfer@gmail.com> Co-authored-by: Maziyar Panahi <maziyar.panahi@iscpif.fr>
- Loading branch information
1 parent
e88682c
commit 09dc500
Showing
7 changed files
with
209 additions
and
116 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import sys | ||
|
||
if sys.version_info[0] == 2: | ||
raise ImportError( | ||
"Spark NLP for Python 2.x is deprecated since version >= 4.0. " | ||
"Please use an older versions to use it with this Python version." | ||
) | ||
else: | ||
import sparknlp | ||
sys.modules['com.johnsnowlabs.ml.ai'] = sparknlp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
62 changes: 62 additions & 0 deletions
62
python/test/annotator/embeddings/open_ai_embeddings_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Copyright 2017-2022 John Snow Labs | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import unittest | ||
import pytest | ||
from sparknlp.annotator import * | ||
from sparknlp.base import * | ||
from pyspark.sql import DataFrame | ||
from pyspark.sql import SparkSession | ||
|
||
@pytest.mark.slow | ||
class OpenAIEmbeddingsTestCase(unittest.TestCase): | ||
# Set your OpenAI API key to run unit test... | ||
def setUp(self): | ||
self.spark = SparkSession.builder \ | ||
.appName("Tests") \ | ||
.master("local[*]") \ | ||
.config("spark.driver.memory","8G") \ | ||
.config("spark.driver.maxResultSize", "2G") \ | ||
.config("spark.jars", "lib/sparknlp.jar") \ | ||
.config("spark.serializer", "org.apache.spark.serializer.KryoSerializer") \ | ||
.config("spark.kryoserializer.buffer.max", "1000m") \ | ||
.config("spark.jsl.settings.openai.api.key","") \ | ||
.getOrCreate() | ||
|
||
def test_openai_embeddings(self): | ||
|
||
documentAssembler = DocumentAssembler() \ | ||
.setInputCol("text") \ | ||
.setOutputCol("document") | ||
openai_embeddings = OpenAIEmbeddings() \ | ||
.setInputCols("document") \ | ||
.setOutputCol("embeddings") \ | ||
.setModel("text-embedding-ada-002") | ||
|
||
import tempfile | ||
openai_embeddings.write().overwrite().save("file:///" + tempfile.gettempdir() + "/openai_embeddings") | ||
loaded = OpenAIEmbeddings.load("file:///" + tempfile.gettempdir() + "/openai_embeddings") | ||
|
||
pipeline = Pipeline().setStages([ | ||
documentAssembler, | ||
loaded | ||
]) | ||
|
||
sample_text = [["The food was delicious and the waiter..."]] | ||
sample_df = self.spark.createDataFrame(sample_text).toDF("text") | ||
pipeline.fit(sample_df).transform(sample_df).select("embeddings").show(truncate=False) | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
99 changes: 54 additions & 45 deletions
99
src/main/scala/com/johnsnowlabs/ml/ai/OpenAIEmbeddings.scala
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters