Skip to content

Commit

Permalink
change input output formats for question answering model
Browse files Browse the repository at this point in the history
Signed-off-by: Bhavana Ramaram <[email protected]>
  • Loading branch information
rbhavna committed Mar 18, 2024
1 parent 0f3fe61 commit 7b6229a
Show file tree
Hide file tree
Showing 11 changed files with 336 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public enum MLInputDataType {
DATA_FRAME,
TEXT_DOCS,
REMOTE,
TEXT_SIMILARITY
TEXT_SIMILARITY,
QUESTION_ANSWERING
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.ml.common.dataset;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.experimental.FieldDefaults;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.ml.common.annotation.InputDataSet;

import java.io.IOException;

@Getter
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@InputDataSet(MLInputDataType.QUESTION_ANSWERING)
public class QuestionAnsweringInputDataSet extends MLInputDataset {

String question;

String context;

@Builder(toBuilder = true)
public QuestionAnsweringInputDataSet(String question, String context) {
super(MLInputDataType.QUESTION_ANSWERING);
if(question == null) {
throw new IllegalArgumentException("Question is not provided");
}
if(context == null) {
throw new IllegalArgumentException("Context is not provided");
}
this.question = question;
this.context = context;
}

public QuestionAnsweringInputDataSet(StreamInput in) throws IOException {
super(MLInputDataType.TEXT_SIMILARITY);
this.question = in.readString();
this.context = in.readString();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeString(question);
out.writeString(context);
}
}
27 changes: 26 additions & 1 deletion common/src/main/java/org/opensearch/ml/common/input/MLInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.opensearch.ml.common.dataframe.DataFrame;
import org.opensearch.ml.common.dataframe.DefaultDataFrame;
import org.opensearch.ml.common.dataset.DataFrameInputDataset;
import org.opensearch.ml.common.dataset.QuestionAnsweringInputDataSet;
import org.opensearch.ml.common.dataset.remote.RemoteInferenceInputDataSet;
import org.opensearch.ml.common.output.model.ModelResultFilter;
import org.opensearch.ml.common.dataset.MLInputDataset;
Expand Down Expand Up @@ -63,6 +64,12 @@ public class MLInput implements Input {
public static final String QUERY_TEXT_FIELD = "query_text";
public static final String PARAMETERS_FIELD = "parameters";

// Input question in question answering model
public static final String QUESTION_FIELD = "question";

// Input context in question answering model
public static final String CONTEXT_FIELD = "context";

// Algorithm name
protected FunctionName algorithm;
// ML algorithm parameters
Expand Down Expand Up @@ -178,6 +185,13 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.endArray();
}
break;
case QUESTION_ANSWERING:
QuestionAnsweringInputDataSet qaInputDataSet = (QuestionAnsweringInputDataSet) this.inputDataset;
String question = qaInputDataSet.getQuestion();
String context = qaInputDataSet.getContext();
builder.field(QUESTION_FIELD, question);
builder.field(CONTEXT_FIELD, context);
break;
case REMOTE:
RemoteInferenceInputDataSet remoteInferenceInputDataSet = (RemoteInferenceInputDataSet) this.inputDataset;
Map<String, String> parameters = remoteInferenceInputDataSet.getParameters();
Expand Down Expand Up @@ -213,6 +227,8 @@ public static MLInput parse(XContentParser parser, String inputAlgoName) throws
List<Integer> targetResponsePositions = new ArrayList<>();
List<String> textDocs = new ArrayList<>();
String queryText = null;
String question = null;
String context = null;

ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
Expand Down Expand Up @@ -263,19 +279,28 @@ public static MLInput parse(XContentParser parser, String inputAlgoName) throws
case QUERY_TEXT_FIELD:
queryText = parser.text();
break;
case QUESTION_FIELD:
question = parser.text();
break;
case CONTEXT_FIELD:
context = parser.text();
break;
default:
parser.skipChildren();
break;
}
}
MLInputDataset inputDataSet = null;
if (algorithm == FunctionName.TEXT_EMBEDDING || algorithm == FunctionName.SPARSE_ENCODING || algorithm == FunctionName.SPARSE_TOKENIZE || algorithm == FunctionName.QUESTION_ANSWERING) {
if (algorithm == FunctionName.TEXT_EMBEDDING || algorithm == FunctionName.SPARSE_ENCODING || algorithm == FunctionName.SPARSE_TOKENIZE) {
ModelResultFilter filter = new ModelResultFilter(returnBytes, returnNumber, targetResponse, targetResponsePositions);
inputDataSet = new TextDocsInputDataSet(textDocs, filter);
}
if (algorithm == FunctionName.TEXT_SIMILARITY) {
inputDataSet = new TextSimilarityInputDataSet(queryText, textDocs);
}
if (algorithm == FunctionName.QUESTION_ANSWERING) {
inputDataSet = new QuestionAnsweringInputDataSet(question, context);
}
return new MLInput(algorithm, mlParameters, searchSourceBuilder, sourceIndices, dataFrame, inputDataSet);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.ml.common.input.nlp;

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.QuestionAnsweringInputDataSet;
import org.opensearch.ml.common.input.MLInput;

import java.io.IOException;

import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;


/**
* MLInput which supports a question answering algorithm
* Inputs are question and context. Output is the answer
*/
@org.opensearch.ml.common.annotation.MLInput(functionNames = {FunctionName.QUESTION_ANSWERING})
public class QuestionAnsweringMLInput extends MLInput {

public QuestionAnsweringMLInput(FunctionName algorithm, MLInputDataset dataset) {
super(algorithm, null, dataset);
}

public QuestionAnsweringMLInput(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) {
QuestionAnsweringInputDataSet ds = (QuestionAnsweringInputDataSet) this.inputDataset;
String question = ds.getQuestion();
String context = ds.getContext();
builder.field(QUESTION_FIELD, question);
builder.field(CONTEXT_FIELD, context);
}
builder.endObject();
return builder;
}

public QuestionAnsweringMLInput(XContentParser parser, FunctionName functionName) throws IOException {
super();
this.algorithm = functionName;
String question = null;
String context = 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 QUESTION_FIELD:
question = parser.text();
case CONTEXT_FIELD:
context = parser.text();
default:
parser.skipChildren();
break;
}
}
if(question == null) {
throw new IllegalArgumentException("Question is not provided");
}
if(context == null) {
throw new IllegalArgumentException("Context is not provided");
}
inputDataset = new QuestionAnsweringInputDataSet(question, context);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ private void testClassLoader_MLInput_DlModel(FunctionName functionName) throws I
@Test
public void testClassLoader_MLInput() throws IOException {
testClassLoader_MLInput_DlModel(FunctionName.TEXT_EMBEDDING);
testClassLoader_MLInput_DlModel(FunctionName.QUESTION_ANSWERING);
testClassLoader_MLInput_DlModel(FunctionName.SPARSE_ENCODING);
testClassLoader_MLInput_DlModel(FunctionName.SPARSE_TOKENIZE);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.ml.common.dataset;

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;

import java.io.IOException;
import java.util.List;

import static org.junit.Assert.assertThrows;

public class QuestionAnsweringInputDatasetTest {

@Test
public void testStreaming() throws IOException {
String question = "What color is apple";
String context = "I like Apples. They are red";
QuestionAnsweringInputDataSet dataset = QuestionAnsweringInputDataSet.builder().question(question).context(context).build();
BytesStreamOutput outbytes = new BytesStreamOutput();
StreamOutput osso = new OutputStreamStreamOutput(outbytes);
dataset.writeTo(osso);
StreamInput in = new BytesStreamInput(BytesReference.toBytes(outbytes.bytes()));
QuestionAnsweringInputDataSet newDs = (QuestionAnsweringInputDataSet) MLInputDataset.fromStream(in);
assert (question.equals("What color is apple"));
assert (context.equals("I like Apples. They are red"));
}

@Test
public void noContext_ThenFail() {
String question = "What color is apple";
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
() -> QuestionAnsweringInputDataSet.builder().question(question).build());
assert (e.getMessage().equals("Context is not provided"));
}

@Test
public void noQuestion_ThenFail() {
String context = "I like Apples. They are red";
assertThrows(IllegalArgumentException.class,
() -> QuestionAnsweringInputDataSet.builder().context(context).build());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.ml.common.input.nlp;

import org.junit.Before;
import org.junit.Test;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentType;
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;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.ToXContent;
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.QuestionAnsweringInputDataSet;
import org.opensearch.ml.common.input.MLInput;
import org.opensearch.search.SearchModule;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertThrows;

public class QuestionAnsweringMLInputTest {

MLInput input;

private final FunctionName algorithm = FunctionName.QUESTION_ANSWERING;

@Before
public void setup() {
String question = "What color is apple";
String context = "I like Apples. They are red";
MLInputDataset dataset = QuestionAnsweringInputDataSet.builder().question(question).context(context).build();
input = new QuestionAnsweringMLInput(algorithm, dataset);
}

@Test
public void testXContent_IsInternallyConsistent() throws IOException {
XContentBuilder builder = MediaTypeRegistry.contentBuilder(XContentType.JSON);
input.toXContent(builder, ToXContent.EMPTY_PARAMS);
String jsonStr = builder.toString();
XContentParser parser = XContentType.JSON.xContent()
.createParser(new NamedXContentRegistry(new SearchModule(Settings.EMPTY,
Collections.emptyList()).getNamedXContents()), null, jsonStr);
parser.nextToken();

MLInput parsedInput = MLInput.parse(parser, input.getFunctionName().name());
assert (parsedInput instanceof QuestionAnsweringMLInput);
QuestionAnsweringMLInput parsedQAMLI = (QuestionAnsweringMLInput) parsedInput;
String question = ((QuestionAnsweringInputDataSet) parsedQAMLI.getInputDataset()).getQuestion();
String context = ((QuestionAnsweringInputDataSet) parsedQAMLI.getInputDataset()).getContext();
assert (question.equals("What color is apple"));
assert (context.equals("I like Apples. They are red"));
}

@Test
public void testXContent_String() throws IOException {
XContentBuilder builder = MediaTypeRegistry.contentBuilder(XContentType.JSON);
input.toXContent(builder, ToXContent.EMPTY_PARAMS);
String jsonStr = builder.toString();
assert (jsonStr.equals("{\"algorithm\":\"QUESTION_ANSWERING\",\"question\":\"What color is apple\",\"context\":\"I like Apples. They are red\"}"));
}

@Test
public void testParseJson() throws IOException {
String json = "{\"algorithm\":\"QUESTION_ANSWERING\",\"question\":\"What color is apple\",\"context\":\"I like Apples. They are red\"}";
XContentParser parser = XContentType.JSON.xContent()
.createParser(new NamedXContentRegistry(new SearchModule(Settings.EMPTY,
Collections.emptyList()).getNamedXContents()), null, json);
parser.nextToken();

MLInput parsedInput = MLInput.parse(parser, input.getFunctionName().name());
assert (parsedInput instanceof QuestionAnsweringMLInput);
QuestionAnsweringMLInput parsedQAMLI = (QuestionAnsweringMLInput) parsedInput;
String question = ((QuestionAnsweringInputDataSet) parsedQAMLI.getInputDataset()).getQuestion();
String context = ((QuestionAnsweringInputDataSet) parsedQAMLI.getInputDataset()).getContext();
assert (question.equals("What color is apple"));
assert (context.equals("I like Apples. They are red"));
}

@Test
public void testStreaming() throws IOException {
BytesStreamOutput outbytes = new BytesStreamOutput();
StreamOutput osso = new OutputStreamStreamOutput(outbytes);
input.writeTo(osso);
StreamInput in = new BytesStreamInput(BytesReference.toBytes(outbytes.bytes()));
QuestionAnsweringMLInput newInput = new QuestionAnsweringMLInput(in);
String newQuestion = ((QuestionAnsweringInputDataSet) newInput.getInputDataset()).getQuestion();
String oldQuestion = ((QuestionAnsweringInputDataSet) input.getInputDataset()).getQuestion();
assert (newQuestion.equals(oldQuestion));
}

}
Loading

0 comments on commit 7b6229a

Please sign in to comment.