Skip to content

Commit 7e9a463

Browse files
Remove error parsing class
1 parent d597e50 commit 7e9a463

File tree

11 files changed

+45
-438
lines changed

11 files changed

+45
-438
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public static RateLimitGrouping of(CustomModel model) {
4141
}
4242
}
4343

44-
private static ResponseHandler createCustomHandler(CustomModel model) {
45-
return new CustomResponseHandler("custom model", CustomResponseEntity::fromResponse, model.getServiceSettings().getErrorParser());
44+
private static ResponseHandler createCustomHandler() {
45+
return new CustomResponseHandler("custom model", CustomResponseEntity::fromResponse);
4646
}
4747

4848
public static CustomRequestManager of(CustomModel model, ThreadPool threadPool) {
@@ -55,7 +55,7 @@ public static CustomRequestManager of(CustomModel model, ThreadPool threadPool)
5555
private CustomRequestManager(CustomModel model, ThreadPool threadPool) {
5656
super(threadPool, model.getInferenceEntityId(), RateLimitGrouping.of(model), model.rateLimitServiceSettings().rateLimitSettings());
5757
this.model = model;
58-
this.handler = createCustomHandler(model);
58+
this.handler = createCustomHandler();
5959
}
6060

6161
@Override

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,24 @@
1212
import org.elasticsearch.rest.RestStatus;
1313
import org.elasticsearch.xpack.inference.external.http.HttpResult;
1414
import org.elasticsearch.xpack.inference.external.http.retry.BaseResponseHandler;
15+
import org.elasticsearch.xpack.inference.external.http.retry.ErrorResponse;
1516
import org.elasticsearch.xpack.inference.external.http.retry.ResponseParser;
1617
import org.elasticsearch.xpack.inference.external.http.retry.RetryException;
1718
import org.elasticsearch.xpack.inference.external.request.Request;
18-
import org.elasticsearch.xpack.inference.services.custom.response.ErrorResponseParser;
19+
20+
import java.nio.charset.StandardCharsets;
21+
import java.util.function.Function;
1922

2023
/**
2124
* Defines how to handle various response types returned from the custom integration.
2225
*/
2326
public class CustomResponseHandler extends BaseResponseHandler {
24-
public CustomResponseHandler(String requestType, ResponseParser parseFunction, ErrorResponseParser errorParser) {
25-
super(requestType, parseFunction, errorParser);
27+
private static final Function<HttpResult, ErrorResponse> ERROR_PARSER = (httpResult) -> new ErrorResponse(
28+
new String(httpResult.body(), StandardCharsets.UTF_8)
29+
);
30+
31+
public CustomResponseHandler(String requestType, ResponseParser parseFunction) {
32+
super(requestType, parseFunction, ERROR_PARSER);
2633
}
2734

2835
@Override

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,7 @@ private static CustomServiceSettings getCustomServiceSettings(CustomModel custom
249249
serviceSettings.getQueryParameters(),
250250
serviceSettings.getRequestContentString(),
251251
serviceSettings.getResponseJsonParser(),
252-
serviceSettings.rateLimitSettings(),
253-
serviceSettings.getErrorParser()
252+
serviceSettings.rateLimitSettings()
254253
);
255254
}
256255

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

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.elasticsearch.xpack.inference.services.ConfigurationParseContext;
2727
import org.elasticsearch.xpack.inference.services.custom.response.CompletionResponseParser;
2828
import org.elasticsearch.xpack.inference.services.custom.response.CustomResponseParser;
29-
import org.elasticsearch.xpack.inference.services.custom.response.ErrorResponseParser;
3029
import org.elasticsearch.xpack.inference.services.custom.response.NoopResponseParser;
3130
import org.elasticsearch.xpack.inference.services.custom.response.RerankResponseParser;
3231
import org.elasticsearch.xpack.inference.services.custom.response.SparseEmbeddingResponseParser;
@@ -60,7 +59,6 @@ public class CustomServiceSettings extends FilteredXContentObject implements Ser
6059
public static final String REQUEST_CONTENT = "content";
6160
public static final String RESPONSE = "response";
6261
public static final String JSON_PARSER = "json_parser";
63-
public static final String ERROR_PARSER = "error_parser";
6462

6563
private static final RateLimitSettings DEFAULT_RATE_LIMIT_SETTINGS = new RateLimitSettings(10_000);
6664
private static final String RESPONSE_SCOPE = String.join(".", ModelConfigurations.SERVICE_SETTINGS, RESPONSE);
@@ -108,15 +106,6 @@ public static CustomServiceSettings fromMap(
108106

109107
var responseJsonParser = extractResponseParser(taskType, jsonParserMap, validationException);
110108

111-
Map<String, Object> errorParserMap = extractRequiredMap(
112-
Objects.requireNonNullElse(responseParserMap, new HashMap<>()),
113-
ERROR_PARSER,
114-
RESPONSE_SCOPE,
115-
validationException
116-
);
117-
118-
var errorParser = ErrorResponseParser.fromMap(errorParserMap, RESPONSE_SCOPE, inferenceId, validationException);
119-
120109
RateLimitSettings rateLimitSettings = RateLimitSettings.of(
121110
map,
122111
DEFAULT_RATE_LIMIT_SETTINGS,
@@ -125,14 +114,13 @@ public static CustomServiceSettings fromMap(
125114
context
126115
);
127116

128-
if (requestBodyMap == null || responseParserMap == null || jsonParserMap == null || errorParserMap == null) {
117+
if (requestBodyMap == null || responseParserMap == null || jsonParserMap == null) {
129118
throw validationException;
130119
}
131120

132121
throwIfNotEmptyMap(requestBodyMap, REQUEST, NAME);
133122
throwIfNotEmptyMap(jsonParserMap, JSON_PARSER, NAME);
134123
throwIfNotEmptyMap(responseParserMap, RESPONSE, NAME);
135-
throwIfNotEmptyMap(errorParserMap, ERROR_PARSER, NAME);
136124

137125
if (validationException.validationErrors().isEmpty() == false) {
138126
throw validationException;
@@ -145,8 +133,7 @@ public static CustomServiceSettings fromMap(
145133
queryParams,
146134
requestContentString,
147135
responseJsonParser,
148-
rateLimitSettings,
149-
errorParser
136+
rateLimitSettings
150137
);
151138
}
152139

@@ -218,7 +205,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
218205
private final String requestContentString;
219206
private final CustomResponseParser responseJsonParser;
220207
private final RateLimitSettings rateLimitSettings;
221-
private final ErrorResponseParser errorParser;
222208

223209
public CustomServiceSettings(
224210
TextEmbeddingSettings textEmbeddingSettings,
@@ -227,8 +213,7 @@ public CustomServiceSettings(
227213
@Nullable QueryParameters queryParameters,
228214
String requestContentString,
229215
CustomResponseParser responseJsonParser,
230-
@Nullable RateLimitSettings rateLimitSettings,
231-
ErrorResponseParser errorParser
216+
@Nullable RateLimitSettings rateLimitSettings
232217
) {
233218
this.textEmbeddingSettings = Objects.requireNonNull(textEmbeddingSettings);
234219
this.url = Objects.requireNonNull(url);
@@ -237,7 +222,6 @@ public CustomServiceSettings(
237222
this.requestContentString = Objects.requireNonNull(requestContentString);
238223
this.responseJsonParser = Objects.requireNonNull(responseJsonParser);
239224
this.rateLimitSettings = Objects.requireNonNullElse(rateLimitSettings, DEFAULT_RATE_LIMIT_SETTINGS);
240-
this.errorParser = Objects.requireNonNull(errorParser);
241225
}
242226

243227
public CustomServiceSettings(StreamInput in) throws IOException {
@@ -248,7 +232,6 @@ public CustomServiceSettings(StreamInput in) throws IOException {
248232
requestContentString = in.readString();
249233
responseJsonParser = in.readNamedWriteable(CustomResponseParser.class);
250234
rateLimitSettings = new RateLimitSettings(in);
251-
errorParser = new ErrorResponseParser(in);
252235
}
253236

254237
@Override
@@ -296,10 +279,6 @@ public CustomResponseParser getResponseJsonParser() {
296279
return responseJsonParser;
297280
}
298281

299-
public ErrorResponseParser getErrorParser() {
300-
return errorParser;
301-
}
302-
303282
@Override
304283
public RateLimitSettings rateLimitSettings() {
305284
return rateLimitSettings;
@@ -344,7 +323,6 @@ public XContentBuilder toXContentFragmentOfExposedFields(XContentBuilder builder
344323
builder.startObject(RESPONSE);
345324
{
346325
responseJsonParser.toXContent(builder, params);
347-
errorParser.toXContent(builder, params);
348326
}
349327
builder.endObject();
350328

@@ -372,7 +350,6 @@ public void writeTo(StreamOutput out) throws IOException {
372350
out.writeString(requestContentString);
373351
out.writeNamedWriteable(responseJsonParser);
374352
rateLimitSettings.writeTo(out);
375-
errorParser.writeTo(out);
376353
}
377354

378355
@Override
@@ -386,8 +363,7 @@ public boolean equals(Object o) {
386363
&& Objects.equals(queryParameters, that.queryParameters)
387364
&& Objects.equals(requestContentString, that.requestContentString)
388365
&& Objects.equals(responseJsonParser, that.responseJsonParser)
389-
&& Objects.equals(rateLimitSettings, that.rateLimitSettings)
390-
&& Objects.equals(errorParser, that.errorParser);
366+
&& Objects.equals(rateLimitSettings, that.rateLimitSettings);
391367
}
392368

393369
@Override
@@ -399,8 +375,7 @@ public int hashCode() {
399375
queryParameters,
400376
requestContentString,
401377
responseJsonParser,
402-
rateLimitSettings,
403-
errorParser
378+
rateLimitSettings
404379
);
405380
}
406381

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

Lines changed: 0 additions & 128 deletions
This file was deleted.

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.elasticsearch.inference.TaskType;
1515
import org.elasticsearch.test.ESTestCase;
1616
import org.elasticsearch.xpack.inference.services.custom.response.CustomResponseParser;
17-
import org.elasticsearch.xpack.inference.services.custom.response.ErrorResponseParser;
1817
import org.elasticsearch.xpack.inference.services.custom.response.TextEmbeddingResponseParser;
1918
import org.elasticsearch.xpack.inference.services.settings.RateLimitSettings;
2019
import org.elasticsearch.xpack.inference.services.settings.SerializableSecureString;
@@ -120,8 +119,7 @@ public static CustomModel getTestModel(TaskType taskType, CustomResponseParser r
120119
QueryParameters.EMPTY,
121120
requestContentString,
122121
responseParser,
123-
new RateLimitSettings(10_000),
124-
new ErrorResponseParser("$.error.message", inferenceId)
122+
new RateLimitSettings(10_000)
125123
);
126124

127125
CustomTaskSettings taskSettings = new CustomTaskSettings(Map.of(taskSettingsKey, taskSettingsValue));

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import org.elasticsearch.threadpool.ThreadPool;
1717
import org.elasticsearch.xpack.inference.external.http.retry.RequestSender;
1818
import org.elasticsearch.xpack.inference.external.http.sender.EmbeddingsInput;
19-
import org.elasticsearch.xpack.inference.services.custom.response.ErrorResponseParser;
2019
import org.elasticsearch.xpack.inference.services.custom.response.RerankResponseParser;
2120
import org.elasticsearch.xpack.inference.services.settings.RateLimitSettings;
2221
import org.elasticsearch.xpack.inference.services.settings.SerializableSecureString;
@@ -64,8 +63,7 @@ public void testCreateRequest_ThrowsException_ForInvalidUrl() {
6463
null,
6564
requestContentString,
6665
new RerankResponseParser("$.result.score"),
67-
new RateLimitSettings(10_000),
68-
new ErrorResponseParser("$.error.message", inferenceId)
66+
new RateLimitSettings(10_000)
6967
);
7068

7169
var model = CustomModelTests.createModel(

0 commit comments

Comments
 (0)