|
| 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; |
| 9 | + |
| 10 | +import org.elasticsearch.inference.TaskType; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +public class MockRerankInferenceServiceIT extends InferenceBaseRestTest { |
| 17 | + |
| 18 | + @SuppressWarnings("unchecked") |
| 19 | + public void testMockService() throws IOException { |
| 20 | + String inferenceEntityId = "test-mock"; |
| 21 | + var putModel = putModel(inferenceEntityId, mockRerankServiceModelConfig(), TaskType.RERANK); |
| 22 | + var model = getModels(inferenceEntityId, TaskType.RERANK).get(0); |
| 23 | + |
| 24 | + for (var modelMap : List.of(putModel, model)) { |
| 25 | + assertEquals(inferenceEntityId, modelMap.get("inference_id")); |
| 26 | + assertEquals(TaskType.RERANK, TaskType.fromString((String) modelMap.get("task_type"))); |
| 27 | + assertEquals("test_reranking_service", modelMap.get("service")); |
| 28 | + } |
| 29 | + |
| 30 | + List<String> input = List.of(randomAlphaOfLength(10)); |
| 31 | + var inference = infer(inferenceEntityId, input); |
| 32 | + assertNonEmptyInferenceResults(inference, 1, TaskType.RERANK); |
| 33 | + assertEquals(inference, infer(inferenceEntityId, input)); |
| 34 | + assertNotEquals(inference, infer(inferenceEntityId, randomValueOtherThan(input, () -> List.of(randomAlphaOfLength(10))))); |
| 35 | + } |
| 36 | + |
| 37 | + public void testMockServiceWithMultipleInputs() throws IOException { |
| 38 | + String inferenceEntityId = "test-mock-with-multi-inputs"; |
| 39 | + putModel(inferenceEntityId, mockRerankServiceModelConfig(), TaskType.RERANK); |
| 40 | + var queryParams = Map.of("timeout", "120s"); |
| 41 | + |
| 42 | + var inference = infer( |
| 43 | + inferenceEntityId, |
| 44 | + TaskType.RERANK, |
| 45 | + List.of(randomAlphaOfLength(5), randomAlphaOfLength(10)), |
| 46 | + "What if?", |
| 47 | + queryParams |
| 48 | + ); |
| 49 | + |
| 50 | + assertNonEmptyInferenceResults(inference, 2, TaskType.RERANK); |
| 51 | + } |
| 52 | + |
| 53 | + @SuppressWarnings("unchecked") |
| 54 | + public void testMockService_DoesNotReturnSecretsInGetResponse() throws IOException { |
| 55 | + String inferenceEntityId = "test-mock"; |
| 56 | + var putModel = putModel(inferenceEntityId, mockRerankServiceModelConfig(), TaskType.RERANK); |
| 57 | + var model = getModels(inferenceEntityId, TaskType.RERANK).get(0); |
| 58 | + |
| 59 | + var serviceSettings = (Map<String, Object>) model.get("service_settings"); |
| 60 | + assertNull(serviceSettings.get("api_key")); |
| 61 | + assertNotNull(serviceSettings.get("model_id")); |
| 62 | + |
| 63 | + var putServiceSettings = (Map<String, Object>) putModel.get("service_settings"); |
| 64 | + assertNull(putServiceSettings.get("api_key")); |
| 65 | + assertNotNull(putServiceSettings.get("model_id")); |
| 66 | + } |
| 67 | +} |
0 commit comments