Skip to content

Commit

Permalink
[ML] Make task_type optional (#104483)
Browse files Browse the repository at this point in the history
Makes the task_type element of the _inference API optional so that 
it is possible to GET, DELETE or POST to an inference entity without
providing the task type
  • Loading branch information
davidkyle committed Feb 6, 2024
1 parent 341f845 commit 5f32518
Show file tree
Hide file tree
Showing 26 changed files with 369 additions and 82 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/104483.yaml
@@ -0,0 +1,5 @@
pr: 104483
summary: Make `task_type` optional in `_inference` APIs
area: Machine Learning
type: enhancement
issues: []
13 changes: 7 additions & 6 deletions docs/reference/inference/delete-inference.asciidoc
Expand Up @@ -6,16 +6,17 @@ experimental[]

Deletes an {infer} model deployment.

IMPORTANT: The {infer} APIs enable you to use certain services, such as ELSER,
OpenAI, or Hugging Face, in your cluster. This is not the same feature that you
can use on an ML node with custom {ml} models. If you want to train and use your
IMPORTANT: The {infer} APIs enable you to use certain services, such as ELSER,
OpenAI, or Hugging Face, in your cluster. This is not the same feature that you
can use on an ML node with custom {ml} models. If you want to train and use your
own model, use the <<ml-df-trained-models-apis>>.


[discrete]
[[delete-inference-api-request]]
==== {api-request-title}

`DELETE /_inference/<model_id>`
`DELETE /_inference/<task_type>/<model_id>`

[discrete]
Expand All @@ -34,15 +35,15 @@ own model, use the <<ml-df-trained-models-apis>>.
The unique identifier of the {infer} model to delete.

<task_type>::
(Required, string)
(Optional, string)
The type of {infer} task that the model performs.


[discrete]
[[delete-inference-api-example]]
==== {api-examples-title}

The following API call deletes the `my-elser-model` {infer} model that can
The following API call deletes the `my-elser-model` {infer} model that can
perform `sparse_embedding` tasks.


Expand All @@ -61,4 +62,4 @@ The API returns the following response:
"acknowledged": true
}
------------------------------------------------------------
// NOTCONSOLE
// NOTCONSOLE
8 changes: 5 additions & 3 deletions docs/reference/inference/get-inference.asciidoc
Expand Up @@ -6,9 +6,9 @@ experimental[]

Retrieves {infer} model information.

IMPORTANT: The {infer} APIs enable you to use certain services, such as ELSER,
OpenAI, or Hugging Face, in your cluster. This is not the same feature that you
can use on an ML node with custom {ml} models. If you want to train and use your
IMPORTANT: The {infer} APIs enable you to use certain services, such as ELSER,
OpenAI, or Hugging Face, in your cluster. This is not the same feature that you
can use on an ML node with custom {ml} models. If you want to train and use your
own model, use the <<ml-df-trained-models-apis>>.


Expand All @@ -18,6 +18,8 @@ own model, use the <<ml-df-trained-models-apis>>.

`GET /_inference/_all`

`GET /_inference/<model_id>`

`GET /_inference/<task_type>/_all`

`GET /_inference/<task_type>/<model_id>`
Expand Down
9 changes: 5 additions & 4 deletions docs/reference/inference/post-inference.asciidoc
Expand Up @@ -6,16 +6,17 @@ experimental[]

Performs an inference task on an input text by using an {infer} model.

IMPORTANT: The {infer} APIs enable you to use certain services, such as ELSER,
OpenAI, or Hugging Face, in your cluster. This is not the same feature that you
can use on an ML node with custom {ml} models. If you want to train and use your
IMPORTANT: The {infer} APIs enable you to use certain services, such as ELSER,
OpenAI, or Hugging Face, in your cluster. This is not the same feature that you
can use on an ML node with custom {ml} models. If you want to train and use your
own model, use the <<ml-df-trained-models-apis>>.


[discrete]
[[post-inference-api-request]]
==== {api-request-title}

`POST /_inference/<model_id>`
`POST /_inference/<task_type>/<model_id>`


Expand Down Expand Up @@ -46,7 +47,7 @@ The unique identifier of the {infer} model.


`<task_type>`::
(Required, string)
(Optional, string)
The type of {infer} task that the model performs.


Expand Down
Expand Up @@ -12,16 +12,28 @@
"url":{
"paths":[
{
"path":"/_inference/{task_type}/{model_id}",
"path": "/_inference/{inference_id}",
"methods": [
"DELETE"
],
"parts": {
"inference_id": {
"type": "string",
"description": "The inference Id"
}
}
},
{
"path":"/_inference/{task_type}/{inference_id}",
"methods":[
"DELETE"
],
"parts":{
"task_type":{
"type":"string",
"description":"The model task type"
"description":"The task type"
},
"model_id":{
"inference_id":{
"type":"string",
"description":"The model Id"
}
Expand Down
Expand Up @@ -12,18 +12,30 @@
"url":{
"paths":[
{
"path":"/_inference/{task_type}/{model_id}",
"path":"/_inference/{inference_id}",
"methods":[
"GET"
],
"parts":{
"inference_id":{
"type":"string",
"description":"The inference Id"
}
}
},
{
"path":"/_inference/{task_type}/{inference_id}",
"methods":[
"GET"
],
"parts":{
"task_type":{
"type":"string",
"description":"The model task type"
"description":"The task type"
},
"model_id":{
"inference_id":{
"type":"string",
"description":"The model Id"
"description":"The inference Id"
}
}
}
Expand Down
Expand Up @@ -13,18 +13,30 @@
"url":{
"paths":[
{
"path":"/_inference/{task_type}/{model_id}",
"path":"/_inference/{inference_id}",
"methods":[
"POST"
],
"parts":{
"inference_id":{
"type":"string",
"description":"The inference Id"
}
}
},
{
"path":"/_inference/{task_type}/{inference_id}",
"methods":[
"POST"
],
"parts":{
"task_type":{
"type":"string",
"description":"The model task type"
"description":"The task type"
},
"model_id":{
"inference_id":{
"type":"string",
"description":"The model Id"
"description":"The inference Id"
}
}
}
Expand Down
Expand Up @@ -13,18 +13,30 @@
"url":{
"paths":[
{
"path":"/_inference/{task_type}/{model_id}",
"path":"/_inference/{inference_id}",
"methods":[
"PUT"
],
"parts":{
"inference_id":{
"type":"string",
"description":"The inference Id"
}
}
},
{
"path":"/_inference/{task_type}/{inference_id}",
"methods":[
"PUT"
],
"parts":{
"task_type":{
"type":"string",
"description":"The model task type"
"description":"The task type"
},
"model_id":{
"inference_id":{
"type":"string",
"description":"The model Id"
"description":"The inference Id"
}
}
}
Expand Down
Expand Up @@ -31,9 +31,9 @@ public static class Request extends AcknowledgedRequest<DeleteInferenceModelActi
private final String inferenceEntityId;
private final TaskType taskType;

public Request(String inferenceEntityId, String taskType) {
public Request(String inferenceEntityId, TaskType taskType) {
this.inferenceEntityId = inferenceEntityId;
this.taskType = TaskType.fromStringOrStatusException(taskType);
this.taskType = taskType;
}

public Request(StreamInput in) throws IOException {
Expand Down
Expand Up @@ -57,7 +57,7 @@ public static class Request extends ActionRequest {
PARSER.declareObject(Request.Builder::setTaskSettings, (p, c) -> p.mapOrdered(), TASK_SETTINGS);
}

public static Request parseRequest(String inferenceEntityId, String taskType, XContentParser parser) {
public static Request parseRequest(String inferenceEntityId, TaskType taskType, XContentParser parser) {
Request.Builder builder = PARSER.apply(parser, null);
builder.setInferenceEntityId(inferenceEntityId);
builder.setTaskType(taskType);
Expand Down Expand Up @@ -197,13 +197,8 @@ public Builder setInferenceEntityId(String inferenceEntityId) {
return this;
}

public Builder setTaskType(String taskTypeStr) {
try {
TaskType taskType = TaskType.fromString(taskTypeStr);
this.taskType = Objects.requireNonNull(taskType);
} catch (IllegalArgumentException e) {
throw new ElasticsearchStatusException("Unknown task_type [{}]", RestStatus.BAD_REQUEST, taskTypeStr);
}
public Builder setTaskType(TaskType taskType) {
this.taskType = taskType;
return this;
}

Expand Down
Expand Up @@ -42,8 +42,8 @@ public static class Request extends AcknowledgedRequest<Request> {
private final BytesReference content;
private final XContentType contentType;

public Request(String taskType, String inferenceEntityId, BytesReference content, XContentType contentType) {
this.taskType = TaskType.fromStringOrStatusException(taskType);
public Request(TaskType taskType, String inferenceEntityId, BytesReference content, XContentType contentType) {
this.taskType = taskType;
this.inferenceEntityId = inferenceEntityId;
this.content = content;
this.contentType = contentType;
Expand Down
Expand Up @@ -19,14 +19,14 @@
import java.util.Locale;

public class PutInferenceModelActionTests extends ESTestCase {
public static String TASK_TYPE;
public static TaskType TASK_TYPE;
public static String MODEL_ID;
public static XContentType X_CONTENT_TYPE;
public static BytesReference BYTES;

@Before
public void setup() throws Exception {
TASK_TYPE = TaskType.ANY.toString();
TASK_TYPE = TaskType.SPARSE_EMBEDDING;
MODEL_ID = randomAlphaOfLengthBetween(1, 10).toLowerCase(Locale.ROOT);
X_CONTENT_TYPE = randomFrom(XContentType.values());
BYTES = new BytesArray(randomAlphaOfLengthBetween(1, 10));
Expand Down

0 comments on commit 5f32518

Please sign in to comment.