-
Notifications
You must be signed in to change notification settings - Fork 240
Add ML gRPC support #2070
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
Open
nathaliellenaa
wants to merge
1
commit into
opensearch-project:main
Choose a base branch
from
nathaliellenaa:ml_grpc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add ML gRPC support #2070
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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
73 changes: 73 additions & 0 deletions
73
...pc/src/main/java/org/opensearch/client/transport/grpc/ml/MlExecuteAgentStreamRequest.java
This file contains hidden or 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,73 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.client.transport.grpc.ml; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * Request for the ML streaming agent execute API ({@code MLService/ExecuteAgentStream}). | ||
| */ | ||
| public final class MlExecuteAgentStreamRequest { | ||
|
|
||
| private final String agentId; | ||
| private final MlParameters parameters; | ||
|
|
||
| private MlExecuteAgentStreamRequest(Builder builder) { | ||
| if (builder.agentId == null || builder.agentId.isEmpty()) { | ||
| throw new IllegalArgumentException("agentId is required"); | ||
| } | ||
| this.agentId = builder.agentId; | ||
| this.parameters = builder.parameters; | ||
| } | ||
|
|
||
| /** | ||
| * The ID of the agent to execute. Never null. | ||
| */ | ||
| public String agentId() { | ||
| return agentId; | ||
| } | ||
|
|
||
| /** | ||
| * The request parameters, or null if unset. | ||
| */ | ||
| @Nullable | ||
| public MlParameters parameters() { | ||
| return parameters; | ||
| } | ||
|
|
||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public static final class Builder { | ||
| private String agentId; | ||
| private MlParameters parameters; | ||
|
|
||
| public Builder agentId(String agentId) { | ||
| this.agentId = agentId; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder parameters(MlParameters parameters) { | ||
| this.parameters = parameters; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Configures the parameters using a builder function. | ||
| */ | ||
| public Builder parameters(java.util.function.Function<MlParameters.Builder, MlParameters.Builder> fn) { | ||
| return parameters(fn.apply(MlParameters.builder()).build()); | ||
| } | ||
|
|
||
| public MlExecuteAgentStreamRequest build() { | ||
| return new MlExecuteAgentStreamRequest(this); | ||
| } | ||
| } | ||
| } |
50 changes: 50 additions & 0 deletions
50
...-client-grpc/src/main/java/org/opensearch/client/transport/grpc/ml/MlInferenceResult.java
This file contains hidden or 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,50 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.client.transport.grpc.ml; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * One inference result of a streamed ML response, holding the outputs the model | ||
| * produced for this chunk. | ||
| */ | ||
| public final class MlInferenceResult { | ||
|
|
||
| private final List<MlOutput> output; | ||
|
|
||
| private MlInferenceResult(Builder builder) { | ||
| this.output = Collections.unmodifiableList(new ArrayList<>(builder.output)); | ||
| } | ||
|
|
||
| /** | ||
| * The outputs of this inference result. Never null; empty when the server sent none. | ||
| */ | ||
| public List<MlOutput> output() { | ||
| return output; | ||
| } | ||
|
|
||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public static final class Builder { | ||
| private final List<MlOutput> output = new ArrayList<>(); | ||
|
|
||
| public Builder addOutput(MlOutput out) { | ||
| this.output.add(out); | ||
| return this; | ||
| } | ||
|
|
||
| public MlInferenceResult build() { | ||
| return new MlInferenceResult(this); | ||
| } | ||
| } | ||
| } |
74 changes: 74 additions & 0 deletions
74
java-client-grpc/src/main/java/org/opensearch/client/transport/grpc/ml/MlMessage.java
This file contains hidden or 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,74 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.client.transport.grpc.ml; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * A single chat message in an ML streaming request. | ||
| */ | ||
| public final class MlMessage { | ||
|
|
||
| private final String role; | ||
| private final String content; | ||
|
|
||
| private MlMessage(Builder builder) { | ||
| this.role = builder.role; | ||
| this.content = builder.content; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a message with the given role and content. | ||
| * | ||
| * @param role the message role (e.g., {@code "user"}, {@code "assistant"}, {@code "system"}) | ||
| * @param content the message content | ||
| */ | ||
| public static MlMessage of(String role, String content) { | ||
| return builder().role(role).content(content).build(); | ||
| } | ||
|
|
||
| /** | ||
| * The message role, or null if unset. | ||
| */ | ||
| @Nullable | ||
| public String role() { | ||
| return role; | ||
| } | ||
|
|
||
| /** | ||
| * The message content, or null if unset. | ||
| */ | ||
| @Nullable | ||
| public String content() { | ||
| return content; | ||
| } | ||
|
|
||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public static final class Builder { | ||
| private String role; | ||
| private String content; | ||
|
|
||
| public Builder role(String role) { | ||
| this.role = role; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder content(String content) { | ||
| this.content = content; | ||
| return this; | ||
| } | ||
|
|
||
| public MlMessage build() { | ||
| return new MlMessage(this); | ||
| } | ||
| } | ||
| } |
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.
@nathaliellenaa thank you for the pull request.
A few things here:
GrpcTransportis a low level generic transport implementation (see pleaseOpenSearchTransportwhat the expected primitives should be exposed)XxxTransportclasses are not supposed to be used in isolation, they are passed toOpenSearchClientwhich provides targeted methods (bulk, mget, ...) or/and clients (OpenSearchClusterClient, OpenSearchIndexClient, ...), in fact we already have one for ML as well -OpenSearchMlClientThe transport itself should not expose anything specific to particular API (like ml fe). The elephant in the room here is that
OpenSearchTransportdoes not support streaming (but underlying clients do https://github.com/opensearch-project/OpenSearch/blob/main/client/rest/src/main/java/org/opensearch/client/RestClient.java#L324), so we have to close this gap first (let me pick up shortly). Once we have the streaming implementation, we could looking into bringing it toOpenSearchMlClient.Hope it makes sense, thanks
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.
Thanks @reta, it makes sense that
GrpcTransportshouldn't be exposing ML-specific methods. I went that route because of the gap you identified:Transport#performRequestreturns a singleResponseT, so there was no way to express a server-streaming RPC returning a sequence of chunks through the existing primitives.Happy to wait for the streaming implementation and then rebase this on top of it.