Skip to content

Commit ad55337

Browse files
Fixing tests
1 parent 3df0f70 commit ad55337

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

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

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,19 @@ public class CompletionResponseParser extends BaseCustomResponseParser<ChatCompl
3030

3131
private final String completionResultPath;
3232

33-
<<<<<<< HEAD
34-
public static CompletionResponseParser fromMap(Map<String, Object> responseParserMap, String scope, ValidationException validationException) {
35-
var path = extractRequiredString(responseParserMap, COMPLETION_PARSER_RESULT, String.join(".", scope, JSON_PARSER), validationException);
36-
37-
if (path == null) {
38-
=======
39-
public static CompletionResponseParser fromMap(Map<String, Object> responseParserMap, ValidationException validationException) {
40-
var path = extractRequiredString(responseParserMap, COMPLETION_PARSER_RESULT, JSON_PARSER, validationException);
33+
public static CompletionResponseParser fromMap(
34+
Map<String, Object> responseParserMap,
35+
String scope,
36+
ValidationException validationException
37+
) {
38+
var path = extractRequiredString(
39+
responseParserMap,
40+
COMPLETION_PARSER_RESULT,
41+
String.join(".", scope, JSON_PARSER),
42+
validationException
43+
);
4144

4245
if (validationException.validationErrors().isEmpty() == false) {
43-
>>>>>>> 23b7a31406ea7fe4502a8b582915cb066bf8b86e
4446
throw validationException;
4547
}
4648

@@ -88,35 +90,22 @@ public String getWriteableName() {
8890

8991
@Override
9092
public ChatCompletionResults transform(Map<String, Object> map) {
91-
<<<<<<< HEAD
92-
var extractedField = MapPathExtractor.extract(map, completionResultPath);
93-
94-
validateNonNull(extractedField);
95-
96-
if (extractedField instanceof List<?> extractedList) {
97-
var completionList = validateAndCastList(extractedList, (obj) -> toType(obj, String.class));
98-
=======
9993
var result = MapPathExtractor.extract(map, completionResultPath);
10094
var extractedField = result.extractedObject();
10195

10296
validateNonNull(extractedField, completionResultPath);
10397

10498
if (extractedField instanceof List<?> extractedList) {
10599
var completionList = castList(extractedList, (obj, fieldName) -> toType(obj, String.class, fieldName), completionResultPath);
106-
>>>>>>> 23b7a31406ea7fe4502a8b582915cb066bf8b86e
107100
return new ChatCompletionResults(completionList.stream().map(ChatCompletionResults.Result::new).toList());
108101
} else if (extractedField instanceof String extractedString) {
109102
return new ChatCompletionResults(List.of(new ChatCompletionResults.Result(extractedString)));
110103
} else {
111104
throw new IllegalArgumentException(
112105
Strings.format(
113-
<<<<<<< HEAD
114-
"Extracted field is an invalid type, expected a list or a string but received [%s]",
115-
=======
116106
"Extracted field [%s] from path [%s] is an invalid type, expected a list or a string but received [%s]",
117107
result.getArrayFieldName(0),
118108
completionResultPath,
119-
>>>>>>> 23b7a31406ea7fe4502a8b582915cb066bf8b86e
120109
extractedField.getClass().getSimpleName()
121110
)
122111
);

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/common/MapPathExtractorTests.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,12 @@ public void testExtract_IteratesSparseEmbeddingStyleMap_ExtractsMaps() {
111111
assertThat(
112112
MapPathExtractor.extract(input, "$.result.sparse_embeddings[*].embedding[*]"),
113113
is(
114-
List.of(
115-
List.of(Map.of("tokenId", 6, "weight", 0.123d), Map.of("tokenId", 100, "weight", -123d)),
116-
List.of(Map.of("tokenId", 7, "weight", 0.456d), Map.of("tokenId", 200, "weight", -456d))
114+
new MapPathExtractor.Result(
115+
List.of(
116+
List.of(Map.of("tokenId", 6, "weight", 0.123d), Map.of("tokenId", 100, "weight", -123d)),
117+
List.of(Map.of("tokenId", 7, "weight", 0.456d), Map.of("tokenId", 200, "weight", -456d))
118+
),
119+
List.of("result.sparse_embeddings", "result.sparse_embeddings.embedding")
117120
)
118121
)
119122
);
@@ -143,7 +146,12 @@ public void testExtract_IteratesSparseEmbeddingStyleMap_ExtractsFieldFromMap() {
143146

144147
assertThat(
145148
MapPathExtractor.extract(input, "$.result.sparse_embeddings[*].embedding[*].tokenId"),
146-
is(List.of(List.of(6, 100), List.of(7, 200)))
149+
is(
150+
new MapPathExtractor.Result(
151+
List.of(List.of(6, 100), List.of(7, 200)),
152+
List.of("result.sparse_embeddings", "result.sparse_embeddings.embedding", "result.sparse_embeddings.embedding.tokenId")
153+
)
154+
)
147155
);
148156
}
149157

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ public static CompletionResponseParser createRandom() {
3939
public void testFromMap() {
4040
var validation = new ValidationException();
4141

42-
var parser = CompletionResponseParser.fromMap(new HashMap<>(Map.of(COMPLETION_PARSER_RESULT, "$.result[*].text")), "scope", validation);
42+
var parser = CompletionResponseParser.fromMap(
43+
new HashMap<>(Map.of(COMPLETION_PARSER_RESULT, "$.result[*].text")),
44+
"scope",
45+
validation
46+
);
4347

4448
assertThat(parser, is(new CompletionResponseParser("$.result[*].text")));
4549
}

0 commit comments

Comments
 (0)