Skip to content

Commit

Permalink
Moving invalid model type exception to shared ServiceUtils function
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-rubinstein committed Sep 24, 2024
1 parent f757efd commit d9a58d0
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@ public static ElasticsearchStatusException unknownSettingsError(Map<String, Obje
);
}

public static ElasticsearchStatusException invalidModelTypeForUpdateEmbeddingsModelDetails(Class<? extends Model> invalidModelType) {
throw new ElasticsearchStatusException(
Strings.format("Can't update embedding details for model with unexpected type %s", invalidModelType),
RestStatus.BAD_REQUEST
);
}

public static String missingSettingErrorMsg(String settingName, String scope) {
return Strings.format("[%s] does not contain the required setting [%s]", scope, settingName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.elasticsearch.TransportVersions;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.Strings;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.inference.ChunkedInferenceServiceResults;
import org.elasticsearch.inference.ChunkingOptions;
Expand All @@ -32,6 +31,7 @@
import org.elasticsearch.xpack.inference.services.ConfigurationParseContext;
import org.elasticsearch.xpack.inference.services.SenderService;
import org.elasticsearch.xpack.inference.services.ServiceComponents;
import org.elasticsearch.xpack.inference.services.ServiceUtils;
import org.elasticsearch.xpack.inference.services.googleaistudio.completion.GoogleAiStudioCompletionModel;
import org.elasticsearch.xpack.inference.services.googleaistudio.embeddings.GoogleAiStudioEmbeddingsModel;
import org.elasticsearch.xpack.inference.services.googleaistudio.embeddings.GoogleAiStudioEmbeddingsServiceSettings;
Expand Down Expand Up @@ -209,10 +209,7 @@ public Model updateModelWithEmbeddingDetails(Model model, int embeddingSize) {

return new GoogleAiStudioEmbeddingsModel(embeddingsModel, updatedServiceSettings);
} else {
throw new ElasticsearchStatusException(
Strings.format("Can't update embedding details for model with unexpected type %s", model.getClass()),
RestStatus.BAD_REQUEST
);
throw ServiceUtils.invalidModelTypeForUpdateEmbeddingsModelDetails(model.getClass());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.elasticsearch.TransportVersions;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.Strings;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.inference.ChunkedInferenceServiceResults;
import org.elasticsearch.inference.ChunkingOptions;
Expand All @@ -27,6 +26,7 @@
import org.elasticsearch.xpack.inference.external.http.sender.HttpRequestSender;
import org.elasticsearch.xpack.inference.services.ConfigurationParseContext;
import org.elasticsearch.xpack.inference.services.ServiceComponents;
import org.elasticsearch.xpack.inference.services.ServiceUtils;
import org.elasticsearch.xpack.inference.services.huggingface.elser.HuggingFaceElserModel;
import org.elasticsearch.xpack.inference.services.huggingface.embeddings.HuggingFaceEmbeddingsModel;
import org.elasticsearch.xpack.inference.services.validation.ModelValidatorBuilder;
Expand Down Expand Up @@ -89,10 +89,7 @@ public Model updateModelWithEmbeddingDetails(Model model, int embeddingSize) {

return new HuggingFaceEmbeddingsModel(embeddingsModel, updatedServiceSettings);
} else {
throw new ElasticsearchStatusException(
Strings.format("Can't update embedding details for model with unexpected type %s", model.getClass()),
RestStatus.BAD_REQUEST
);
throw ServiceUtils.invalidModelTypeForUpdateEmbeddingsModelDetails(model.getClass());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.elasticsearch.TransportVersion;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.Strings;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.inference.ChunkedInferenceServiceResults;
import org.elasticsearch.inference.ChunkingOptions;
Expand All @@ -31,6 +30,7 @@
import org.elasticsearch.xpack.inference.services.ConfigurationParseContext;
import org.elasticsearch.xpack.inference.services.SenderService;
import org.elasticsearch.xpack.inference.services.ServiceComponents;
import org.elasticsearch.xpack.inference.services.ServiceUtils;
import org.elasticsearch.xpack.inference.services.mistral.embeddings.MistralEmbeddingsModel;
import org.elasticsearch.xpack.inference.services.mistral.embeddings.MistralEmbeddingsServiceSettings;
import org.elasticsearch.xpack.inference.services.validation.ModelValidatorBuilder;
Expand Down Expand Up @@ -236,10 +236,7 @@ public Model updateModelWithEmbeddingDetails(Model model, int embeddingSize) {
);
return new MistralEmbeddingsModel(embeddingsModel, updatedServiceSettings);
} else {
throw new ElasticsearchStatusException(
Strings.format("Can't update embedding details for model with unexpected type %s", model.getClass()),
RestStatus.BAD_REQUEST
);
throw ServiceUtils.invalidModelTypeForUpdateEmbeddingsModelDetails(model.getClass());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.elasticsearch.TransportVersions;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.Strings;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.inference.ChunkedInferenceServiceResults;
import org.elasticsearch.inference.ChunkingOptions;
Expand All @@ -32,6 +31,7 @@
import org.elasticsearch.xpack.inference.services.ConfigurationParseContext;
import org.elasticsearch.xpack.inference.services.SenderService;
import org.elasticsearch.xpack.inference.services.ServiceComponents;
import org.elasticsearch.xpack.inference.services.ServiceUtils;
import org.elasticsearch.xpack.inference.services.openai.completion.OpenAiChatCompletionModel;
import org.elasticsearch.xpack.inference.services.openai.embeddings.OpenAiEmbeddingsModel;
import org.elasticsearch.xpack.inference.services.openai.embeddings.OpenAiEmbeddingsServiceSettings;
Expand Down Expand Up @@ -269,10 +269,7 @@ public Model updateModelWithEmbeddingDetails(Model model, int embeddingSize) {

return new OpenAiEmbeddingsModel(embeddingsModel, updatedServiceSettings);
} else {
throw new ElasticsearchStatusException(
Strings.format("Can't update embedding details for model with unexpected type %s", model.getClass()),
RestStatus.BAD_REQUEST
);
throw ServiceUtils.invalidModelTypeForUpdateEmbeddingsModelDetails(model.getClass());
}
}

Expand Down

0 comments on commit d9a58d0

Please sign in to comment.