Skip to content

Commit 659b132

Browse files
dan-rubinsteinelasticsearchmachineprwhelanelasticmachine
authored
Add exception for perform embedding inference requests with query provided (#131641)
* Add exception for perform embedding inference requests with query provided * Update docs/changelog/131641.yaml * [CI] Auto commit changes from spotless --------- Co-authored-by: elasticsearchmachine <[email protected]> Co-authored-by: Pat Whelan <[email protected]> Co-authored-by: Elastic Machine <[email protected]>
1 parent 2c656cc commit 659b132

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

docs/changelog/131641.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 131641
2+
summary: Add exception for perform embedding inference requests with query provided
3+
area: Machine Learning
4+
type: bug
5+
issues: []

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/inference/action/InferenceAction.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,14 @@ public ActionRequestValidationException validate() {
273273
}
274274
}
275275

276+
if (taskType.equals(TaskType.TEXT_EMBEDDING) || taskType.equals(TaskType.SPARSE_EMBEDDING)) {
277+
if (query != null) {
278+
var e = new ActionRequestValidationException();
279+
e.addValidationError(format("Field [query] cannot be specified for task type [%s]", taskType));
280+
return e;
281+
}
282+
}
283+
276284
if (taskType.equals(TaskType.TEXT_EMBEDDING) == false
277285
&& taskType.equals(TaskType.ANY) == false
278286
&& (inputType != null && InputType.isInternalTypeOrUnspecified(inputType) == false)) {

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/inference/action/InferenceActionRequestTests.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,24 @@ public void testValidation_TextEmbedding_WithTopN() {
191191
assertThat(inputError.getMessage(), is("Validation Failed: 1: Field [top_n] cannot be specified for task type [text_embedding];"));
192192
}
193193

194+
public void testValidation_TextEmbedding_WithQuery() {
195+
InferenceAction.Request queryRequest = new InferenceAction.Request(
196+
TaskType.TEXT_EMBEDDING,
197+
"model",
198+
"query",
199+
null,
200+
null,
201+
List.of("input"),
202+
null,
203+
null,
204+
null,
205+
false
206+
);
207+
ActionRequestValidationException queryError = queryRequest.validate();
208+
assertNotNull(queryError);
209+
assertThat(queryError.getMessage(), is("Validation Failed: 1: Field [query] cannot be specified for task type [text_embedding];"));
210+
}
211+
194212
public void testValidation_Rerank_Null() {
195213
InferenceAction.Request queryNullRequest = new InferenceAction.Request(
196214
TaskType.RERANK,
@@ -249,7 +267,7 @@ public void testValidation_SparseEmbedding_WithInputType() {
249267
InferenceAction.Request queryRequest = new InferenceAction.Request(
250268
TaskType.SPARSE_EMBEDDING,
251269
"model",
252-
"",
270+
null,
253271
null,
254272
null,
255273
List.of("input"),
@@ -309,6 +327,27 @@ public void testValidation_SparseEmbedding_WithTopN() {
309327
);
310328
}
311329

330+
public void testValidation_SparseEmbedding_WithQuery() {
331+
InferenceAction.Request queryRequest = new InferenceAction.Request(
332+
TaskType.SPARSE_EMBEDDING,
333+
"model",
334+
"query",
335+
null,
336+
null,
337+
List.of("input"),
338+
null,
339+
null,
340+
null,
341+
false
342+
);
343+
ActionRequestValidationException queryError = queryRequest.validate();
344+
assertNotNull(queryError);
345+
assertThat(
346+
queryError.getMessage(),
347+
is("Validation Failed: 1: Field [query] cannot be specified for task type [sparse_embedding];")
348+
);
349+
}
350+
312351
public void testValidation_Completion_WithInputType() {
313352
InferenceAction.Request queryRequest = new InferenceAction.Request(
314353
TaskType.COMPLETION,

0 commit comments

Comments
 (0)