Skip to content

[ML] Consolidate ExecutableActions #110806

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
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public static ActionListener<InferenceServiceResults> wrapFailuresInElasticsearc
String errorMessage,
ActionListener<InferenceServiceResults> listener
) {
return ActionListener.wrap(listener::onResponse, e -> {
return listener.delegateResponse((l, e) -> {
var unwrappedException = ExceptionsHelper.unwrapCause(e);

if (unwrappedException instanceof ElasticsearchException esException) {
listener.onFailure(esException);
l.onFailure(esException);
} else {
listener.onFailure(createInternalServerError(unwrappedException, errorMessage));
l.onFailure(createInternalServerError(unwrappedException, errorMessage));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,38 @@
* 2.0.
*/

package org.elasticsearch.xpack.inference.external.action.amazonbedrock;
package org.elasticsearch.xpack.inference.external.action;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.inference.InferenceServiceResults;
import org.elasticsearch.xpack.inference.external.action.ExecutableAction;
import org.elasticsearch.xpack.inference.external.http.sender.InferenceInputs;
import org.elasticsearch.xpack.inference.external.http.sender.RequestManager;
import org.elasticsearch.xpack.inference.external.http.sender.Sender;

import java.util.Objects;

import static org.elasticsearch.xpack.inference.external.action.ActionUtils.createInternalServerError;
import static org.elasticsearch.xpack.inference.external.action.ActionUtils.wrapFailuresInElasticsearchException;

public class AmazonBedrockEmbeddingsAction implements ExecutableAction {
public class SenderExecutableAction implements ExecutableAction {

private final Sender sender;
private final RequestManager requestManager;
private final String errorMessage;
private final String failedToSendRequestErrorMessage;

public AmazonBedrockEmbeddingsAction(Sender sender, RequestManager requestManager, String errorMessage) {
public SenderExecutableAction(Sender sender, RequestManager requestManager, String failedToSendRequestErrorMessage) {
this.sender = Objects.requireNonNull(sender);
this.requestManager = Objects.requireNonNull(requestManager);
this.errorMessage = Objects.requireNonNull(errorMessage);
this.failedToSendRequestErrorMessage = Objects.requireNonNull(failedToSendRequestErrorMessage);
}

@Override
public void execute(InferenceInputs inferenceInputs, TimeValue timeout, ActionListener<InferenceServiceResults> listener) {
var wrappedListener = wrapFailuresInElasticsearchException(failedToSendRequestErrorMessage, listener);
try {
ActionListener<InferenceServiceResults> wrappedListener = wrapFailuresInElasticsearchException(errorMessage, listener);

sender.send(requestManager, inferenceInputs, timeout, wrappedListener);
} catch (ElasticsearchException e) {
listener.onFailure(e);
} catch (Exception e) {
listener.onFailure(createInternalServerError(e, errorMessage));
wrappedListener.onFailure(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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;

import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.inference.InferenceServiceResults;
import org.elasticsearch.rest.RestStatus;
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.RequestManager;
import org.elasticsearch.xpack.inference.external.http.sender.Sender;

import java.util.Objects;

public class SingleInputSenderExecutableAction extends SenderExecutableAction {
private final String requestTypeForInputValidationError;

public SingleInputSenderExecutableAction(
Sender sender,
RequestManager requestManager,
String failedToSendRequestErrorMessage,
String requestTypeForInputValidationError
) {
super(sender, requestManager, failedToSendRequestErrorMessage);
this.requestTypeForInputValidationError = Objects.requireNonNull(requestTypeForInputValidationError);
}

@Override
public void execute(InferenceInputs inferenceInputs, TimeValue timeout, ActionListener<InferenceServiceResults> listener) {
if (inferenceInputs instanceof DocumentsOnlyInput == false) {
listener.onFailure(new ElasticsearchStatusException("Invalid inference input type", RestStatus.INTERNAL_SERVER_ERROR));
return;
}

var docsOnlyInput = (DocumentsOnlyInput) inferenceInputs;
if (docsOnlyInput.getInputs().size() > 1) {
listener.onFailure(
new ElasticsearchStatusException(requestTypeForInputValidationError + " only accepts 1 input", RestStatus.BAD_REQUEST)
);
return;
}

super.execute(inferenceInputs, timeout, listener);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.xpack.inference.external.action.ExecutableAction;
import org.elasticsearch.xpack.inference.external.action.SenderExecutableAction;
import org.elasticsearch.xpack.inference.external.http.sender.AmazonBedrockChatCompletionRequestManager;
import org.elasticsearch.xpack.inference.external.http.sender.AmazonBedrockEmbeddingsRequestManager;
import org.elasticsearch.xpack.inference.external.http.sender.Sender;
Expand Down Expand Up @@ -43,14 +44,14 @@ public ExecutableAction create(AmazonBedrockEmbeddingsModel embeddingsModel, Map
timeout
);
var errorMessage = constructFailedToSendRequestMessage(null, "Amazon Bedrock embeddings");
return new AmazonBedrockEmbeddingsAction(sender, requestManager, errorMessage);
return new SenderExecutableAction(sender, requestManager, errorMessage);
}

@Override
public ExecutableAction create(AmazonBedrockChatCompletionModel completionModel, Map<String, Object> taskSettings) {
var overriddenModel = AmazonBedrockChatCompletionModel.of(completionModel, taskSettings);
var requestManager = new AmazonBedrockChatCompletionRequestManager(overriddenModel, serviceComponents.threadPool(), timeout);
var errorMessage = constructFailedToSendRequestMessage(null, "Amazon Bedrock completion");
return new AmazonBedrockChatCompletionAction(sender, requestManager, errorMessage);
return new SenderExecutableAction(sender, requestManager, errorMessage);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@
package org.elasticsearch.xpack.inference.external.action.anthropic;

import org.elasticsearch.xpack.inference.external.action.ExecutableAction;
import org.elasticsearch.xpack.inference.external.action.SingleInputSenderExecutableAction;
import org.elasticsearch.xpack.inference.external.http.sender.AnthropicCompletionRequestManager;
import org.elasticsearch.xpack.inference.external.http.sender.Sender;
import org.elasticsearch.xpack.inference.services.ServiceComponents;
import org.elasticsearch.xpack.inference.services.anthropic.completion.AnthropicChatCompletionModel;

import java.util.Map;
import java.util.Objects;

import static org.elasticsearch.xpack.inference.external.action.ActionUtils.constructFailedToSendRequestMessage;

/**
* Provides a way to construct an {@link ExecutableAction} using the visitor pattern based on the anthropic model type.
*/
public class AnthropicActionCreator implements AnthropicActionVisitor {
private static final String ERROR_PREFIX = "Anthropic chat completions";
private final Sender sender;
private final ServiceComponents serviceComponents;

Expand All @@ -30,7 +35,8 @@ public AnthropicActionCreator(Sender sender, ServiceComponents serviceComponents
@Override
public ExecutableAction create(AnthropicChatCompletionModel model, Map<String, Object> taskSettings) {
var overriddenModel = AnthropicChatCompletionModel.of(model, taskSettings);

return new AnthropicChatCompletionAction(sender, overriddenModel, serviceComponents);
var requestCreator = AnthropicCompletionRequestManager.of(overriddenModel, serviceComponents.threadPool());
var errorMessage = constructFailedToSendRequestMessage(overriddenModel.getUri(), ERROR_PREFIX);
return new SingleInputSenderExecutableAction(sender, requestCreator, errorMessage, ERROR_PREFIX);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package org.elasticsearch.xpack.inference.external.action.azureaistudio;

import org.elasticsearch.xpack.inference.external.action.ExecutableAction;
import org.elasticsearch.xpack.inference.external.action.SenderExecutableAction;
import org.elasticsearch.xpack.inference.external.http.sender.AzureAiStudioChatCompletionRequestManager;
import org.elasticsearch.xpack.inference.external.http.sender.AzureAiStudioEmbeddingsRequestManager;
import org.elasticsearch.xpack.inference.external.http.sender.Sender;
Expand All @@ -34,7 +35,7 @@ public ExecutableAction create(AzureAiStudioChatCompletionModel completionModel,
var overriddenModel = AzureAiStudioChatCompletionModel.of(completionModel, taskSettings);
var requestManager = new AzureAiStudioChatCompletionRequestManager(overriddenModel, serviceComponents.threadPool());
var errorMessage = constructFailedToSendRequestMessage(completionModel.uri(), "Azure AI Studio completion");
return new AzureAiStudioAction(sender, requestManager, errorMessage);
return new SenderExecutableAction(sender, requestManager, errorMessage);
}

@Override
Expand All @@ -46,6 +47,6 @@ public ExecutableAction create(AzureAiStudioEmbeddingsModel embeddingsModel, Map
serviceComponents.threadPool()
);
var errorMessage = constructFailedToSendRequestMessage(embeddingsModel.uri(), "Azure AI Studio embeddings");
return new AzureAiStudioAction(sender, requestManager, errorMessage);
return new SenderExecutableAction(sender, requestManager, errorMessage);
}
}
Loading