|
| 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.services.mistral.action; |
| 9 | + |
| 10 | +import org.apache.http.HttpHeaders; |
| 11 | +import org.elasticsearch.ElasticsearchException; |
| 12 | +import org.elasticsearch.action.support.PlainActionFuture; |
| 13 | +import org.elasticsearch.common.settings.Settings; |
| 14 | +import org.elasticsearch.core.TimeValue; |
| 15 | +import org.elasticsearch.inference.InferenceServiceResults; |
| 16 | +import org.elasticsearch.test.ESTestCase; |
| 17 | +import org.elasticsearch.test.http.MockResponse; |
| 18 | +import org.elasticsearch.test.http.MockWebServer; |
| 19 | +import org.elasticsearch.threadpool.ThreadPool; |
| 20 | +import org.elasticsearch.xcontent.XContentType; |
| 21 | +import org.elasticsearch.xpack.core.inference.action.InferenceAction; |
| 22 | +import org.elasticsearch.xpack.inference.common.TruncatorTests; |
| 23 | +import org.elasticsearch.xpack.inference.external.http.HttpClientManager; |
| 24 | +import org.elasticsearch.xpack.inference.external.http.sender.ChatCompletionInput; |
| 25 | +import org.elasticsearch.xpack.inference.external.http.sender.HttpRequestSenderTests; |
| 26 | +import org.elasticsearch.xpack.inference.external.http.sender.Sender; |
| 27 | +import org.elasticsearch.xpack.inference.logging.ThrottlerManager; |
| 28 | +import org.elasticsearch.xpack.inference.services.ServiceComponents; |
| 29 | +import org.elasticsearch.xpack.inference.services.mistral.completions.MistralChatCompletionModelTests; |
| 30 | +import org.junit.After; |
| 31 | +import org.junit.Before; |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Map; |
| 36 | +import java.util.concurrent.TimeUnit; |
| 37 | + |
| 38 | +import static org.elasticsearch.xpack.core.inference.results.ChatCompletionResultsTests.buildExpectationCompletion; |
| 39 | +import static org.elasticsearch.xpack.inference.Utils.inferenceUtilityPool; |
| 40 | +import static org.elasticsearch.xpack.inference.Utils.mockClusterServiceEmpty; |
| 41 | +import static org.elasticsearch.xpack.inference.external.http.Utils.entityAsMap; |
| 42 | +import static org.elasticsearch.xpack.inference.external.http.Utils.getUrl; |
| 43 | +import static org.elasticsearch.xpack.inference.external.http.retry.RetrySettingsTests.buildSettingsWithRetryFields; |
| 44 | +import static org.elasticsearch.xpack.inference.external.http.sender.HttpRequestSenderTests.createSender; |
| 45 | +import static org.elasticsearch.xpack.inference.logging.ThrottlerManagerTests.mockThrottlerManager; |
| 46 | +import static org.elasticsearch.xpack.inference.services.ServiceComponentsTests.createWithEmptySettings; |
| 47 | +import static org.hamcrest.Matchers.equalTo; |
| 48 | +import static org.hamcrest.Matchers.hasSize; |
| 49 | +import static org.hamcrest.Matchers.is; |
| 50 | +import static org.mockito.Mockito.mock; |
| 51 | + |
| 52 | +public class MistralActionCreatorTests extends ESTestCase { |
| 53 | + private static final TimeValue TIMEOUT = new TimeValue(30, TimeUnit.SECONDS); |
| 54 | + private final MockWebServer webServer = new MockWebServer(); |
| 55 | + private ThreadPool threadPool; |
| 56 | + private HttpClientManager clientManager; |
| 57 | + |
| 58 | + @Before |
| 59 | + public void init() throws Exception { |
| 60 | + webServer.start(); |
| 61 | + threadPool = createThreadPool(inferenceUtilityPool()); |
| 62 | + clientManager = HttpClientManager.create(Settings.EMPTY, threadPool, mockClusterServiceEmpty(), mock(ThrottlerManager.class)); |
| 63 | + } |
| 64 | + |
| 65 | + @After |
| 66 | + public void shutdown() throws IOException { |
| 67 | + clientManager.close(); |
| 68 | + terminate(threadPool); |
| 69 | + webServer.close(); |
| 70 | + } |
| 71 | + |
| 72 | + public void testExecute_ReturnsSuccessfulResponse_ForChatCompletionAction() throws IOException { |
| 73 | + var senderFactory = HttpRequestSenderTests.createSenderFactory(threadPool, clientManager); |
| 74 | + |
| 75 | + try (var sender = createSender(senderFactory)) { |
| 76 | + sender.start(); |
| 77 | + |
| 78 | + String responseJson = """ |
| 79 | + { |
| 80 | + "object": "chat.completion", |
| 81 | + "id": "", |
| 82 | + "created": 1745855316, |
| 83 | + "model": "/repository", |
| 84 | + "system_fingerprint": "3.2.3-sha-a1f3ebe", |
| 85 | + "choices": [ |
| 86 | + { |
| 87 | + "index": 0, |
| 88 | + "message": { |
| 89 | + "role": "assistant", |
| 90 | + "content": "Hello there, how may I assist you today?" |
| 91 | + }, |
| 92 | + "logprobs": null, |
| 93 | + "finish_reason": "stop" |
| 94 | + } |
| 95 | + ], |
| 96 | + "usage": { |
| 97 | + "prompt_tokens": 8, |
| 98 | + "completion_tokens": 50, |
| 99 | + "total_tokens": 58 |
| 100 | + } |
| 101 | + } |
| 102 | + """; |
| 103 | + webServer.enqueue(new MockResponse().setResponseCode(200).setBody(responseJson)); |
| 104 | + |
| 105 | + PlainActionFuture<InferenceServiceResults> listener = createChatCompletionFuture(sender, createWithEmptySettings(threadPool)); |
| 106 | + |
| 107 | + var result = listener.actionGet(TIMEOUT); |
| 108 | + |
| 109 | + assertThat(result.asMap(), is(buildExpectationCompletion(List.of("Hello there, how may I assist you today?")))); |
| 110 | + |
| 111 | + assertChatCompletionRequest(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public void testSend_FailsFromInvalidResponseFormat_ForChatCompletionAction() throws IOException { |
| 116 | + var settings = buildSettingsWithRetryFields( |
| 117 | + TimeValue.timeValueMillis(1), |
| 118 | + TimeValue.timeValueMinutes(1), |
| 119 | + TimeValue.timeValueSeconds(0) |
| 120 | + ); |
| 121 | + var senderFactory = HttpRequestSenderTests.createSenderFactory(threadPool, clientManager, settings); |
| 122 | + |
| 123 | + try (var sender = createSender(senderFactory)) { |
| 124 | + sender.start(); |
| 125 | + |
| 126 | + String responseJson = """ |
| 127 | + { |
| 128 | + "invalid_field": "unexpected" |
| 129 | + } |
| 130 | + """; |
| 131 | + webServer.enqueue(new MockResponse().setResponseCode(200).setBody(responseJson)); |
| 132 | + |
| 133 | + PlainActionFuture<InferenceServiceResults> listener = createChatCompletionFuture( |
| 134 | + sender, |
| 135 | + new ServiceComponents(threadPool, mockThrottlerManager(), settings, TruncatorTests.createTruncator()) |
| 136 | + ); |
| 137 | + |
| 138 | + var thrownException = expectThrows(ElasticsearchException.class, () -> listener.actionGet(TIMEOUT)); |
| 139 | + assertThat( |
| 140 | + thrownException.getMessage(), |
| 141 | + is("Failed to send Mistral completion request from inference entity id " + "[id]. Cause: Required [choices]") |
| 142 | + ); |
| 143 | + |
| 144 | + assertChatCompletionRequest(); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private PlainActionFuture<InferenceServiceResults> createChatCompletionFuture(Sender sender, ServiceComponents threadPool) { |
| 149 | + var model = MistralChatCompletionModelTests.createCompletionModel("secret", "model"); |
| 150 | + model.setURI(getUrl(webServer)); |
| 151 | + var actionCreator = new MistralActionCreator(sender, threadPool); |
| 152 | + var action = actionCreator.create(model); |
| 153 | + |
| 154 | + PlainActionFuture<InferenceServiceResults> listener = new PlainActionFuture<>(); |
| 155 | + action.execute(new ChatCompletionInput(List.of("Hello"), false), InferenceAction.Request.DEFAULT_TIMEOUT, listener); |
| 156 | + return listener; |
| 157 | + } |
| 158 | + |
| 159 | + private void assertChatCompletionRequest() throws IOException { |
| 160 | + assertThat(webServer.requests(), hasSize(1)); |
| 161 | + assertNull(webServer.requests().get(0).getUri().getQuery()); |
| 162 | + assertThat( |
| 163 | + webServer.requests().get(0).getHeader(HttpHeaders.CONTENT_TYPE), |
| 164 | + equalTo(XContentType.JSON.mediaTypeWithoutParameters()) |
| 165 | + ); |
| 166 | + assertThat(webServer.requests().get(0).getHeader(HttpHeaders.AUTHORIZATION), equalTo("Bearer secret")); |
| 167 | + |
| 168 | + var requestMap = entityAsMap(webServer.requests().get(0).getBody()); |
| 169 | + assertThat(requestMap.size(), is(4)); |
| 170 | + assertThat(requestMap.get("messages"), is(List.of(Map.of("role", "user", "content", "Hello")))); |
| 171 | + assertThat(requestMap.get("model"), is("model")); |
| 172 | + assertThat(requestMap.get("n"), is(1)); |
| 173 | + assertThat(requestMap.get("stream"), is(false)); |
| 174 | + } |
| 175 | +} |
0 commit comments