forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Stream Inference API (elastic#113158) (elastic#113423)
Create `POST _inference/<task>/<id>/_stream` and `POST _inference/<id>/_stream` API. REST Streaming API will reuse InferenceAction. For now, all services and task types will return an HTTP 405 status code and error message. Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
- Loading branch information
1 parent
9a21ca6
commit cb42fd4
Showing
24 changed files
with
798 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 113158 | ||
summary: Adds a new Inference API for streaming responses back to the user. | ||
area: Machine Learning | ||
type: enhancement | ||
issues: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...c/javaRestTest/java/org/elasticsearch/xpack/inference/AsyncInferenceResponseConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.inference; | ||
|
||
import org.apache.http.HttpEntity; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.entity.ContentType; | ||
import org.apache.http.nio.ContentDecoder; | ||
import org.apache.http.nio.IOControl; | ||
import org.apache.http.nio.protocol.AbstractAsyncResponseConsumer; | ||
import org.apache.http.nio.util.SimpleInputBuffer; | ||
import org.apache.http.protocol.HttpContext; | ||
import org.elasticsearch.xpack.inference.external.response.streaming.ServerSentEvent; | ||
import org.elasticsearch.xpack.inference.external.response.streaming.ServerSentEventParser; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayDeque; | ||
import java.util.Deque; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
class AsyncInferenceResponseConsumer extends AbstractAsyncResponseConsumer<HttpResponse> { | ||
private final AtomicReference<HttpResponse> httpResponse = new AtomicReference<>(); | ||
private final Deque<ServerSentEvent> collector = new ArrayDeque<>(); | ||
private final ServerSentEventParser sseParser = new ServerSentEventParser(); | ||
private final SimpleInputBuffer inputBuffer = new SimpleInputBuffer(4096); | ||
|
||
@Override | ||
protected void onResponseReceived(HttpResponse httpResponse) { | ||
this.httpResponse.set(httpResponse); | ||
} | ||
|
||
@Override | ||
protected void onContentReceived(ContentDecoder contentDecoder, IOControl ioControl) throws IOException { | ||
inputBuffer.consumeContent(contentDecoder); | ||
} | ||
|
||
@Override | ||
protected void onEntityEnclosed(HttpEntity httpEntity, ContentType contentType) { | ||
httpResponse.updateAndGet(response -> { | ||
response.setEntity(httpEntity); | ||
return response; | ||
}); | ||
} | ||
|
||
@Override | ||
protected HttpResponse buildResult(HttpContext httpContext) { | ||
var allBytes = new byte[inputBuffer.length()]; | ||
try { | ||
inputBuffer.read(allBytes); | ||
sseParser.parse(allBytes).forEach(collector::offer); | ||
} catch (IOException e) { | ||
failed(e); | ||
} | ||
return httpResponse.get(); | ||
} | ||
|
||
@Override | ||
protected void releaseResources() {} | ||
|
||
Deque<ServerSentEvent> events() { | ||
return collector; | ||
} | ||
} |
Oops, something went wrong.