-
Notifications
You must be signed in to change notification settings - Fork 718
/
Copy pathBGEEmbeddingsTestSpec.scala
215 lines (166 loc) · 7.17 KB
/
BGEEmbeddingsTestSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* 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.
*/
package com.johnsnowlabs.nlp.embeddings
import com.johnsnowlabs.nlp.annotator.{SentenceDetector, Tokenizer}
import com.johnsnowlabs.nlp.annotators.sentence_detector_dl.SentenceDetectorDLModel
import com.johnsnowlabs.nlp.base.DocumentAssembler
import com.johnsnowlabs.nlp.util.io.ResourceHelper
import com.johnsnowlabs.tags.SlowTest
import com.johnsnowlabs.util.Benchmark
import org.apache.spark.ml.{Pipeline, PipelineModel}
import org.apache.spark.sql.functions.{col, size}
import org.scalatest.flatspec.AnyFlatSpec
class BGEEmbeddingsTestSpec extends AnyFlatSpec {
"BGE Embeddings" should "correctly embed multiple sentences" taggedAs SlowTest in {
import ResourceHelper.spark.implicits._
val ddd = Seq(
"query: how much protein should a female eat",
"query: summit define",
"passage: 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.",
"passage: 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.")
.toDF("text")
val document = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("document")
val embeddings = BGEEmbeddings
.pretrained()
.setInputCols(Array("document"))
.setOutputCol("bge")
val pipeline = new Pipeline().setStages(Array(document, embeddings))
val pipelineDF = pipeline.fit(ddd).transform(ddd)
pipelineDF.select("bge.embeddings").show(truncate = false)
}
"BGE Embeddings" should "be saved and loaded correctly" taggedAs SlowTest in {
import ResourceHelper.spark.implicits._
val ddd = Seq(
"query: how much protein should a female eat",
"query: summit define",
"passage: 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.",
"passage: 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.")
.toDF("text")
val documentAssembler = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("document")
val embeddings = BGEEmbeddings
.pretrained()
.setInputCols(Array("document"))
.setOutputCol("embeddings")
val pipeline = new Pipeline()
.setStages(Array(documentAssembler, embeddings))
val pipelineModel = pipeline.fit(ddd)
val pipelineDF = pipelineModel.transform(ddd)
pipelineDF.select("embeddings.result").show(false)
Benchmark.time("Time to save BGEEmbeddings pipeline model") {
pipelineModel.write.overwrite().save("./tmp_bge_pipeline")
}
Benchmark.time("Time to save BGEEmbeddings model") {
pipelineModel.stages.last
.asInstanceOf[BGEEmbeddings]
.write
.overwrite()
.save("./tmp_bge_model")
}
val loadedPipelineModel = PipelineModel.load("./tmp_bge_pipeline")
loadedPipelineModel.transform(ddd).select("embeddings.result").show(false)
val loadedSequenceModel = BGEEmbeddings.load("./tmp_bge_model")
}
it should "have embeddings of the same size" taggedAs SlowTest in {
import ResourceHelper.spark.implicits._
val testDf = Seq(
"I like apples",
"I like bananas \\n and other things \\n like icream \\n and cats",
"I like rockets")
.toDF("text")
val document = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("document")
val embeddings = BGEEmbeddings
.pretrained()
.setInputCols(Array("document"))
.setOutputCol("bge")
val pipeline = new Pipeline().setStages(Array(document, embeddings))
val pipelineDF = pipeline.fit(testDf).transform(testDf)
val embeddingsDF = pipelineDF.withColumn("embeddings", col("bge.embeddings").getItem(0))
val sizesArray: Array[Int] = embeddingsDF
.select(size(col("embeddings")).as("size"))
.collect()
.map(row => row.getAs[Int]("size"))
assert(sizesArray.forall(_ == sizesArray.head))
}
it should "work with sentences" taggedAs SlowTest in {
import ResourceHelper.spark.implicits._
val testData = "I really enjoy my job. This is amazing"
val testDf = Seq(testData).toDF("text")
val document = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("document")
val sentenceDetectorDL = SentenceDetectorDLModel
.pretrained("sentence_detector_dl", "en")
.setInputCols(Array("document"))
.setOutputCol("sentences")
val embeddings = BGEEmbeddings
.pretrained()
.setInputCols(Array("sentences"))
.setOutputCol("bge")
val pipeline = new Pipeline().setStages(Array(document, sentenceDetectorDL, embeddings))
val pipelineDF = pipeline.fit(testDf).transform(testDf)
pipelineDF.select("bge.embeddings").show(false)
}
it should "not return empty embeddings" taggedAs SlowTest in {
import ResourceHelper.spark.implicits._
val interests = Seq(
"I like music",
"I like movies",
"I like books",
"I like sports",
"I like travel",
"I like food",
"I like games",
"I like art",
"I like nature",
"I like science",
"I like technology",
"I like history",
"I like fashion",
"I like cars",
"I like animals",
"I like gardening")
val testDf = interests.toDF("text")
val document = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("document")
val embeddings = BGEEmbeddings
.pretrained()
.setInputCols(Array("document"))
.setOutputCol("bge")
val pipeline = new Pipeline().setStages(Array(document, embeddings))
val pipelineDF = pipeline.fit(testDf).transform(testDf)
val embeddingsDF = pipelineDF.withColumn("embeddings", col("bge.embeddings").getItem(0))
val sizesArray: Array[Int] = embeddingsDF
.select(size(col("embeddings")).as("size"))
.collect()
.map(row => row.getAs[Int]("size"))
assert(sizesArray.forall(_ > 0))
}
}