-
Notifications
You must be signed in to change notification settings - Fork 25.8k
[Inference API] Add Completion Inference API for Alibaba Cloud AI Search Model #112512
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
Merged
davidkyle
merged 10 commits into
elastic:main
from
Huaixinww:feature/add-alibabacloud-ai-search-completion-model
Sep 12, 2024
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
59b3cc9
add alibabacloud ai search completion model
Huaixinww 713cb76
add ut
Huaixinww 10e4c21
improve
Huaixinww 4ee0ed4
Add change log
davidkyle 494789f
merge task_settings
Huaixinww 296d0cc
Merge branch 'main' into feature/add-alibabacloud-ai-search-completio…
elasticmachine e36cb8f
improve error message
Huaixinww e5baf29
Update x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/…
davidkyle 9430acf
Merge branch 'elastic:main' into feature/add-alibabacloud-ai-search-c…
Huaixinww 87c4406
Merge branch 'main' into feature/add-alibabacloud-ai-search-completio…
elasticmachine 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pr: 112512 | ||
| summary: Add Completion Inference API for Alibaba Cloud AI Search Model | ||
| area: Machine Learning | ||
| type: enhancement | ||
| issues: [] |
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
87 changes: 87 additions & 0 deletions
87
...pack/inference/external/action/alibabacloudsearch/AlibabaCloudSearchCompletionAction.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,87 @@ | ||
| /* | ||
| * 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.action.alibabacloudsearch; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.elasticsearch.ElasticsearchException; | ||
| import org.elasticsearch.ElasticsearchStatusException; | ||
| import org.elasticsearch.action.ActionListener; | ||
| import org.elasticsearch.core.TimeValue; | ||
| import org.elasticsearch.inference.InferenceServiceResults; | ||
| import org.elasticsearch.inference.TaskType; | ||
| import org.elasticsearch.rest.RestStatus; | ||
| import org.elasticsearch.xpack.inference.external.action.ExecutableAction; | ||
| import org.elasticsearch.xpack.inference.external.alibabacloudsearch.AlibabaCloudSearchAccount; | ||
| import org.elasticsearch.xpack.inference.external.http.sender.AlibabaCloudSearchCompletionRequestManager; | ||
| import org.elasticsearch.xpack.inference.external.http.sender.DocumentsOnlyInput; | ||
| import org.elasticsearch.xpack.inference.external.http.sender.InferenceInputs; | ||
| import org.elasticsearch.xpack.inference.external.http.sender.Sender; | ||
| import org.elasticsearch.xpack.inference.services.ServiceComponents; | ||
| import org.elasticsearch.xpack.inference.services.alibabacloudsearch.completion.AlibabaCloudSearchCompletionModel; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| import static org.elasticsearch.core.Strings.format; | ||
| import static org.elasticsearch.xpack.inference.external.action.ActionUtils.constructFailedToSendRequestMessage; | ||
| import static org.elasticsearch.xpack.inference.external.action.ActionUtils.createInternalServerError; | ||
| import static org.elasticsearch.xpack.inference.external.action.ActionUtils.wrapFailuresInElasticsearchException; | ||
|
|
||
| public class AlibabaCloudSearchCompletionAction implements ExecutableAction { | ||
| private static final Logger logger = LogManager.getLogger(AlibabaCloudSearchCompletionAction.class); | ||
|
|
||
| private final AlibabaCloudSearchAccount account; | ||
| private final AlibabaCloudSearchCompletionModel model; | ||
| private final String failedToSendRequestErrorMessage; | ||
| private final Sender sender; | ||
| private final AlibabaCloudSearchCompletionRequestManager requestCreator; | ||
|
|
||
| public AlibabaCloudSearchCompletionAction(Sender sender, AlibabaCloudSearchCompletionModel model, ServiceComponents serviceComponents) { | ||
| this.model = Objects.requireNonNull(model); | ||
| this.sender = Objects.requireNonNull(sender); | ||
| this.account = new AlibabaCloudSearchAccount(this.model.getSecretSettings().apiKey()); | ||
| this.failedToSendRequestErrorMessage = constructFailedToSendRequestMessage(null, "AlibabaCloud Search completion"); | ||
| this.requestCreator = AlibabaCloudSearchCompletionRequestManager.of(account, model, serviceComponents.threadPool()); | ||
| } | ||
|
|
||
| @Override | ||
| public void execute(InferenceInputs inferenceInputs, TimeValue timeout, ActionListener<InferenceServiceResults> listener) { | ||
| if (inferenceInputs instanceof DocumentsOnlyInput == false) { | ||
| listener.onFailure( | ||
| new ElasticsearchStatusException( | ||
| format("Invalid inference input type, task type [%s] do not support Field [query]", TaskType.COMPLETION), | ||
| RestStatus.INTERNAL_SERVER_ERROR | ||
| ) | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| var docsOnlyInput = (DocumentsOnlyInput) inferenceInputs; | ||
| if (docsOnlyInput.getInputs().size() % 2 == 0) { | ||
| listener.onFailure( | ||
| new ElasticsearchStatusException( | ||
| "Alibaba Completion's inputs must be an odd number, the last input is the current query.", | ||
|
davidkyle marked this conversation as resolved.
Outdated
|
||
| RestStatus.BAD_REQUEST | ||
| ) | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| ActionListener<InferenceServiceResults> wrappedListener = wrapFailuresInElasticsearchException( | ||
| failedToSendRequestErrorMessage, | ||
| listener | ||
| ); | ||
| sender.send(requestCreator, inferenceInputs, timeout, wrappedListener); | ||
| } catch (ElasticsearchException e) { | ||
| listener.onFailure(e); | ||
| } catch (Exception e) { | ||
| listener.onFailure(createInternalServerError(e, failedToSendRequestErrorMessage)); | ||
| } | ||
| } | ||
| } | ||
76 changes: 76 additions & 0 deletions
76
...arch/xpack/inference/external/http/sender/AlibabaCloudSearchCompletionRequestManager.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,76 @@ | ||
| /* | ||
| * 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.alibabacloudsearch.AlibabaCloudSearchAccount; | ||
| import org.elasticsearch.xpack.inference.external.alibabacloudsearch.AlibabaCloudSearchResponseHandler; | ||
| 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.alibabacloudsearch.completion.AlibabaCloudSearchCompletionRequest; | ||
| import org.elasticsearch.xpack.inference.external.response.alibabacloudsearch.AlibabaCloudSearchCompletionResponseEntity; | ||
| import org.elasticsearch.xpack.inference.services.alibabacloudsearch.completion.AlibabaCloudSearchCompletionModel; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.function.Supplier; | ||
|
|
||
| public class AlibabaCloudSearchCompletionRequestManager extends AlibabaCloudSearchRequestManager { | ||
| private static final Logger logger = LogManager.getLogger(AlibabaCloudSearchCompletionRequestManager.class); | ||
|
|
||
| private static final ResponseHandler HANDLER = createCompletionHandler(); | ||
|
|
||
| private static ResponseHandler createCompletionHandler() { | ||
| return new AlibabaCloudSearchResponseHandler( | ||
| "alibaba cloud search completion", | ||
| AlibabaCloudSearchCompletionResponseEntity::fromResponse | ||
| ); | ||
| } | ||
|
|
||
| public static AlibabaCloudSearchCompletionRequestManager of( | ||
| AlibabaCloudSearchAccount account, | ||
| AlibabaCloudSearchCompletionModel model, | ||
| ThreadPool threadPool | ||
| ) { | ||
| return new AlibabaCloudSearchCompletionRequestManager( | ||
| Objects.requireNonNull(account), | ||
| Objects.requireNonNull(model), | ||
| Objects.requireNonNull(threadPool) | ||
| ); | ||
| } | ||
|
|
||
| private final AlibabaCloudSearchCompletionModel model; | ||
|
|
||
| private final AlibabaCloudSearchAccount account; | ||
|
|
||
| private AlibabaCloudSearchCompletionRequestManager( | ||
| AlibabaCloudSearchAccount account, | ||
| AlibabaCloudSearchCompletionModel model, | ||
| ThreadPool threadPool | ||
| ) { | ||
| super(threadPool, model); | ||
| this.account = Objects.requireNonNull(account); | ||
| this.model = Objects.requireNonNull(model); | ||
| } | ||
|
|
||
| @Override | ||
| public void execute( | ||
| InferenceInputs inferenceInputs, | ||
| RequestSender requestSender, | ||
| Supplier<Boolean> hasRequestCompletedFunction, | ||
| ActionListener<InferenceServiceResults> listener | ||
| ) { | ||
| List<String> input = DocumentsOnlyInput.of(inferenceInputs).getInputs(); | ||
| AlibabaCloudSearchCompletionRequest request = new AlibabaCloudSearchCompletionRequest(account, input, model); | ||
| execute(new ExecutableInferenceRequest(requestSender, logger, request, HANDLER, hasRequestCompletedFunction, listener)); | ||
| } | ||
| } |
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
112 changes: 112 additions & 0 deletions
112
...e/external/request/alibabacloudsearch/completion/AlibabaCloudSearchCompletionRequest.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,112 @@ | ||
| /* | ||
| * 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.alibabacloudsearch.completion; | ||
|
|
||
| import org.apache.http.HttpHeaders; | ||
| import org.apache.http.client.methods.HttpPost; | ||
| import org.apache.http.client.utils.URIBuilder; | ||
| import org.apache.http.entity.ByteArrayEntity; | ||
| import org.elasticsearch.common.Strings; | ||
| import org.elasticsearch.xcontent.XContentType; | ||
| import org.elasticsearch.xpack.inference.external.alibabacloudsearch.AlibabaCloudSearchAccount; | ||
| import org.elasticsearch.xpack.inference.external.request.HttpRequest; | ||
| import org.elasticsearch.xpack.inference.external.request.Request; | ||
| import org.elasticsearch.xpack.inference.external.request.alibabacloudsearch.AlibabaCloudSearchRequest; | ||
| import org.elasticsearch.xpack.inference.external.request.alibabacloudsearch.AlibabaCloudSearchUtils; | ||
| import org.elasticsearch.xpack.inference.services.alibabacloudsearch.completion.AlibabaCloudSearchCompletionModel; | ||
| import org.elasticsearch.xpack.inference.services.alibabacloudsearch.completion.AlibabaCloudSearchCompletionTaskSettings; | ||
|
|
||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| import static org.elasticsearch.xpack.inference.external.request.RequestUtils.buildUri; | ||
| import static org.elasticsearch.xpack.inference.external.request.RequestUtils.createAuthBearerHeader; | ||
|
|
||
| public class AlibabaCloudSearchCompletionRequest extends AlibabaCloudSearchRequest { | ||
| private final AlibabaCloudSearchAccount account; | ||
| private final List<String> input; | ||
| private final URI uri; | ||
| private final AlibabaCloudSearchCompletionTaskSettings taskSettings; | ||
| private final String model; | ||
| private final String host; | ||
| private final String workspaceName; | ||
| private final String httpSchema; | ||
| private final String inferenceEntityId; | ||
|
|
||
| public AlibabaCloudSearchCompletionRequest( | ||
| AlibabaCloudSearchAccount account, | ||
| List<String> input, | ||
| AlibabaCloudSearchCompletionModel completionModel | ||
| ) { | ||
| Objects.requireNonNull(completionModel); | ||
|
|
||
| this.account = Objects.requireNonNull(account); | ||
| this.input = Objects.requireNonNull(input); | ||
| taskSettings = completionModel.getTaskSettings(); | ||
| model = completionModel.getServiceSettings().getCommonSettings().modelId(); | ||
| host = completionModel.getServiceSettings().getCommonSettings().getHost(); | ||
| workspaceName = completionModel.getServiceSettings().getCommonSettings().getWorkspaceName(); | ||
| httpSchema = completionModel.getServiceSettings().getCommonSettings().getHttpSchema() != null | ||
| ? completionModel.getServiceSettings().getCommonSettings().getHttpSchema() | ||
| : "https"; | ||
| uri = buildUri(null, AlibabaCloudSearchUtils.SERVICE_NAME, this::buildDefaultUri); | ||
| inferenceEntityId = completionModel.getInferenceEntityId(); | ||
| } | ||
|
|
||
| @Override | ||
| public HttpRequest createHttpRequest() { | ||
| HttpPost httpPost = new HttpPost(uri); | ||
|
|
||
| ByteArrayEntity byteEntity = new ByteArrayEntity( | ||
| Strings.toString(new AlibabaCloudSearchCompletionRequestEntity(input, taskSettings, model)).getBytes(StandardCharsets.UTF_8) | ||
| ); | ||
| httpPost.setEntity(byteEntity); | ||
|
|
||
| httpPost.setHeader(HttpHeaders.CONTENT_TYPE, XContentType.JSON.mediaType()); | ||
| httpPost.setHeader(createAuthBearerHeader(account.apiKey())); | ||
|
|
||
| return new HttpRequest(httpPost, getInferenceEntityId()); | ||
| } | ||
|
|
||
| @Override | ||
| public String getInferenceEntityId() { | ||
| return inferenceEntityId; | ||
| } | ||
|
|
||
| @Override | ||
| public URI getURI() { | ||
| return uri; | ||
| } | ||
|
|
||
| @Override | ||
| public Request truncate() { | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean[] getTruncationInfo() { | ||
| return null; | ||
| } | ||
|
|
||
| URI buildDefaultUri() throws URISyntaxException { | ||
| return new URIBuilder().setScheme(httpSchema) | ||
| .setHost(host) | ||
| .setPathSegments( | ||
| AlibabaCloudSearchUtils.VERSION_3, | ||
| AlibabaCloudSearchUtils.OPENAPI_PATH, | ||
| AlibabaCloudSearchUtils.WORKSPACE_PATH, | ||
| workspaceName, | ||
| AlibabaCloudSearchUtils.COMPLETION_PATH, | ||
| model | ||
| ) | ||
| .build(); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.