-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Elastic Inference Service] Add ElasticInferenceService Unified ChatCompletions Integration #118871
Merged
Merged
Changes from 66 commits
Commits
Show all changes
67 commits
Select commit
Hold shift + click to select a range
ccec39b
Starting completion model
jonathan-buttner 467747f
Adding model
jonathan-buttner 69ba46d
initial implementation of request and response handling, manager, and…
maxhniebergall 39e2c27
Working response from openai
jonathan-buttner 7984b69
Update docs/changelog/118301.yaml
jonathan-buttner be588f4
Fixing comment
jonathan-buttner 38a58f9
Adding some initial tests
jonathan-buttner cad6f1e
Merge branch 'main' of github.com:elastic/elasticsearch into ml-eis-i…
jonathan-buttner 2e4fb05
Moving tests around
jonathan-buttner 1c0ab90
Merge branch 'main' into ml-eis-integration
jaybcee 1abe7e6
Address some TODOs
jaybcee 8e47f34
Merge branch 'main' into ml-eis-integration-jbc
jaybcee 9840c62
Merge branch 'main' into ml-eis-integration-jbc
jaybcee cfd7580
Remove a TODO
jaybcee 4fb6930
[CI] Auto commit changes from spotless
elasticsearchmachine 6a3d916
Merge branch 'main' into ml-eis-integration-jbc
jaybcee 2730017
Fix tests
jaybcee 729be3d
Merge branch 'main' into ml-eis-integration-jbc
jaybcee ab979a1
Fix more tests
jaybcee 2ce281f
Merge branch 'main' into ml-eis-integration-jbc
jaybcee 775c81b
Merge branch 'main' into ml-eis-integration-jbc
jaybcee c609559
Update docs/changelog/118871.yaml
jaybcee 5704a29
Update docs/changelog/118871.yaml
jaybcee 42c96a2
Delete docs/changelog/118301.yaml
jaybcee 34073db
Update docs/changelog/118871.yaml
jaybcee b03ee46
Update x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/…
jaybcee dfa0a05
Rename EISUnifiedChatCompletionResponseHandler
jaybcee 566bacc
Renames to ElasticInferenceServiceUnifiedCompletionRequestManager
jaybcee 3d888b1
Remove eis from response
jaybcee 80db7d3
Renames EISUnifiedChatCompletionRequest
jaybcee df74a82
Renames and comments
jaybcee d7dbf61
Adds n=1 hardcode comment
jaybcee 1f96e4a
Fixes
jaybcee 03a139a
Renames tool
jaybcee e90e693
Updates transport
jaybcee 26f1ac3
propagateTraceContext extraction
jaybcee 1d8d641
format
jaybcee 3399d61
Merge branch 'main' into ml-eis-integration-jbc
jaybcee fcfbfd3
[CI] Auto commit changes from spotless
elasticsearchmachine 9b5503a
Clean up trace
jaybcee 2a3faa4
[CI] Auto commit changes from spotless
elasticsearchmachine bd715c1
Merge branch 'main' into ml-eis-integration-jbc
maxhniebergall 67d21f8
finish merge
maxhniebergall 0feba86
Remove OpenAiRequest as it was uneeded
maxhniebergall 93cc995
Address comments
jaybcee 9dd88bf
[CI] Auto commit changes from spotless
elasticsearchmachine 13c0bdf
Merge branch 'main' into ml-eis-integration-jbc
jaybcee 635f734
[CI] Auto commit changes from spotless
elasticsearchmachine deab545
Merge branch 'main' into ml-eis-integration-jbc
jaybcee a089586
Merge branch 'main' into ml-eis-integration-jbc
jaybcee ce4cdf0
Merge branch 'main' into ml-eis-integration-jbc
jaybcee daaa0e1
Merge branch 'main' into ml-eis-integration-jbc
jaybcee 7fd0883
Update x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/…
jaybcee f84b1da
Update x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/…
jaybcee eefcecf
Update x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/…
jaybcee 567c54e
Update x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/…
jaybcee 01b60d6
Update x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/…
jaybcee 503fb4b
[CI] Auto commit changes from spotless
elasticsearchmachine 44f9f8d
Merge branch 'main' into ml-eis-integration-jbc
jaybcee 0419ab9
Address comments
jaybcee e9afd72
[CI] Auto commit changes from spotless
elasticsearchmachine f105279
Update changelog
jaybcee a9dc184
Better error messages
jaybcee 343519d
Merge branch 'main' into ml-eis-integration-jbc
jaybcee 7c8b1b3
Merge branch 'main' into ml-eis-integration-jbc
jaybcee f48eb06
Merge branch 'main' into ml-eis-integration-jbc
jaybcee e014357
[CI] Auto commit changes from spotless
elasticsearchmachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 118871 | ||
summary: "[Elastic Inference Service] Add ElasticInferenceService Unified ChatCompletions Integration" | ||
area: Inference | ||
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
40 changes: 40 additions & 0 deletions
40
...ference/external/elastic/ElasticInferenceServiceUnifiedChatCompletionResponseHandler.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,40 @@ | ||
/* | ||
* 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.external.elastic; | ||
|
||
import org.elasticsearch.inference.InferenceServiceResults; | ||
import org.elasticsearch.xpack.core.inference.results.StreamingUnifiedChatCompletionResults; | ||
import org.elasticsearch.xpack.inference.external.http.HttpResult; | ||
import org.elasticsearch.xpack.inference.external.http.retry.ResponseParser; | ||
import org.elasticsearch.xpack.inference.external.openai.OpenAiUnifiedStreamingProcessor; | ||
import org.elasticsearch.xpack.inference.external.request.Request; | ||
import org.elasticsearch.xpack.inference.external.response.streaming.ServerSentEventParser; | ||
import org.elasticsearch.xpack.inference.external.response.streaming.ServerSentEventProcessor; | ||
|
||
import java.util.concurrent.Flow; | ||
|
||
public class ElasticInferenceServiceUnifiedChatCompletionResponseHandler extends ElasticInferenceServiceResponseHandler { | ||
public ElasticInferenceServiceUnifiedChatCompletionResponseHandler(String requestType, ResponseParser parseFunction) { | ||
super(requestType, parseFunction); | ||
} | ||
|
||
@Override | ||
public boolean canHandleStreamingResponses() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public InferenceServiceResults parseResult(Request request, Flow.Publisher<HttpResult> flow) { | ||
var serverSentEventProcessor = new ServerSentEventProcessor(new ServerSentEventParser()); | ||
var openAiProcessor = new OpenAiUnifiedStreamingProcessor(); // EIS uses the unified API spec | ||
|
||
flow.subscribe(serverSentEventProcessor); | ||
serverSentEventProcessor.subscribe(openAiProcessor); | ||
return new StreamingUnifiedChatCompletionResults(openAiProcessor); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...nference/external/http/sender/ElasticInferenceServiceUnifiedCompletionRequestManager.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,82 @@ | ||
/* | ||
* 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.external.http.sender; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.inference.InferenceServiceResults; | ||
import org.elasticsearch.threadpool.ThreadPool; | ||
import org.elasticsearch.xpack.inference.external.elastic.ElasticInferenceServiceUnifiedChatCompletionResponseHandler; | ||
import org.elasticsearch.xpack.inference.external.http.retry.RequestSender; | ||
import org.elasticsearch.xpack.inference.external.http.retry.ResponseHandler; | ||
import org.elasticsearch.xpack.inference.external.request.elastic.ElasticInferenceServiceUnifiedChatCompletionRequest; | ||
import org.elasticsearch.xpack.inference.external.response.openai.OpenAiChatCompletionResponseEntity; | ||
import org.elasticsearch.xpack.inference.services.elastic.completion.ElasticInferenceServiceCompletionModel; | ||
import org.elasticsearch.xpack.inference.telemetry.TraceContext; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Supplier; | ||
|
||
public class ElasticInferenceServiceUnifiedCompletionRequestManager extends ElasticInferenceServiceRequestManager { | ||
|
||
private static final Logger logger = LogManager.getLogger(ElasticInferenceServiceUnifiedCompletionRequestManager.class); | ||
|
||
private static final ResponseHandler HANDLER = createCompletionHandler(); | ||
|
||
public static ElasticInferenceServiceUnifiedCompletionRequestManager of( | ||
ElasticInferenceServiceCompletionModel model, | ||
ThreadPool threadPool, | ||
TraceContext traceContext | ||
) { | ||
return new ElasticInferenceServiceUnifiedCompletionRequestManager( | ||
Objects.requireNonNull(model), | ||
Objects.requireNonNull(threadPool), | ||
Objects.requireNonNull(traceContext) | ||
); | ||
} | ||
|
||
private final ElasticInferenceServiceCompletionModel model; | ||
private final TraceContext traceContext; | ||
|
||
private ElasticInferenceServiceUnifiedCompletionRequestManager( | ||
ElasticInferenceServiceCompletionModel model, | ||
ThreadPool threadPool, | ||
TraceContext traceContext | ||
) { | ||
super(threadPool, model); | ||
this.model = model; | ||
this.traceContext = traceContext; | ||
} | ||
|
||
@Override | ||
public void execute( | ||
InferenceInputs inferenceInputs, | ||
RequestSender requestSender, | ||
Supplier<Boolean> hasRequestCompletedFunction, | ||
ActionListener<InferenceServiceResults> listener | ||
) { | ||
|
||
ElasticInferenceServiceUnifiedChatCompletionRequest request = new ElasticInferenceServiceUnifiedChatCompletionRequest( | ||
inferenceInputs.castTo(UnifiedChatInput.class), | ||
model, | ||
traceContext | ||
); | ||
|
||
execute(new ExecutableInferenceRequest(requestSender, logger, request, HANDLER, hasRequestCompletedFunction, listener)); | ||
} | ||
|
||
private static ResponseHandler createCompletionHandler() { | ||
return new ElasticInferenceServiceUnifiedChatCompletionResponseHandler( | ||
"elastic inference service completion", | ||
// We use OpenAiChatCompletionResponseEntity here as the ElasticInferenceServiceResponseEntity fields are a subset of the OpenAI | ||
// one. | ||
OpenAiChatCompletionResponseEntity::fromResponse | ||
); | ||
} | ||
} |
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
85 changes: 85 additions & 0 deletions
85
...ference/external/request/elastic/ElasticInferenceServiceUnifiedChatCompletionRequest.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,85 @@ | ||
/* | ||
* 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.external.request.elastic; | ||
|
||
import org.apache.http.HttpHeaders; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.entity.ByteArrayEntity; | ||
import org.apache.http.message.BasicHeader; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.xcontent.XContentType; | ||
import org.elasticsearch.xpack.inference.external.http.sender.UnifiedChatInput; | ||
import org.elasticsearch.xpack.inference.external.request.HttpRequest; | ||
import org.elasticsearch.xpack.inference.external.request.Request; | ||
import org.elasticsearch.xpack.inference.services.elastic.completion.ElasticInferenceServiceCompletionModel; | ||
import org.elasticsearch.xpack.inference.telemetry.TraceContext; | ||
import org.elasticsearch.xpack.inference.telemetry.TraceContextHandler; | ||
|
||
import java.net.URI; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Objects; | ||
|
||
public class ElasticInferenceServiceUnifiedChatCompletionRequest implements Request { | ||
|
||
private final ElasticInferenceServiceCompletionModel model; | ||
private final UnifiedChatInput unifiedChatInput; | ||
private final TraceContextHandler traceContextHandler; | ||
|
||
public ElasticInferenceServiceUnifiedChatCompletionRequest( | ||
UnifiedChatInput unifiedChatInput, | ||
ElasticInferenceServiceCompletionModel model, | ||
TraceContext traceContext | ||
) { | ||
this.unifiedChatInput = Objects.requireNonNull(unifiedChatInput); | ||
this.model = Objects.requireNonNull(model); | ||
this.traceContextHandler = new TraceContextHandler(traceContext); | ||
} | ||
|
||
@Override | ||
public HttpRequest createHttpRequest() { | ||
var httpPost = new HttpPost(model.uri()); | ||
var requestEntity = Strings.toString( | ||
new ElasticInferenceServiceUnifiedChatCompletionRequestEntity(unifiedChatInput, model.getServiceSettings().modelId()) | ||
); | ||
|
||
ByteArrayEntity byteEntity = new ByteArrayEntity(requestEntity.getBytes(StandardCharsets.UTF_8)); | ||
httpPost.setEntity(byteEntity); | ||
|
||
traceContextHandler.propagateTraceContext(httpPost); | ||
httpPost.setHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, XContentType.JSON.mediaType())); | ||
|
||
return new HttpRequest(httpPost, getInferenceEntityId()); | ||
} | ||
|
||
@Override | ||
public URI getURI() { | ||
return model.uri(); | ||
} | ||
|
||
@Override | ||
public Request truncate() { | ||
// No truncation | ||
return this; | ||
} | ||
|
||
@Override | ||
public boolean[] getTruncationInfo() { | ||
// No truncation | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getInferenceEntityId() { | ||
return model.getInferenceEntityId(); | ||
} | ||
|
||
@Override | ||
public boolean isStreaming() { | ||
return true; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...e/external/request/elastic/ElasticInferenceServiceUnifiedChatCompletionRequestEntity.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,38 @@ | ||
/* | ||
* 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.external.request.elastic; | ||
|
||
import org.elasticsearch.xcontent.ToXContentObject; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
import org.elasticsearch.xpack.inference.external.http.sender.UnifiedChatInput; | ||
import org.elasticsearch.xpack.inference.external.unified.UnifiedChatCompletionRequestEntity; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
public class ElasticInferenceServiceUnifiedChatCompletionRequestEntity implements ToXContentObject { | ||
private static final String MODEL_FIELD = "model"; | ||
|
||
private final UnifiedChatCompletionRequestEntity unifiedRequestEntity; | ||
private final String modelId; | ||
|
||
public ElasticInferenceServiceUnifiedChatCompletionRequestEntity(UnifiedChatInput unifiedChatInput, String modelId) { | ||
this.unifiedRequestEntity = new UnifiedChatCompletionRequestEntity(Objects.requireNonNull(unifiedChatInput)); | ||
this.modelId = Objects.requireNonNull(modelId); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
unifiedRequestEntity.toXContent(builder, params); | ||
builder.field(MODEL_FIELD, modelId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reminder for myself that we can probably merge this and the openai class back together since they're sending the same stuff. |
||
builder.endObject(); | ||
|
||
return builder; | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this string with spaces in it correct? Seems like a bit of a weird value. Normally I think our non-error-message strings uses underscores instead of spaces. Definitely just a nit though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The OpenAI one does the same thing. Not sure whats best, but I think they should be consistent. I'll keep this for now.