|
| 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.integration; |
| 9 | + |
| 10 | +import org.elasticsearch.ResourceNotFoundException; |
| 11 | +import org.elasticsearch.action.support.PlainActionFuture; |
| 12 | +import org.elasticsearch.common.settings.Settings; |
| 13 | +import org.elasticsearch.core.TimeValue; |
| 14 | +import org.elasticsearch.inference.InferenceService; |
| 15 | +import org.elasticsearch.inference.MinimalServiceSettings; |
| 16 | +import org.elasticsearch.inference.Model; |
| 17 | +import org.elasticsearch.inference.TaskType; |
| 18 | +import org.elasticsearch.inference.UnparsedModel; |
| 19 | +import org.elasticsearch.plugins.Plugin; |
| 20 | +import org.elasticsearch.reindex.ReindexPlugin; |
| 21 | +import org.elasticsearch.test.ESSingleNodeTestCase; |
| 22 | +import org.elasticsearch.test.http.MockResponse; |
| 23 | +import org.elasticsearch.test.http.MockWebServer; |
| 24 | +import org.elasticsearch.threadpool.ThreadPool; |
| 25 | +import org.elasticsearch.xpack.inference.external.http.HttpClientManager; |
| 26 | +import org.elasticsearch.xpack.inference.external.http.sender.HttpRequestSenderTests; |
| 27 | +import org.elasticsearch.xpack.inference.logging.ThrottlerManager; |
| 28 | +import org.elasticsearch.xpack.inference.registry.ModelRegistry; |
| 29 | +import org.elasticsearch.xpack.inference.services.elastic.ElasticInferenceService; |
| 30 | +import org.elasticsearch.xpack.inference.services.elastic.ElasticInferenceServiceComponents; |
| 31 | +import org.elasticsearch.xpack.inference.services.elastic.authorization.ElasticInferenceServiceAuthorizationHandler; |
| 32 | +import org.junit.After; |
| 33 | +import org.junit.Before; |
| 34 | + |
| 35 | +import java.util.Collection; |
| 36 | +import java.util.EnumSet; |
| 37 | +import java.util.List; |
| 38 | +import java.util.concurrent.TimeUnit; |
| 39 | + |
| 40 | +import static org.elasticsearch.xpack.inference.Utils.inferenceUtilityPool; |
| 41 | +import static org.elasticsearch.xpack.inference.Utils.mockClusterServiceEmpty; |
| 42 | +import static org.elasticsearch.xpack.inference.external.http.Utils.getUrl; |
| 43 | +import static org.elasticsearch.xpack.inference.services.ServiceComponentsTests.createWithEmptySettings; |
| 44 | +import static org.hamcrest.CoreMatchers.is; |
| 45 | +import static org.mockito.Mockito.mock; |
| 46 | + |
| 47 | +public class InferenceRevokeDefaultEndpointsIT extends ESSingleNodeTestCase { |
| 48 | + private static final TimeValue TIMEOUT = new TimeValue(30, TimeUnit.SECONDS); |
| 49 | + |
| 50 | + private ModelRegistry modelRegistry; |
| 51 | + private final MockWebServer webServer = new MockWebServer(); |
| 52 | + private ThreadPool threadPool; |
| 53 | + |
| 54 | + @Before |
| 55 | + public void createComponents() throws Exception { |
| 56 | + threadPool = createThreadPool(inferenceUtilityPool()); |
| 57 | + webServer.start(); |
| 58 | + modelRegistry = new ModelRegistry(client()); |
| 59 | + } |
| 60 | + |
| 61 | + @After |
| 62 | + public void shutdown() { |
| 63 | + terminate(threadPool); |
| 64 | + webServer.close(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected Collection<Class<? extends Plugin>> getPlugins() { |
| 69 | + return pluginList(ReindexPlugin.class); |
| 70 | + } |
| 71 | + |
| 72 | + public void testDefaultConfigs_Returns_DefaultChatCompletion_V1_WhenTaskTypeIsCorrect() throws Exception { |
| 73 | + var clientManager = HttpClientManager.create(Settings.EMPTY, threadPool, mockClusterServiceEmpty(), mock(ThrottlerManager.class)); |
| 74 | + var senderFactory = HttpRequestSenderTests.createSenderFactory(threadPool, clientManager); |
| 75 | + var gatewayUrl = getUrl(webServer); |
| 76 | + |
| 77 | + String responseJson = """ |
| 78 | + { |
| 79 | + "models": [ |
| 80 | + { |
| 81 | + "model_name": "rainbow-sprinkles", |
| 82 | + "task_types": ["chat"] |
| 83 | + } |
| 84 | + ] |
| 85 | + } |
| 86 | + """; |
| 87 | + |
| 88 | + webServer.enqueue(new MockResponse().setResponseCode(200).setBody(responseJson)); |
| 89 | + |
| 90 | + try ( |
| 91 | + var service = new ElasticInferenceService( |
| 92 | + senderFactory, |
| 93 | + createWithEmptySettings(threadPool), |
| 94 | + new ElasticInferenceServiceComponents(gatewayUrl), |
| 95 | + modelRegistry, |
| 96 | + new ElasticInferenceServiceAuthorizationHandler(gatewayUrl, threadPool) |
| 97 | + ) |
| 98 | + ) { |
| 99 | + service.waitForAuthorizationToComplete(TIMEOUT); |
| 100 | + assertThat(service.supportedStreamingTasks(), is(EnumSet.of(TaskType.CHAT_COMPLETION, TaskType.ANY))); |
| 101 | + assertThat( |
| 102 | + service.defaultConfigIds(), |
| 103 | + is( |
| 104 | + List.of( |
| 105 | + new InferenceService.DefaultConfigId(".rainbow-sprinkles-elastic", MinimalServiceSettings.chatCompletion(), service) |
| 106 | + ) |
| 107 | + ) |
| 108 | + ); |
| 109 | + assertThat(service.supportedTaskTypes(), is(EnumSet.of(TaskType.CHAT_COMPLETION))); |
| 110 | + |
| 111 | + PlainActionFuture<List<Model>> listener = new PlainActionFuture<>(); |
| 112 | + service.defaultConfigs(listener); |
| 113 | + assertThat(listener.actionGet(TIMEOUT).get(0).getConfigurations().getInferenceEntityId(), is(".rainbow-sprinkles-elastic")); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + public void testRemoves_DefaultChatCompletion_V1_WhenAuthorizationReturnsEmpty() throws Exception { |
| 118 | + var gatewayUrl = getUrl(webServer); |
| 119 | + |
| 120 | + { |
| 121 | + var clientManager = HttpClientManager.create( |
| 122 | + Settings.EMPTY, |
| 123 | + threadPool, |
| 124 | + mockClusterServiceEmpty(), |
| 125 | + mock(ThrottlerManager.class) |
| 126 | + ); |
| 127 | + var senderFactory = HttpRequestSenderTests.createSenderFactory(threadPool, clientManager); |
| 128 | + |
| 129 | + String responseJson = """ |
| 130 | + { |
| 131 | + "models": [ |
| 132 | + { |
| 133 | + "model_name": "rainbow-sprinkles", |
| 134 | + "task_types": ["chat"] |
| 135 | + } |
| 136 | + ] |
| 137 | + } |
| 138 | + """; |
| 139 | + |
| 140 | + webServer.enqueue(new MockResponse().setResponseCode(200).setBody(responseJson)); |
| 141 | + |
| 142 | + try ( |
| 143 | + var service = new ElasticInferenceService( |
| 144 | + senderFactory, |
| 145 | + createWithEmptySettings(threadPool), |
| 146 | + new ElasticInferenceServiceComponents(gatewayUrl), |
| 147 | + modelRegistry, |
| 148 | + new ElasticInferenceServiceAuthorizationHandler(gatewayUrl, threadPool) |
| 149 | + ) |
| 150 | + ) { |
| 151 | + service.waitForAuthorizationToComplete(TIMEOUT); |
| 152 | + assertThat(service.supportedStreamingTasks(), is(EnumSet.of(TaskType.CHAT_COMPLETION, TaskType.ANY))); |
| 153 | + assertThat( |
| 154 | + service.defaultConfigIds(), |
| 155 | + is( |
| 156 | + List.of( |
| 157 | + new InferenceService.DefaultConfigId( |
| 158 | + ".rainbow-sprinkles-elastic", |
| 159 | + MinimalServiceSettings.chatCompletion(), |
| 160 | + service |
| 161 | + ) |
| 162 | + ) |
| 163 | + ) |
| 164 | + ); |
| 165 | + assertThat(service.supportedTaskTypes(), is(EnumSet.of(TaskType.CHAT_COMPLETION))); |
| 166 | + |
| 167 | + PlainActionFuture<List<Model>> listener = new PlainActionFuture<>(); |
| 168 | + service.defaultConfigs(listener); |
| 169 | + assertThat(listener.actionGet(TIMEOUT).get(0).getConfigurations().getInferenceEntityId(), is(".rainbow-sprinkles-elastic")); |
| 170 | + |
| 171 | + var getModelListener = new PlainActionFuture<UnparsedModel>(); |
| 172 | + // persists the default endpoints |
| 173 | + modelRegistry.getModel(".rainbow-sprinkles-elastic", getModelListener); |
| 174 | + |
| 175 | + var inferenceEntity = getModelListener.actionGet(TIMEOUT); |
| 176 | + assertThat(inferenceEntity.inferenceEntityId(), is(".rainbow-sprinkles-elastic")); |
| 177 | + assertThat(inferenceEntity.taskType(), is(TaskType.CHAT_COMPLETION)); |
| 178 | + } |
| 179 | + } |
| 180 | + { |
| 181 | + String noAuthorizationResponseJson = """ |
| 182 | + { |
| 183 | + "models": [] |
| 184 | + } |
| 185 | + """; |
| 186 | + |
| 187 | + webServer.enqueue(new MockResponse().setResponseCode(200).setBody(noAuthorizationResponseJson)); |
| 188 | + |
| 189 | + var httpManager = HttpClientManager.create(Settings.EMPTY, threadPool, mockClusterServiceEmpty(), mock(ThrottlerManager.class)); |
| 190 | + var senderFactory = HttpRequestSenderTests.createSenderFactory(threadPool, httpManager); |
| 191 | + |
| 192 | + try ( |
| 193 | + var service = new ElasticInferenceService( |
| 194 | + senderFactory, |
| 195 | + createWithEmptySettings(threadPool), |
| 196 | + new ElasticInferenceServiceComponents(gatewayUrl), |
| 197 | + modelRegistry, |
| 198 | + new ElasticInferenceServiceAuthorizationHandler(gatewayUrl, threadPool) |
| 199 | + ) |
| 200 | + ) { |
| 201 | + service.waitForAuthorizationToComplete(TIMEOUT); |
| 202 | + assertThat(service.supportedStreamingTasks(), is(EnumSet.noneOf(TaskType.class))); |
| 203 | + assertTrue(service.defaultConfigIds().isEmpty()); |
| 204 | + assertThat(service.supportedTaskTypes(), is(EnumSet.noneOf(TaskType.class))); |
| 205 | + |
| 206 | + var getModelListener = new PlainActionFuture<UnparsedModel>(); |
| 207 | + modelRegistry.getModel(".rainbow-sprinkles-elastic", getModelListener); |
| 208 | + |
| 209 | + var exception = expectThrows(ResourceNotFoundException.class, () -> getModelListener.actionGet(TIMEOUT)); |
| 210 | + assertThat(exception.getMessage(), is("Inference endpoint not found [.rainbow-sprinkles-elastic]")); |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | +} |
0 commit comments