Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow input null for text docs input #1401

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ public TextDocsMLInput(XContentParser parser, FunctionName functionName) throws
case TEXT_DOCS_FIELD:
ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.currentToken(), parser);
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
docs.add(parser.text());
if (parser.currentToken() == null || parser.currentToken() == XContentParser.Token.VALUE_NULL) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the input is [null, null]?

Copy link
Collaborator Author

@ylwu-amzn ylwu-amzn Sep 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This depends on the model. If model can't accept, it will throw exception.

docs.add(null);
} else {
docs.add(parser.text());
}
}
break;
case RESULT_FILTER_FIELD:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.opensearch.core.common.Strings;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.NamedXContentRegistry;
Expand All @@ -26,6 +24,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class TextDocsMLInputTest {
Expand All @@ -52,22 +51,22 @@ public void parseTextDocsMLInput() throws IOException {
input.toXContent(builder, ToXContent.EMPTY_PARAMS);
String jsonStr = builder.toString();
System.out.println(jsonStr);
parseMLInput(jsonStr);
parseMLInput(jsonStr, 2);
}

@Test
public void parseTextDocsMLInput_OldWay() throws IOException {
String jsonStr = "{\"text_docs\": [ \"doc1\", \"doc2\" ],\"return_number\": true, \"return_bytes\": true,\"target_response\": [ \"field1\" ], \"target_response_positions\": [2]}";
parseMLInput(jsonStr);
String jsonStr = "{\"text_docs\": [ \"doc1\", \"doc2\", null ],\"return_number\": true, \"return_bytes\": true,\"target_response\": [ \"field1\" ], \"target_response_positions\": [2]}";
parseMLInput(jsonStr, 3);
}

@Test
public void parseTextDocsMLInput_NewWay() throws IOException {
String jsonStr = "{\"text_docs\":[\"doc1\",\"doc2\"],\"result_filter\":{\"return_bytes\":true,\"return_number\":true,\"target_response\":[\"field1\"], \"target_response_positions\": [2]}}";
parseMLInput(jsonStr);
parseMLInput(jsonStr, 2);
}

private void parseMLInput(String jsonStr) throws IOException {
private void parseMLInput(String jsonStr, int docSize) throws IOException {
XContentParser parser = XContentType.JSON.xContent()
.createParser(new NamedXContentRegistry(new SearchModule(Settings.EMPTY,
Collections.emptyList()).getNamedXContents()), null, jsonStr);
Expand All @@ -78,9 +77,12 @@ private void parseMLInput(String jsonStr) throws IOException {
assertEquals(input.getFunctionName(), parsedInput.getFunctionName());
assertEquals(input.getInputDataset().getInputDataType(), parsedInput.getInputDataset().getInputDataType());
TextDocsInputDataSet inputDataset = (TextDocsInputDataSet) parsedInput.getInputDataset();
assertEquals(2, inputDataset.getDocs().size());
assertEquals(docSize, inputDataset.getDocs().size());
assertEquals("doc1", inputDataset.getDocs().get(0));
assertEquals("doc2", inputDataset.getDocs().get(1));
if (inputDataset.getDocs().size() > 2) {
assertNull(inputDataset.getDocs().get(2));
}
assertNotNull(inputDataset.getResultFilter());
assertTrue(inputDataset.getResultFilter().isReturnBytes());
assertTrue(inputDataset.getResultFilter().isReturnNumber());
Expand Down