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

[Inference API] Use extractOptionalPositiveInteger in MistralEmbeddingsServiceSettings #110485

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 @@ -33,7 +33,6 @@
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractOptionalPositiveInteger;
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractRequiredString;
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractSimilarity;
import static org.elasticsearch.xpack.inference.services.ServiceUtils.removeAsType;
import static org.elasticsearch.xpack.inference.services.mistral.MistralConstants.MODEL_FIELD;

public class MistralEmbeddingsServiceSettings extends FilteredXContentObject implements ServiceSettings {
Expand Down Expand Up @@ -67,7 +66,7 @@ public static MistralEmbeddingsServiceSettings fromMap(Map<String, Object> map,
MistralService.NAME,
context
);
Integer dims = removeAsType(map, DIMENSIONS, Integer.class);
Integer dims = extractOptionalPositiveInteger(map, DIMENSIONS, ModelConfigurations.SERVICE_SETTINGS, validationException);

if (validationException.validationErrors().isEmpty() == false) {
throw validationException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package org.elasticsearch.xpack.inference.services.mistral.embeddings;

import org.elasticsearch.common.Strings;
import org.elasticsearch.common.ValidationException;
import org.elasticsearch.common.io.stream.ByteArrayStreamInput;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.core.Nullable;
Expand All @@ -27,6 +28,7 @@
import java.util.Map;

import static org.elasticsearch.xpack.inference.services.ServiceFields.SIMILARITY;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;

public class MistralEmbeddingsServiceSettingsTests extends ESTestCase {
Expand Down Expand Up @@ -77,6 +79,84 @@ public void testFromMap_PersistentContext_DoesNotThrowException_WhenDimensionsIs
assertThat(serviceSettings, is(new MistralEmbeddingsServiceSettings(model, null, null, null, null)));
}

public void testFromMap_ThrowsException_WhenDimensionsAreZero() {
var model = "mistral-embed";
var dimensions = 0;

var settingsMap = createRequestSettingsMap(model, dimensions, null, SimilarityMeasure.COSINE);

var thrownException = expectThrows(
ValidationException.class,
() -> MistralEmbeddingsServiceSettings.fromMap(settingsMap, ConfigurationParseContext.REQUEST)
);

assertThat(
thrownException.getMessage(),
containsString("Validation Failed: 1: [service_settings] Invalid value [0]. [dimensions] must be a positive integer;")
);
}

public void testFromMap_ThrowsException_WhenDimensionsAreNegative() {
var model = "mistral-embed";
var dimensions = randomNegativeInt();

var settingsMap = createRequestSettingsMap(model, dimensions, null, SimilarityMeasure.COSINE);

var thrownException = expectThrows(
ValidationException.class,
() -> MistralEmbeddingsServiceSettings.fromMap(settingsMap, ConfigurationParseContext.REQUEST)
);

assertThat(
thrownException.getMessage(),
containsString(
Strings.format(
"Validation Failed: 1: [service_settings] Invalid value [%d]. [dimensions] must be a positive integer;",
dimensions
)
)
);
}

public void testFromMap_ThrowsException_WhenMaxInputTokensAreZero() {
var model = "mistral-embed";
var maxInputTokens = 0;

var settingsMap = createRequestSettingsMap(model, null, maxInputTokens, SimilarityMeasure.COSINE);

var thrownException = expectThrows(
ValidationException.class,
() -> MistralEmbeddingsServiceSettings.fromMap(settingsMap, ConfigurationParseContext.REQUEST)
);

assertThat(
thrownException.getMessage(),
containsString("Validation Failed: 1: [service_settings] Invalid value [0]. [max_input_tokens] must be a positive integer;")
);
}

public void testFromMap_ThrowsException_WhenMaxInputTokensAreNegative() {
var model = "mistral-embed";
var maxInputTokens = randomNegativeInt();

var settingsMap = createRequestSettingsMap(model, null, maxInputTokens, SimilarityMeasure.COSINE);

var thrownException = expectThrows(
ValidationException.class,
() -> MistralEmbeddingsServiceSettings.fromMap(settingsMap, ConfigurationParseContext.REQUEST)
);

assertThat(
thrownException.getMessage(),
containsString(
Strings.format(
"Validation Failed: 1: [service_settings] Invalid value [%d]. [max_input_tokens] must be a positive integer;",
maxInputTokens
)
)
);
}

public void testFromMap_PersistentContext_DoesNotThrowException_WhenSimilarityIsPresent() {
var model = "mistral-embed";

Expand Down