Skip to content

Commit 1eae10d

Browse files
Renaming and flattening request map (#128699) (#128853)
1 parent 2d8c1ee commit 1eae10d

File tree

5 files changed

+23
-84
lines changed

5 files changed

+23
-84
lines changed

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/custom/CustomServiceSettings.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public class CustomServiceSettings extends FilteredXContentObject implements Ser
5757
public static final String URL = "url";
5858
public static final String HEADERS = "headers";
5959
public static final String REQUEST = "request";
60-
public static final String REQUEST_CONTENT = "content";
6160
public static final String RESPONSE = "response";
6261
public static final String JSON_PARSER = "json_parser";
6362
public static final String ERROR_PARSER = "error_parser";
@@ -83,14 +82,7 @@ public static CustomServiceSettings fromMap(
8382
removeNullValues(headers);
8483
var stringHeaders = validateMapStringValues(headers, HEADERS, validationException, false);
8584

86-
Map<String, Object> requestBodyMap = extractRequiredMap(map, REQUEST, ModelConfigurations.SERVICE_SETTINGS, validationException);
87-
88-
String requestContentString = extractRequiredString(
89-
Objects.requireNonNullElse(requestBodyMap, new HashMap<>()),
90-
REQUEST_CONTENT,
91-
ModelConfigurations.SERVICE_SETTINGS,
92-
validationException
93-
);
85+
String requestContentString = extractRequiredString(map, REQUEST, ModelConfigurations.SERVICE_SETTINGS, validationException);
9486

9587
Map<String, Object> responseParserMap = extractRequiredMap(
9688
map,
@@ -125,11 +117,10 @@ public static CustomServiceSettings fromMap(
125117
context
126118
);
127119

128-
if (requestBodyMap == null || responseParserMap == null || jsonParserMap == null || errorParserMap == null) {
120+
if (responseParserMap == null || jsonParserMap == null || errorParserMap == null) {
129121
throw validationException;
130122
}
131123

132-
throwIfNotEmptyMap(requestBodyMap, REQUEST, NAME);
133124
throwIfNotEmptyMap(jsonParserMap, JSON_PARSER, NAME);
134125
throwIfNotEmptyMap(responseParserMap, RESPONSE, NAME);
135126
throwIfNotEmptyMap(errorParserMap, ERROR_PARSER, NAME);
@@ -335,11 +326,7 @@ public XContentBuilder toXContentFragmentOfExposedFields(XContentBuilder builder
335326

336327
queryParameters.toXContent(builder, params);
337328

338-
builder.startObject(REQUEST);
339-
{
340-
builder.field(REQUEST_CONTENT, requestContentString);
341-
}
342-
builder.endObject();
329+
builder.field(REQUEST, requestContentString);
343330

344331
builder.startObject(RESPONSE);
345332
{

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/custom/request/CustomRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import java.util.Objects;
3333

3434
import static org.elasticsearch.xpack.inference.common.JsonUtils.toJson;
35-
import static org.elasticsearch.xpack.inference.services.custom.CustomServiceSettings.REQUEST_CONTENT;
35+
import static org.elasticsearch.xpack.inference.services.custom.CustomServiceSettings.REQUEST;
3636
import static org.elasticsearch.xpack.inference.services.custom.CustomServiceSettings.URL;
3737

3838
public class CustomRequest implements Request {
@@ -133,7 +133,7 @@ private void setHeaders(HttpRequestBase httpRequest) {
133133
private void setRequestContent(HttpPost httpRequest) {
134134
String replacedRequestContentString = jsonPlaceholderReplacer.replace(
135135
model.getServiceSettings().getRequestContentString(),
136-
REQUEST_CONTENT
136+
REQUEST
137137
);
138138
StringEntity stringEntity = new StringEntity(replacedRequestContentString, StandardCharsets.UTF_8);
139139
httpRequest.setEntity(stringEntity);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
* To use this class, extend it and pass the constructor a configuration.
5555
* </p>
5656
*/
57-
public abstract class AbstractServiceTests extends ESTestCase {
57+
public abstract class AbstractInferenceServiceTests extends ESTestCase {
5858

5959
protected final MockWebServer webServer = new MockWebServer();
6060
protected ThreadPool threadPool;
@@ -80,7 +80,7 @@ public void tearDown() throws Exception {
8080

8181
private final TestConfiguration testConfiguration;
8282

83-
public AbstractServiceTests(TestConfiguration testConfiguration) {
83+
public AbstractInferenceServiceTests(TestConfiguration testConfiguration) {
8484
this.testConfiguration = Objects.requireNonNull(testConfiguration);
8585
}
8686

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/services/custom/CustomServiceSettingsTests.java

Lines changed: 13 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void testFromMap() {
126126
QueryParameters.QUERY_PARAMETERS,
127127
queryParameters,
128128
CustomServiceSettings.REQUEST,
129-
new HashMap<>(Map.of(CustomServiceSettings.REQUEST_CONTENT, requestContentString)),
129+
requestContentString,
130130
CustomServiceSettings.RESPONSE,
131131
new HashMap<>(
132132
Map.of(
@@ -179,7 +179,7 @@ public void testFromMap_WithOptionalsNotSpecified() {
179179
CustomServiceSettings.URL,
180180
url,
181181
CustomServiceSettings.REQUEST,
182-
new HashMap<>(Map.of(CustomServiceSettings.REQUEST_CONTENT, requestContentString)),
182+
requestContentString,
183183
CustomServiceSettings.RESPONSE,
184184
new HashMap<>(
185185
Map.of(
@@ -243,7 +243,7 @@ public void testFromMap_RemovesNullValues_FromMaps() {
243243
CustomServiceSettings.HEADERS,
244244
headersWithNulls,
245245
CustomServiceSettings.REQUEST,
246-
new HashMap<>(Map.of(CustomServiceSettings.REQUEST_CONTENT, requestContentString)),
246+
requestContentString,
247247
CustomServiceSettings.RESPONSE,
248248
new HashMap<>(
249249
Map.of(
@@ -304,7 +304,7 @@ public void testFromMap_ReturnsError_IfHeadersContainsNonStringValues() {
304304
CustomServiceSettings.HEADERS,
305305
new HashMap<>(Map.of("key", 1)),
306306
CustomServiceSettings.REQUEST,
307-
new HashMap<>(Map.of(CustomServiceSettings.REQUEST_CONTENT, requestContentString)),
307+
requestContentString,
308308
CustomServiceSettings.RESPONSE,
309309
new HashMap<>(
310310
Map.of(
@@ -353,7 +353,7 @@ public void testFromMap_ReturnsError_IfQueryParamsContainsNonStringValues() {
353353
QueryParameters.QUERY_PARAMETERS,
354354
List.of(List.of("key", 1)),
355355
CustomServiceSettings.REQUEST,
356-
new HashMap<>(Map.of(CustomServiceSettings.REQUEST_CONTENT, requestContentString)),
356+
requestContentString,
357357
CustomServiceSettings.RESPONSE,
358358
new HashMap<>(
359359
Map.of(
@@ -393,7 +393,7 @@ public void testFromMap_ReturnsError_IfRequestMapIsMissing() {
393393
CustomServiceSettings.HEADERS,
394394
new HashMap<>(Map.of("key", "value")),
395395
"invalid_request",
396-
new HashMap<>(Map.of(CustomServiceSettings.REQUEST_CONTENT, requestContentString)),
396+
requestContentString,
397397
CustomServiceSettings.RESPONSE,
398398
new HashMap<>(
399399
Map.of(
@@ -413,13 +413,7 @@ public void testFromMap_ReturnsError_IfRequestMapIsMissing() {
413413
() -> CustomServiceSettings.fromMap(mapSettings, ConfigurationParseContext.REQUEST, TaskType.TEXT_EMBEDDING, "inference_id")
414414
);
415415

416-
assertThat(
417-
exception.getMessage(),
418-
is(
419-
"Validation Failed: 1: [service_settings] does not contain the required setting [request];"
420-
+ "2: [service_settings] does not contain the required setting [content];"
421-
)
422-
);
416+
assertThat(exception.getMessage(), is("Validation Failed: 1: [service_settings] does not contain the required setting [request];"));
423417
}
424418

425419
public void testFromMap_ReturnsError_IfResponseMapIsMissing() {
@@ -433,7 +427,7 @@ public void testFromMap_ReturnsError_IfResponseMapIsMissing() {
433427
CustomServiceSettings.HEADERS,
434428
new HashMap<>(Map.of("key", "value")),
435429
CustomServiceSettings.REQUEST,
436-
new HashMap<>(Map.of(CustomServiceSettings.REQUEST_CONTENT, requestContentString)),
430+
requestContentString,
437431
"invalid_response",
438432
new HashMap<>(
439433
Map.of(
@@ -464,46 +458,6 @@ public void testFromMap_ReturnsError_IfResponseMapIsMissing() {
464458
);
465459
}
466460

467-
public void testFromMap_ReturnsError_IfRequestMapIsNotEmptyAfterParsing() {
468-
String url = "http://www.abc.com";
469-
String requestContentString = "request body";
470-
471-
var mapSettings = new HashMap<String, Object>(
472-
Map.of(
473-
CustomServiceSettings.URL,
474-
url,
475-
CustomServiceSettings.HEADERS,
476-
new HashMap<>(Map.of("key", "value")),
477-
CustomServiceSettings.REQUEST,
478-
new HashMap<>(Map.of(CustomServiceSettings.REQUEST_CONTENT, requestContentString, "key", "value")),
479-
CustomServiceSettings.RESPONSE,
480-
new HashMap<>(
481-
Map.of(
482-
CustomServiceSettings.JSON_PARSER,
483-
new HashMap<>(
484-
Map.of(TextEmbeddingResponseParser.TEXT_EMBEDDING_PARSER_EMBEDDINGS, "$.result.embeddings[*].embedding")
485-
),
486-
CustomServiceSettings.ERROR_PARSER,
487-
new HashMap<>(Map.of(ErrorResponseParser.MESSAGE_PATH, "$.error.message"))
488-
)
489-
)
490-
)
491-
);
492-
493-
var exception = expectThrows(
494-
ElasticsearchStatusException.class,
495-
() -> CustomServiceSettings.fromMap(mapSettings, ConfigurationParseContext.REQUEST, TaskType.TEXT_EMBEDDING, "inference_id")
496-
);
497-
498-
assertThat(
499-
exception.getMessage(),
500-
is(
501-
"Configuration contains unknown settings [{key=value}] while parsing field [request]"
502-
+ " for settings [custom_service_settings]"
503-
)
504-
);
505-
}
506-
507461
public void testFromMap_ReturnsError_IfJsonParserMapIsNotEmptyAfterParsing() {
508462
String url = "http://www.abc.com";
509463
String requestContentString = "request body";
@@ -515,7 +469,7 @@ public void testFromMap_ReturnsError_IfJsonParserMapIsNotEmptyAfterParsing() {
515469
CustomServiceSettings.HEADERS,
516470
new HashMap<>(Map.of("key", "value")),
517471
CustomServiceSettings.REQUEST,
518-
new HashMap<>(Map.of(CustomServiceSettings.REQUEST_CONTENT, requestContentString)),
472+
requestContentString,
519473
CustomServiceSettings.RESPONSE,
520474
new HashMap<>(
521475
Map.of(
@@ -560,7 +514,7 @@ public void testFromMap_ReturnsError_IfResponseMapIsNotEmptyAfterParsing() {
560514
CustomServiceSettings.HEADERS,
561515
new HashMap<>(Map.of("key", "value")),
562516
CustomServiceSettings.REQUEST,
563-
new HashMap<>(Map.of(CustomServiceSettings.REQUEST_CONTENT, requestContentString)),
517+
requestContentString,
564518
CustomServiceSettings.RESPONSE,
565519
new HashMap<>(
566520
Map.of(
@@ -602,7 +556,7 @@ public void testFromMap_ReturnsError_IfErrorParserMapIsNotEmptyAfterParsing() {
602556
CustomServiceSettings.HEADERS,
603557
new HashMap<>(Map.of("key", "value")),
604558
CustomServiceSettings.REQUEST,
605-
new HashMap<>(Map.of(CustomServiceSettings.REQUEST_CONTENT, requestContentString)),
559+
requestContentString,
606560
CustomServiceSettings.RESPONSE,
607561
new HashMap<>(
608562
Map.of(
@@ -642,7 +596,7 @@ public void testFromMap_ReturnsError_IfTaskTypeIsInvalid() {
642596
CustomServiceSettings.HEADERS,
643597
new HashMap<>(Map.of("key", "value")),
644598
CustomServiceSettings.REQUEST,
645-
new HashMap<>(Map.of(CustomServiceSettings.REQUEST_CONTENT, requestContentString)),
599+
requestContentString,
646600
CustomServiceSettings.RESPONSE,
647601
new HashMap<>(
648602
Map.of(
@@ -687,9 +641,7 @@ public void testXContent() throws IOException {
687641
"headers": {
688642
"key": "value"
689643
},
690-
"request": {
691-
"content": "string"
692-
},
644+
"request": "string",
693645
"response": {
694646
"json_parser": {
695647
"text_embeddings": "$.result.embeddings[*].embedding"

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/services/custom/CustomServiceTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.elasticsearch.xpack.core.ml.search.WeightedToken;
2626
import org.elasticsearch.xpack.inference.external.http.HttpClientManager;
2727
import org.elasticsearch.xpack.inference.external.http.sender.HttpRequestSenderTests;
28-
import org.elasticsearch.xpack.inference.services.AbstractServiceTests;
28+
import org.elasticsearch.xpack.inference.services.AbstractInferenceServiceTests;
2929
import org.elasticsearch.xpack.inference.services.SenderService;
3030
import org.elasticsearch.xpack.inference.services.ServiceFields;
3131
import org.elasticsearch.xpack.inference.services.custom.response.CompletionResponseParser;
@@ -54,7 +54,7 @@
5454
import static org.hamcrest.Matchers.instanceOf;
5555
import static org.hamcrest.Matchers.is;
5656

57-
public class CustomServiceTests extends AbstractServiceTests {
57+
public class CustomServiceTests extends AbstractInferenceServiceTests {
5858

5959
public CustomServiceTests() {
6060
super(createTestConfiguration());
@@ -150,7 +150,7 @@ private static Map<String, Object> createServiceSettingsMap(TaskType taskType) {
150150
QueryParameters.QUERY_PARAMETERS,
151151
List.of(List.of("key", "value")),
152152
CustomServiceSettings.REQUEST,
153-
new HashMap<>(Map.of(CustomServiceSettings.REQUEST_CONTENT, "request body")),
153+
"request body",
154154
CustomServiceSettings.RESPONSE,
155155
new HashMap<>(
156156
Map.of(

0 commit comments

Comments
 (0)