Skip to content

Commit 8813936

Browse files
Refactor OpenShift AI service settings to streamline common settings extraction and validation
1 parent aeec397 commit 8813936

File tree

4 files changed

+76
-62
lines changed

4 files changed

+76
-62
lines changed

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/openshiftai/OpenShiftAiServiceSettings.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,28 @@
88
package org.elasticsearch.xpack.inference.services.openshiftai;
99

1010
import org.elasticsearch.TransportVersion;
11+
import org.elasticsearch.common.ValidationException;
1112
import org.elasticsearch.common.io.stream.StreamInput;
1213
import org.elasticsearch.common.io.stream.StreamOutput;
1314
import org.elasticsearch.core.Nullable;
15+
import org.elasticsearch.inference.ModelConfigurations;
1416
import org.elasticsearch.inference.ServiceSettings;
1517
import org.elasticsearch.xcontent.XContentBuilder;
18+
import org.elasticsearch.xpack.inference.services.ConfigurationParseContext;
1619
import org.elasticsearch.xpack.inference.services.settings.FilteredXContentObject;
1720
import org.elasticsearch.xpack.inference.services.settings.RateLimitSettings;
1821

1922
import java.io.IOException;
2023
import java.net.URI;
24+
import java.util.Map;
2125
import java.util.Objects;
26+
import java.util.function.Function;
2227

2328
import static org.elasticsearch.xpack.inference.services.ServiceFields.MODEL_ID;
2429
import static org.elasticsearch.xpack.inference.services.ServiceFields.URL;
2530
import static org.elasticsearch.xpack.inference.services.ServiceUtils.createUri;
31+
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractOptionalString;
32+
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractUri;
2633

2734
/**
2835
* Represents the settings for an OpenShift AI service.
@@ -119,4 +126,53 @@ protected XContentBuilder toXContentFragmentOfExposedFields(XContentBuilder buil
119126
rateLimitSettings.toXContent(builder, params);
120127
return builder;
121128
}
129+
130+
/**
131+
* Creates an instance of T from the provided map using the given factory function.
132+
* @param map the map containing the service settings
133+
* @param context the context for parsing configuration settings
134+
* @param factory the factory function to create an instance of T
135+
* @return an instance of T
136+
* @param <T> the type of {@link OpenShiftAiServiceSettings} to create
137+
*/
138+
protected static <T extends OpenShiftAiServiceSettings> T fromMap(
139+
Map<String, Object> map,
140+
ConfigurationParseContext context,
141+
Function<OpenShiftAiCommonServiceSettings, T> factory
142+
) {
143+
var validationException = new ValidationException();
144+
var commonServiceSettings = extractOpenShiftAiCommonServiceSettings(map, context, validationException);
145+
146+
if (validationException.validationErrors().isEmpty() == false) {
147+
throw validationException;
148+
}
149+
150+
return factory.apply(commonServiceSettings);
151+
}
152+
153+
/**
154+
* Extracts common OpenShift AI service settings from the provided map.
155+
* @param map the map containing the service settings
156+
* @param context the context for parsing configuration settings
157+
* @param validationException the validation exception to collect validation errors
158+
* @return an instance of {@link OpenShiftAiCommonServiceSettings}
159+
*/
160+
protected static OpenShiftAiCommonServiceSettings extractOpenShiftAiCommonServiceSettings(
161+
Map<String, Object> map,
162+
ConfigurationParseContext context,
163+
ValidationException validationException
164+
) {
165+
var model = extractOptionalString(map, MODEL_ID, ModelConfigurations.SERVICE_SETTINGS, validationException);
166+
var uri = extractUri(map, URL, validationException);
167+
var rateLimitSettings = RateLimitSettings.of(
168+
map,
169+
DEFAULT_RATE_LIMIT_SETTINGS,
170+
validationException,
171+
OpenShiftAiService.NAME,
172+
context
173+
);
174+
return new OpenShiftAiCommonServiceSettings(model, uri, rateLimitSettings);
175+
}
176+
177+
protected record OpenShiftAiCommonServiceSettings(String model, URI uri, RateLimitSettings rateLimitSettings) {}
122178
}

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/openshiftai/completion/OpenShiftAiChatCompletionServiceSettings.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
import org.elasticsearch.common.ValidationException;
1111
import org.elasticsearch.common.io.stream.StreamInput;
1212
import org.elasticsearch.core.Nullable;
13-
import org.elasticsearch.inference.ModelConfigurations;
1413
import org.elasticsearch.xpack.inference.services.ConfigurationParseContext;
15-
import org.elasticsearch.xpack.inference.services.openshiftai.OpenShiftAiService;
1614
import org.elasticsearch.xpack.inference.services.openshiftai.OpenShiftAiServiceSettings;
1715
import org.elasticsearch.xpack.inference.services.settings.RateLimitSettings;
1816

@@ -21,11 +19,7 @@
2119
import java.util.Map;
2220
import java.util.Objects;
2321

24-
import static org.elasticsearch.xpack.inference.services.ServiceFields.MODEL_ID;
25-
import static org.elasticsearch.xpack.inference.services.ServiceFields.URL;
2622
import static org.elasticsearch.xpack.inference.services.ServiceUtils.createUri;
27-
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractOptionalString;
28-
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractUri;
2923

3024
/**
3125
* Represents the settings for an OpenShift AI chat completion service.
@@ -43,23 +37,15 @@ public class OpenShiftAiChatCompletionServiceSettings extends OpenShiftAiService
4337
* @throws ValidationException if required fields are missing or invalid
4438
*/
4539
public static OpenShiftAiChatCompletionServiceSettings fromMap(Map<String, Object> map, ConfigurationParseContext context) {
46-
ValidationException validationException = new ValidationException();
47-
48-
var model = extractOptionalString(map, MODEL_ID, ModelConfigurations.SERVICE_SETTINGS, validationException);
49-
var uri = extractUri(map, URL, validationException);
50-
RateLimitSettings rateLimitSettings = RateLimitSettings.of(
40+
return fromMap(
5141
map,
52-
DEFAULT_RATE_LIMIT_SETTINGS,
53-
validationException,
54-
OpenShiftAiService.NAME,
55-
context
42+
context,
43+
commonServiceSettings -> new OpenShiftAiChatCompletionServiceSettings(
44+
commonServiceSettings.model(),
45+
commonServiceSettings.uri(),
46+
commonServiceSettings.rateLimitSettings()
47+
)
5648
);
57-
58-
if (validationException.validationErrors().isEmpty() == false) {
59-
throw validationException;
60-
}
61-
62-
return new OpenShiftAiChatCompletionServiceSettings(model, uri, rateLimitSettings);
6349
}
6450

6551
/**

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/openshiftai/embeddings/OpenShiftAiEmbeddingsServiceSettings.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.elasticsearch.xpack.core.inference.InferenceUtils;
1919
import org.elasticsearch.xpack.inference.services.ConfigurationParseContext;
2020
import org.elasticsearch.xpack.inference.services.ServiceUtils;
21-
import org.elasticsearch.xpack.inference.services.openshiftai.OpenShiftAiService;
2221
import org.elasticsearch.xpack.inference.services.openshiftai.OpenShiftAiServiceSettings;
2322
import org.elasticsearch.xpack.inference.services.settings.RateLimitSettings;
2423

@@ -29,15 +28,11 @@
2928

3029
import static org.elasticsearch.xpack.inference.services.ServiceFields.DIMENSIONS;
3130
import static org.elasticsearch.xpack.inference.services.ServiceFields.MAX_INPUT_TOKENS;
32-
import static org.elasticsearch.xpack.inference.services.ServiceFields.MODEL_ID;
3331
import static org.elasticsearch.xpack.inference.services.ServiceFields.SIMILARITY;
34-
import static org.elasticsearch.xpack.inference.services.ServiceFields.URL;
3532
import static org.elasticsearch.xpack.inference.services.ServiceUtils.createUri;
3633
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractOptionalBoolean;
3734
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractOptionalPositiveInteger;
38-
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractOptionalString;
3935
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractSimilarity;
40-
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractUri;
4136

4237
/**
4338
* Settings for the OpenShift AI embeddings service.
@@ -61,10 +56,8 @@ public class OpenShiftAiEmbeddingsServiceSettings extends OpenShiftAiServiceSett
6156
* @throws ValidationException if any required fields are missing or invalid
6257
*/
6358
public static OpenShiftAiEmbeddingsServiceSettings fromMap(Map<String, Object> map, ConfigurationParseContext context) {
64-
ValidationException validationException = new ValidationException();
65-
66-
var model = extractOptionalString(map, MODEL_ID, ModelConfigurations.SERVICE_SETTINGS, validationException);
67-
var uri = extractUri(map, URL, validationException);
59+
var validationException = new ValidationException();
60+
var commonServiceSettings = extractOpenShiftAiCommonServiceSettings(map, context, validationException);
6861
var dimensions = extractOptionalPositiveInteger(map, DIMENSIONS, ModelConfigurations.SERVICE_SETTINGS, validationException);
6962
var similarity = extractSimilarity(map, ModelConfigurations.SERVICE_SETTINGS, validationException);
7063
var maxInputTokens = extractOptionalPositiveInteger(
@@ -73,14 +66,7 @@ public static OpenShiftAiEmbeddingsServiceSettings fromMap(Map<String, Object> m
7366
ModelConfigurations.SERVICE_SETTINGS,
7467
validationException
7568
);
76-
var rateLimitSettings = RateLimitSettings.of(
77-
map,
78-
DEFAULT_RATE_LIMIT_SETTINGS,
79-
validationException,
80-
OpenShiftAiService.NAME,
81-
context
82-
);
83-
Boolean dimensionsSetByUser = extractOptionalBoolean(map, DIMENSIONS_SET_BY_USER, validationException);
69+
var dimensionsSetByUser = extractOptionalBoolean(map, DIMENSIONS_SET_BY_USER, validationException);
8470
switch (context) {
8571
case REQUEST -> {
8672
if (dimensionsSetByUser != null) {
@@ -103,12 +89,12 @@ public static OpenShiftAiEmbeddingsServiceSettings fromMap(Map<String, Object> m
10389
}
10490

10591
return new OpenShiftAiEmbeddingsServiceSettings(
106-
model,
107-
uri,
92+
commonServiceSettings.model(),
93+
commonServiceSettings.uri(),
10894
dimensions,
10995
similarity,
11096
maxInputTokens,
111-
rateLimitSettings,
97+
commonServiceSettings.rateLimitSettings(),
11298
dimensionsSetByUser
11399
);
114100
}

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/openshiftai/rerank/OpenShiftAiRerankServiceSettings.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
import org.elasticsearch.common.ValidationException;
1111
import org.elasticsearch.common.io.stream.StreamInput;
1212
import org.elasticsearch.core.Nullable;
13-
import org.elasticsearch.inference.ModelConfigurations;
1413
import org.elasticsearch.xpack.inference.services.ConfigurationParseContext;
15-
import org.elasticsearch.xpack.inference.services.openshiftai.OpenShiftAiService;
1614
import org.elasticsearch.xpack.inference.services.openshiftai.OpenShiftAiServiceSettings;
1715
import org.elasticsearch.xpack.inference.services.settings.RateLimitSettings;
1816

@@ -21,11 +19,7 @@
2119
import java.util.Map;
2220
import java.util.Objects;
2321

24-
import static org.elasticsearch.xpack.inference.services.ServiceFields.MODEL_ID;
25-
import static org.elasticsearch.xpack.inference.services.ServiceFields.URL;
2622
import static org.elasticsearch.xpack.inference.services.ServiceUtils.createUri;
27-
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractOptionalString;
28-
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractUri;
2923

3024
/**
3125
* Represents the settings for an OpenShift AI rerank service.
@@ -43,23 +37,15 @@ public class OpenShiftAiRerankServiceSettings extends OpenShiftAiServiceSettings
4337
* @throws ValidationException if required fields are missing or invalid
4438
*/
4539
public static OpenShiftAiRerankServiceSettings fromMap(Map<String, Object> map, ConfigurationParseContext context) {
46-
ValidationException validationException = new ValidationException();
47-
48-
var model = extractOptionalString(map, MODEL_ID, ModelConfigurations.SERVICE_SETTINGS, validationException);
49-
var uri = extractUri(map, URL, validationException);
50-
RateLimitSettings rateLimitSettings = RateLimitSettings.of(
40+
return fromMap(
5141
map,
52-
DEFAULT_RATE_LIMIT_SETTINGS,
53-
validationException,
54-
OpenShiftAiService.NAME,
55-
context
42+
context,
43+
commonServiceSettings -> new OpenShiftAiRerankServiceSettings(
44+
commonServiceSettings.model(),
45+
commonServiceSettings.uri(),
46+
commonServiceSettings.rateLimitSettings()
47+
)
5648
);
57-
58-
if (validationException.validationErrors().isEmpty() == false) {
59-
throw validationException;
60-
}
61-
62-
return new OpenShiftAiRerankServiceSettings(model, uri, rateLimitSettings);
6349
}
6450

6551
/**

0 commit comments

Comments
 (0)