|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.inference.external.action.alibabacloudsearch; |
| 9 | + |
| 10 | +import org.elasticsearch.ElasticsearchException; |
| 11 | +import org.elasticsearch.ElasticsearchStatusException; |
| 12 | +import org.elasticsearch.action.ActionListener; |
| 13 | +import org.elasticsearch.action.support.PlainActionFuture; |
| 14 | +import org.elasticsearch.common.settings.Settings; |
| 15 | +import org.elasticsearch.core.TimeValue; |
| 16 | +import org.elasticsearch.inference.InferenceServiceResults; |
| 17 | +import org.elasticsearch.inference.TaskType; |
| 18 | +import org.elasticsearch.rest.RestStatus; |
| 19 | +import org.elasticsearch.test.ESTestCase; |
| 20 | +import org.elasticsearch.test.http.MockWebServer; |
| 21 | +import org.elasticsearch.threadpool.ThreadPool; |
| 22 | +import org.elasticsearch.xpack.core.inference.action.InferenceAction; |
| 23 | +import org.elasticsearch.xpack.core.inference.results.ChatCompletionResults; |
| 24 | +import org.elasticsearch.xpack.inference.external.action.ExecutableAction; |
| 25 | +import org.elasticsearch.xpack.inference.external.http.HttpClientManager; |
| 26 | +import org.elasticsearch.xpack.inference.external.http.sender.ChatCompletionInput; |
| 27 | +import org.elasticsearch.xpack.inference.external.http.sender.DocumentsOnlyInput; |
| 28 | +import org.elasticsearch.xpack.inference.external.http.sender.Sender; |
| 29 | +import org.elasticsearch.xpack.inference.logging.ThrottlerManager; |
| 30 | +import org.elasticsearch.xpack.inference.services.ServiceComponentsTests; |
| 31 | +import org.elasticsearch.xpack.inference.services.alibabacloudsearch.completion.AlibabaCloudSearchCompletionModelTests; |
| 32 | +import org.elasticsearch.xpack.inference.services.alibabacloudsearch.completion.AlibabaCloudSearchCompletionServiceSettingsTests; |
| 33 | +import org.elasticsearch.xpack.inference.services.alibabacloudsearch.completion.AlibabaCloudSearchCompletionTaskSettingsTests; |
| 34 | +import org.junit.After; |
| 35 | +import org.junit.Before; |
| 36 | + |
| 37 | +import java.io.IOException; |
| 38 | +import java.util.List; |
| 39 | +import java.util.concurrent.TimeUnit; |
| 40 | + |
| 41 | +import static org.apache.lucene.tests.util.LuceneTestCase.expectThrows; |
| 42 | +import static org.elasticsearch.xpack.inference.Utils.inferenceUtilityPool; |
| 43 | +import static org.elasticsearch.xpack.inference.Utils.mockClusterServiceEmpty; |
| 44 | +import static org.elasticsearch.xpack.inference.external.action.ActionUtils.constructFailedToSendRequestMessage; |
| 45 | +import static org.elasticsearch.xpack.inference.results.ChatCompletionResultsTests.buildExpectationCompletion; |
| 46 | +import static org.elasticsearch.xpack.inference.services.settings.DefaultSecretSettingsTests.getSecretSettingsMap; |
| 47 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 48 | +import static org.hamcrest.Matchers.is; |
| 49 | +import static org.mockito.ArgumentMatchers.any; |
| 50 | +import static org.mockito.Mockito.doAnswer; |
| 51 | +import static org.mockito.Mockito.doThrow; |
| 52 | +import static org.mockito.Mockito.mock; |
| 53 | + |
| 54 | +public class AlibabaCloudSearchCompletionActionTests extends ESTestCase { |
| 55 | + |
| 56 | + private static final TimeValue TIMEOUT = new TimeValue(30, TimeUnit.SECONDS); |
| 57 | + private final MockWebServer webServer = new MockWebServer(); |
| 58 | + private ThreadPool threadPool; |
| 59 | + private HttpClientManager clientManager; |
| 60 | + |
| 61 | + @Before |
| 62 | + public void init() throws IOException { |
| 63 | + webServer.start(); |
| 64 | + threadPool = createThreadPool(inferenceUtilityPool()); |
| 65 | + clientManager = HttpClientManager.create(Settings.EMPTY, threadPool, mockClusterServiceEmpty(), mock(ThrottlerManager.class)); |
| 66 | + } |
| 67 | + |
| 68 | + @After |
| 69 | + public void shutdown() throws IOException { |
| 70 | + clientManager.close(); |
| 71 | + terminate(threadPool); |
| 72 | + webServer.close(); |
| 73 | + } |
| 74 | + |
| 75 | + public void testExecute_Success() { |
| 76 | + var sender = mock(Sender.class); |
| 77 | + |
| 78 | + var resultString = randomAlphaOfLength(100); |
| 79 | + doAnswer(invocation -> { |
| 80 | + ActionListener<InferenceServiceResults> listener = invocation.getArgument(3); |
| 81 | + listener.onResponse(new ChatCompletionResults(List.of(new ChatCompletionResults.Result(resultString)))); |
| 82 | + |
| 83 | + return Void.TYPE; |
| 84 | + }).when(sender).send(any(), any(), any(), any()); |
| 85 | + var action = createAction(threadPool, sender); |
| 86 | + |
| 87 | + PlainActionFuture<InferenceServiceResults> listener = new PlainActionFuture<>(); |
| 88 | + action.execute(new ChatCompletionInput(List.of(randomAlphaOfLength(10))), InferenceAction.Request.DEFAULT_TIMEOUT, listener); |
| 89 | + |
| 90 | + var result = listener.actionGet(TIMEOUT); |
| 91 | + assertThat(result.asMap(), is(buildExpectationCompletion(List.of(resultString)))); |
| 92 | + } |
| 93 | + |
| 94 | + public void testExecute_ListenerThrowsElasticsearchException_WhenSenderThrowsElasticsearchException() { |
| 95 | + var sender = mock(Sender.class); |
| 96 | + doThrow(new ElasticsearchException("error")).when(sender).send(any(), any(), any(), any()); |
| 97 | + var action = createAction(threadPool, sender); |
| 98 | + |
| 99 | + PlainActionFuture<InferenceServiceResults> listener = new PlainActionFuture<>(); |
| 100 | + action.execute(new ChatCompletionInput(List.of(randomAlphaOfLength(10))), InferenceAction.Request.DEFAULT_TIMEOUT, listener); |
| 101 | + |
| 102 | + var thrownException = expectThrows(ElasticsearchException.class, () -> listener.actionGet(TIMEOUT)); |
| 103 | + assertThat(thrownException.getMessage(), is("error")); |
| 104 | + } |
| 105 | + |
| 106 | + public void testExecute_ListenerThrowsInternalServerError_WhenSenderThrowsException() { |
| 107 | + var sender = mock(Sender.class); |
| 108 | + doThrow(new RuntimeException("error")).when(sender).send(any(), any(), any(), any()); |
| 109 | + var action = createAction(threadPool, sender); |
| 110 | + |
| 111 | + PlainActionFuture<InferenceServiceResults> listener = new PlainActionFuture<>(); |
| 112 | + action.execute(new ChatCompletionInput(List.of(randomAlphaOfLength(10))), InferenceAction.Request.DEFAULT_TIMEOUT, listener); |
| 113 | + |
| 114 | + var thrownException = expectThrows(ElasticsearchException.class, () -> listener.actionGet(TIMEOUT)); |
| 115 | + assertThat(thrownException.getMessage(), is(constructFailedToSendRequestMessage("AlibabaCloud Search completion"))); |
| 116 | + } |
| 117 | + |
| 118 | + public void testExecute_ThrowsIllegalArgumentException_WhenInputIsNotChatCompletionInput() { |
| 119 | + var action = createAction(threadPool, mock(Sender.class)); |
| 120 | + |
| 121 | + PlainActionFuture<InferenceServiceResults> listener = new PlainActionFuture<>(); |
| 122 | + assertThrows(IllegalArgumentException.class, () -> { |
| 123 | + action.execute(new DocumentsOnlyInput(List.of(randomAlphaOfLength(10))), InferenceAction.Request.DEFAULT_TIMEOUT, listener); |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + public void testExecute_ListenerThrowsElasticsearchStatusException_WhenInputSizeIsEven() { |
| 128 | + var action = createAction(threadPool, mock(Sender.class)); |
| 129 | + |
| 130 | + PlainActionFuture<InferenceServiceResults> listener = new PlainActionFuture<>(); |
| 131 | + action.execute( |
| 132 | + new ChatCompletionInput(List.of(randomAlphaOfLength(10), randomAlphaOfLength(10))), |
| 133 | + InferenceAction.Request.DEFAULT_TIMEOUT, |
| 134 | + listener |
| 135 | + ); |
| 136 | + |
| 137 | + var thrownException = expectThrows(ElasticsearchStatusException.class, () -> listener.actionGet(TIMEOUT)); |
| 138 | + assertThat( |
| 139 | + thrownException.getMessage(), |
| 140 | + is( |
| 141 | + "Alibaba Completion's inputs must be an odd number. The last input is the current query, " |
| 142 | + + "all preceding inputs are the completion history as pairs of user input and the assistant's response." |
| 143 | + ) |
| 144 | + ); |
| 145 | + assertThat(thrownException.status(), is(RestStatus.BAD_REQUEST)); |
| 146 | + } |
| 147 | + |
| 148 | + private ExecutableAction createAction(ThreadPool threadPool, Sender sender) { |
| 149 | + var model = AlibabaCloudSearchCompletionModelTests.createModel( |
| 150 | + "completion_test", |
| 151 | + TaskType.COMPLETION, |
| 152 | + AlibabaCloudSearchCompletionServiceSettingsTests.getServiceSettingsMap("completion_test", "host", "default"), |
| 153 | + AlibabaCloudSearchCompletionTaskSettingsTests.getTaskSettingsMap(null), |
| 154 | + getSecretSettingsMap("secret") |
| 155 | + ); |
| 156 | + |
| 157 | + var serviceComponents = ServiceComponentsTests.createWithEmptySettings(threadPool); |
| 158 | + return new AlibabaCloudSearchCompletionAction(sender, model, serviceComponents); |
| 159 | + } |
| 160 | +} |
0 commit comments