|
| 1 | +package ai.dat.embedder.azure; |
| 2 | + |
| 3 | +import ai.dat.core.configuration.ConfigOption; |
| 4 | +import ai.dat.core.configuration.ConfigOptions; |
| 5 | +import ai.dat.core.configuration.ReadableConfig; |
| 6 | +import ai.dat.core.factories.EmbeddingModelFactory; |
| 7 | +import ai.dat.core.utils.FactoryUtil; |
| 8 | +import com.google.common.base.Preconditions; |
| 9 | +import dev.langchain4j.model.azure.AzureOpenAiEmbeddingModel; |
| 10 | +import dev.langchain4j.model.embedding.EmbeddingModel; |
| 11 | + |
| 12 | +import java.time.Duration; |
| 13 | +import java.util.LinkedHashSet; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.Set; |
| 17 | + |
| 18 | +/** |
| 19 | + * |
| 20 | + */ |
| 21 | +public class AzureOpenAiEmbeddingModelFactory implements EmbeddingModelFactory { |
| 22 | + |
| 23 | + public static final String IDENTIFIER = "azure-openai"; |
| 24 | + |
| 25 | + public static final ConfigOption<String> ENDPOINT = |
| 26 | + ConfigOptions.key("endpoint") |
| 27 | + .stringType() |
| 28 | + .noDefaultValue() |
| 29 | + .withDescription(""" |
| 30 | + Supported Azure OpenAI endpoints |
| 31 | + (protocol and hostname, for example: https://aoairesource.openai.azure.com. \ |
| 32 | + Replace "aoairesource" with your Azure OpenAI resource name). |
| 33 | + https://{your-resource-name}.openai.azure.com |
| 34 | + """); |
| 35 | + |
| 36 | + public static final ConfigOption<String> DEPLOYMENT_ID = |
| 37 | + ConfigOptions.key("deployment-id") |
| 38 | + .stringType() |
| 39 | + .noDefaultValue() |
| 40 | + .withDescription("Deployment ID of the model which was deployed."); |
| 41 | + |
| 42 | + public static final ConfigOption<String> API_VERSION = |
| 43 | + ConfigOptions.key("api-version") |
| 44 | + .stringType() |
| 45 | + .noDefaultValue() |
| 46 | + .withDescription("The API version of Azure OpenAI."); |
| 47 | + |
| 48 | + public static final ConfigOption<String> API_KEY = |
| 49 | + ConfigOptions.key("api-key") |
| 50 | + .stringType() |
| 51 | + .noDefaultValue() |
| 52 | + .withDescription("Provide Azure OpenAI API key here."); |
| 53 | + |
| 54 | + public static final ConfigOption<Integer> DIMENSIONS = |
| 55 | + ConfigOptions.key("dimensions") |
| 56 | + .intType() |
| 57 | + .noDefaultValue() |
| 58 | + .withDescription("Azure OpenAI embedding model dimensions"); |
| 59 | + |
| 60 | + public static final ConfigOption<Duration> TIMEOUT = |
| 61 | + ConfigOptions.key("timeout") |
| 62 | + .durationType() |
| 63 | + .noDefaultValue() |
| 64 | + .withDescription("The maximum time allowed for the API call to complete."); |
| 65 | + |
| 66 | + public static final ConfigOption<Integer> MAX_RETRIES = |
| 67 | + ConfigOptions.key("max-retries") |
| 68 | + .intType() |
| 69 | + .defaultValue(2) |
| 70 | + .withDescription("The maximum number of retries in case of API call failure."); |
| 71 | + |
| 72 | + public static final ConfigOption<Boolean> LOG_REQUESTS_AND_RESPONSES = |
| 73 | + ConfigOptions.key("log-requests-and-responses") |
| 74 | + .booleanType() |
| 75 | + .defaultValue(false) |
| 76 | + .withDescription("Whether to print the LLM requests and responses log"); |
| 77 | + |
| 78 | + public static final ConfigOption<Map<String, String>> CUSTOM_HEADERS = |
| 79 | + ConfigOptions.key("custom-headers") |
| 80 | + .mapType() |
| 81 | + .noDefaultValue() |
| 82 | + .withDescription("Custom HTTP headers. " + |
| 83 | + "For example: {\"content-type\": \"application/json\", " + |
| 84 | + "\"accept\": \"application/json, text/event-stream\"}"); |
| 85 | + |
| 86 | + @Override |
| 87 | + public String factoryIdentifier() { |
| 88 | + return IDENTIFIER; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public Set<ConfigOption<?>> requiredOptions() { |
| 93 | + return new LinkedHashSet<>(List.of(ENDPOINT, DEPLOYMENT_ID, API_VERSION, API_KEY)); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public Set<ConfigOption<?>> optionalOptions() { |
| 98 | + return new LinkedHashSet<>(List.of(DIMENSIONS, TIMEOUT, |
| 99 | + MAX_RETRIES, LOG_REQUESTS_AND_RESPONSES, CUSTOM_HEADERS)); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public Set<ConfigOption<?>> fingerprintOptions() { |
| 104 | + return Set.of(ENDPOINT, DEPLOYMENT_ID, API_VERSION); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public EmbeddingModel create(ReadableConfig config) { |
| 109 | + FactoryUtil.validateFactoryOptions(this, config); |
| 110 | + validateConfigOptions(config); |
| 111 | + |
| 112 | + String endpoint = config.get(ENDPOINT); |
| 113 | + String deploymentId = config.get(DEPLOYMENT_ID); |
| 114 | + String apiVersion = config.get(API_VERSION); |
| 115 | + String apiKey = config.get(API_KEY); |
| 116 | + |
| 117 | + AzureOpenAiEmbeddingModel.Builder builder = AzureOpenAiEmbeddingModel.builder() |
| 118 | + .endpoint(endpoint) |
| 119 | + .deploymentName(deploymentId) |
| 120 | + .serviceVersion(apiVersion) |
| 121 | + .apiKey(apiKey); |
| 122 | + |
| 123 | + config.getOptional(TIMEOUT).ifPresent(builder::timeout); |
| 124 | + config.getOptional(DIMENSIONS).ifPresent(builder::dimensions); |
| 125 | + config.getOptional(MAX_RETRIES).ifPresent(builder::maxRetries); |
| 126 | + config.getOptional(LOG_REQUESTS_AND_RESPONSES).ifPresent(builder::logRequestsAndResponses); |
| 127 | + config.getOptional(CUSTOM_HEADERS).ifPresent(builder::customHeaders); |
| 128 | + |
| 129 | + return builder.build(); |
| 130 | + } |
| 131 | + |
| 132 | + private void validateConfigOptions(ReadableConfig config) { |
| 133 | + Integer maxRetries = config.get(MAX_RETRIES); |
| 134 | + Preconditions.checkArgument(maxRetries >= 0, |
| 135 | + "'" + MAX_RETRIES.key() + "' value must be greater than or equal to 0"); |
| 136 | + config.getOptional(DIMENSIONS) |
| 137 | + .ifPresent(d -> Preconditions.checkArgument(d > 0, |
| 138 | + "'" + DIMENSIONS.key() + "' value must be greater than 0")); |
| 139 | + } |
| 140 | +} |
0 commit comments