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

Block commas in model description #1692

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add check in rest handler
Signed-off-by: Ryan Bogan <rbogan@amazon.com>
  • Loading branch information
ryanbogan committed May 7, 2024
commit 92de7dadbe8c270b9c1421f92b0d8e1398cca6c6
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ private TrainingModelRequest createTransportRequest(RestRequest restRequest) thr
searchSize = (Integer) NumberFieldMapper.NumberType.INTEGER.parse(parser.objectBytes(), false);
} else if (MODEL_DESCRIPTION.equals(fieldName) && ensureNotSet(fieldName, description)) {
description = parser.textOrNull();
if (description.contains(",")) {
throw new IllegalArgumentException("Model description cannot contain any commas: ','");
}
} else {
throw new IllegalArgumentException("Unable to parse token. \"" + fieldName + "\" is not a valid " + "parameter.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.opensearch.client.Response;
import org.opensearch.client.ResponseException;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.xcontent.MediaTypeRegistry;
Expand Down Expand Up @@ -192,6 +193,68 @@ public void testTrainModel_fail_tooMuchData() throws Exception {
assertTrainingFails(modelId, 30, 1000);
}

public void testTrainModel_fail_commaInDescription() throws Exception {
// Test checks that training when passing in an id succeeds

String modelId = "test-model-id";
String trainingIndexName = "train-index";
String trainingFieldName = "train-field";
int dimension = 8;

// Create a training index and randomly ingest data into it
createBasicKnnIndex(trainingIndexName, trainingFieldName, dimension);
int trainingDataCount = 200;
bulkIngestRandomVectors(trainingIndexName, trainingFieldName, trainingDataCount, dimension);
ryanbogan marked this conversation as resolved.
Show resolved Hide resolved

// Call the train API with this definition:
/*
{
"training_index": "train_index",
"training_field": "train_field",
"dimension": 8,
"description": "this should be allowed to be null",
"method": {
"name":"ivf",
"engine":"faiss",
"space_type": "l2",
"parameters":{
"nlist":1,
"encoder":{
"name":"pq",
"parameters":{
"code_size":2,
"m": 2
}
}
}
}
}
*/
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()
.field(NAME, "ivf")
.field(KNN_ENGINE, "faiss")
.field(METHOD_PARAMETER_SPACE_TYPE, "l2")
.startObject(PARAMETERS)
.field(METHOD_PARAMETER_NLIST, 1)
.startObject(METHOD_ENCODER_PARAMETER)
.field(NAME, "pq")
.startObject(PARAMETERS)
.field(ENCODER_PARAMETER_PQ_CODE_SIZE, 2)
.field(ENCODER_PARAMETER_PQ_M, 2)
.endObject()
.endObject()
.endObject()
.endObject();
Map<String, Object> method = xContentBuilderToMap(builder);

Exception e = expectThrows(
ResponseException.class,
() -> trainModel(modelId, trainingIndexName, trainingFieldName, dimension, method, "dummy description, with comma")
);
assertTrue(e.getMessage().contains("Model description cannot contain any commas: ','"));
}

public void testTrainModel_success_withId() throws Exception {
// Test checks that training when passing in an id succeeds

Expand Down
Loading