Skip to content

Commit

Permalink
Fix #1098: prevent NPE for unknown embedding provider id (#1099)
Browse files Browse the repository at this point in the history
  • Loading branch information
tatu-at-datastax authored May 17, 2024
1 parent d79cf64 commit 68fe36b
Showing 1 changed file with 19 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io.stargate.embedding.gateway.EmbeddingService;
import io.stargate.sgv2.jsonapi.config.OperationsConfig;
import io.stargate.sgv2.jsonapi.exception.ErrorCode;
import io.stargate.sgv2.jsonapi.exception.JsonApiException;
import io.stargate.sgv2.jsonapi.service.embedding.configuration.EmbeddingProviderConfigStore;
import io.stargate.sgv2.jsonapi.service.embedding.configuration.ProviderConstants;
import io.stargate.sgv2.jsonapi.service.embedding.gateway.EmbeddingGatewayClient;
Expand Down Expand Up @@ -96,33 +95,29 @@ private synchronized EmbeddingProvider addService(
}

if (configuration.serviceProvider().equals(ProviderConstants.CUSTOM)) {
Optional<Class<?>> clazz = configuration.implementationClass();
if (!clazz.isPresent()) {
throw ErrorCode.VECTORIZE_SERVICE_TYPE_UNAVAILABLE.toApiException("custom class undefined");
}
try {
Optional<Class<?>> clazz = configuration.implementationClass();
if (clazz.isPresent()) {
final EmbeddingProvider customEmbeddingProvider =
(EmbeddingProvider) clazz.get().getConstructor().newInstance();
return customEmbeddingProvider;
} else {
throw new JsonApiException(
ErrorCode.VECTORIZE_SERVICE_TYPE_UNAVAILABLE,
ErrorCode.VECTORIZE_SERVICE_TYPE_UNAVAILABLE.getMessage() + "custom class undefined");
}
return (EmbeddingProvider) clazz.get().getConstructor().newInstance();
} catch (Exception e) {
throw new JsonApiException(
ErrorCode.VECTORIZE_SERVICE_TYPE_UNAVAILABLE,
ErrorCode.VECTORIZE_SERVICE_TYPE_UNAVAILABLE.getMessage()
+ "custom class provided does not resolve to EmbeddingProvider "
+ configuration.implementationClass().get().getCanonicalName());
throw ErrorCode.VECTORIZE_SERVICE_TYPE_UNAVAILABLE.toApiException(
"custom class provided ('%s') does not resolve to EmbeddingProvider",
clazz.get().getCanonicalName());
}
}

return providersMap
.get(configuration.serviceProvider())
.create(
configuration.requestConfiguration(),
configuration.baseUrl(),
modelName,
dimension,
vectorizeServiceParameters);
ProviderConstructor ctor = providersMap.get(configuration.serviceProvider());
if (ctor == null) {
throw ErrorCode.VECTORIZE_SERVICE_TYPE_UNAVAILABLE.toApiException(
"unknown service provider '%s'", configuration.serviceProvider());
}
return ctor.create(
configuration.requestConfiguration(),
configuration.baseUrl(),
modelName,
dimension,
vectorizeServiceParameters);
}
}

0 comments on commit 68fe36b

Please sign in to comment.