-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add text similarity inputs and function name Signed-off-by: HenryL27 <[email protected]> * add text similarity cross encoder model Signed-off-by: HenryL27 <[email protected]> * add text similarity unit tests Signed-off-by: HenryL27 <[email protected]> * add text similarity input unittests Signed-off-by: HenryL27 <[email protected]> * add text similarity dataset unittests Signed-off-by: HenryL27 <[email protected]> * add function name annotation Signed-off-by: HenryL27 <[email protected]> * refactor API to use single query Signed-off-by: HenryL27 <[email protected]> * omit private from class vars Co-authored-by: Navneet Verma <[email protected]> Signed-off-by: HenryL27 <[email protected]> * change output name from logits to similarity Signed-off-by: HenryL27 <[email protected]> * hashify isDLModel Signed-off-by: HenryL27 <[email protected]> * add error message for non-torchscript cross encoders Signed-off-by: HenryL27 <[email protected]> * allow onnx, actually. Signed-off-by: HenryL27 <[email protected]> * apply spotless after rebase Signed-off-by: HenryL27 <[email protected]> * add unittest for new mlinput toXcontent clause Signed-off-by: HenryL27 <[email protected]> * static DLModels Signed-off-by: HenryL27 <[email protected]> * add tests and error message tweaks Signed-off-by: HenryL27 <[email protected]> * name test models w framework Signed-off-by: HenryL27 <[email protected]> * change pt->torch_script Signed-off-by: HenryL27 <[email protected]> --------- Signed-off-by: HenryL27 <[email protected]> Co-authored-by: Navneet Verma <[email protected]>
- Loading branch information
Showing
13 changed files
with
932 additions
and
4 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -9,5 +9,6 @@ public enum MLInputDataType { | |
SEARCH_QUERY, | ||
DATA_FRAME, | ||
TEXT_DOCS, | ||
TEXT_SIMILARITY, | ||
REMOTE | ||
} |
75 changes: 75 additions & 0 deletions
75
common/src/main/java/org/opensearch/ml/common/dataset/TextSimilarityInputDataSet.java
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,75 @@ | ||
/* | ||
* Copyright 2023 Aryn | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* 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 org.opensearch.ml.common.dataset; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
import org.opensearch.ml.common.annotation.InputDataSet; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.experimental.FieldDefaults; | ||
|
||
@Getter | ||
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) | ||
@InputDataSet(MLInputDataType.TEXT_SIMILARITY) | ||
public class TextSimilarityInputDataSet extends MLInputDataset { | ||
|
||
List<String> textDocs; | ||
|
||
String queryText; | ||
|
||
@Builder(toBuilder = true) | ||
public TextSimilarityInputDataSet(String queryText, List<String> textDocs) { | ||
super(MLInputDataType.TEXT_SIMILARITY); | ||
Objects.requireNonNull(textDocs); | ||
Objects.requireNonNull(queryText); | ||
if(textDocs.isEmpty()) { | ||
throw new IllegalArgumentException("No text documents were provided"); | ||
} | ||
this.textDocs = textDocs; | ||
this.queryText = queryText; | ||
} | ||
|
||
public TextSimilarityInputDataSet(StreamInput in) throws IOException { | ||
super(MLInputDataType.TEXT_SIMILARITY); | ||
this.queryText = in.readString(); | ||
int size = in.readInt(); | ||
this.textDocs = new ArrayList<String>(); | ||
for(int i = 0; i < size; i++) { | ||
String context = in.readString(); | ||
this.textDocs.add(context); | ||
} | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
out.writeString(queryText); | ||
out.writeInt(this.textDocs.size()); | ||
for (String doc : this.textDocs) { | ||
out.writeString(doc); | ||
} | ||
} | ||
} |
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
116 changes: 116 additions & 0 deletions
116
common/src/main/java/org/opensearch/ml/common/input/nlp/TextSimilarityMLInput.java
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,116 @@ | ||
/* | ||
* Copyright 2023 Aryn | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* 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 org.opensearch.ml.common.input.nlp; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.core.xcontent.XContentParser; | ||
import org.opensearch.ml.common.FunctionName; | ||
import org.opensearch.ml.common.dataset.MLInputDataset; | ||
import org.opensearch.ml.common.dataset.TextSimilarityInputDataSet; | ||
import org.opensearch.ml.common.input.MLInput; | ||
|
||
import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken; | ||
|
||
|
||
/** | ||
* MLInput which supports a text similarity algorithm | ||
* Inputs are a query and a list of texts. Outputs are real numbers | ||
* Use this for Cross Encoder models | ||
*/ | ||
@org.opensearch.ml.common.annotation.MLInput(functionNames = {FunctionName.TEXT_SIMILARITY}) | ||
public class TextSimilarityMLInput extends MLInput { | ||
|
||
public TextSimilarityMLInput(FunctionName algorithm, MLInputDataset dataset) { | ||
super(algorithm, null, dataset); | ||
} | ||
|
||
public TextSimilarityMLInput(StreamInput in) throws IOException { | ||
super(in); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
builder.field(ALGORITHM_FIELD, algorithm.name()); | ||
if(parameters != null) { | ||
builder.field(ML_PARAMETERS_FIELD, parameters); | ||
} | ||
if(inputDataset != null) { | ||
TextSimilarityInputDataSet ds = (TextSimilarityInputDataSet) this.inputDataset; | ||
List<String> docs = ds.getTextDocs(); | ||
String queryText = ds.getQueryText(); | ||
builder.field(QUERY_TEXT_FIELD, queryText); | ||
if (docs != null && !docs.isEmpty()) { | ||
builder.startArray(TEXT_DOCS_FIELD); | ||
for(String d : docs) { | ||
builder.value(d); | ||
} | ||
builder.endArray(); | ||
} | ||
} | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
public TextSimilarityMLInput(XContentParser parser, FunctionName functionName) throws IOException { | ||
super(); | ||
this.algorithm = functionName; | ||
List<String> docs = new ArrayList<>(); | ||
String queryText = null; | ||
|
||
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser); | ||
while (parser.nextToken() != XContentParser.Token.END_OBJECT) { | ||
String fieldName = parser.currentName(); | ||
parser.nextToken(); | ||
|
||
switch (fieldName) { | ||
case TEXT_DOCS_FIELD: | ||
ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.currentToken(), parser); | ||
while (parser.nextToken() != XContentParser.Token.END_ARRAY) { | ||
String context = parser.text(); | ||
docs.add(context); | ||
} | ||
break; | ||
case QUERY_TEXT_FIELD: | ||
queryText = parser.text(); | ||
default: | ||
parser.skipChildren(); | ||
break; | ||
} | ||
} | ||
if(docs.isEmpty()) { | ||
throw new IllegalArgumentException("No text documents were provided"); | ||
} | ||
if(queryText == null) { | ||
throw new IllegalArgumentException("No query text was provided"); | ||
} | ||
inputDataset = new TextSimilarityInputDataSet(queryText, docs); | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
common/src/test/java/org/opensearch/ml/common/dataset/TextSimilarityInputDatasetTest.java
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,65 @@ | ||
/* | ||
* Copyright 2023 Aryn | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* 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 org.opensearch.ml.common.dataset; | ||
|
||
import static org.junit.Assert.assertThrows; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
import org.opensearch.common.io.stream.BytesStreamOutput; | ||
import org.opensearch.core.common.bytes.BytesReference; | ||
import org.opensearch.core.common.io.stream.BytesStreamInput; | ||
import org.opensearch.core.common.io.stream.OutputStreamStreamOutput; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
|
||
public class TextSimilarityInputDatasetTest { | ||
|
||
@Test | ||
public void testStreaming() throws IOException { | ||
List<String> docs = List.of("That is a happy dog", "it's summer"); | ||
String queryText = "today is sunny"; | ||
TextSimilarityInputDataSet dataset = TextSimilarityInputDataSet.builder().queryText(queryText).textDocs(docs).build(); | ||
BytesStreamOutput outbytes = new BytesStreamOutput(); | ||
StreamOutput osso = new OutputStreamStreamOutput(outbytes); | ||
dataset.writeTo(osso); | ||
StreamInput in = new BytesStreamInput(BytesReference.toBytes(outbytes.bytes())); | ||
TextSimilarityInputDataSet newDs = (TextSimilarityInputDataSet) MLInputDataset.fromStream(in); | ||
assert (dataset.getTextDocs().equals(newDs.getTextDocs())); | ||
assert (dataset.getQueryText().equals(newDs.getQueryText())); | ||
} | ||
|
||
@Test | ||
public void noPairs_ThenFail() { | ||
List<String> docs = List.of(); | ||
String queryText = "today is sunny"; | ||
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, | ||
() -> TextSimilarityInputDataSet.builder().textDocs(docs).queryText(queryText).build()); | ||
assert (e.getMessage().equals("No text documents were provided")); | ||
} | ||
|
||
@Test | ||
public void noQuery_ThenFail() { | ||
List<String> docs = List.of("That is a happy dog", "it's summer"); | ||
String queryText = null; | ||
assertThrows(NullPointerException.class, | ||
() -> TextSimilarityInputDataSet.builder().textDocs(docs).queryText(queryText).build()); | ||
} | ||
} |
Oops, something went wrong.