|
| 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.voyageai; |
| 9 | + |
| 10 | +import org.elasticsearch.common.Strings; |
| 11 | +import org.elasticsearch.common.ValidationException; |
| 12 | +import org.elasticsearch.common.io.stream.Writeable; |
| 13 | +import org.elasticsearch.core.Nullable; |
| 14 | +import org.elasticsearch.test.AbstractWireSerializingTestCase; |
| 15 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 16 | +import org.elasticsearch.xcontent.XContentFactory; |
| 17 | +import org.elasticsearch.xcontent.XContentType; |
| 18 | +import org.elasticsearch.xpack.inference.services.ConfigurationParseContext; |
| 19 | +import org.elasticsearch.xpack.inference.services.ServiceFields; |
| 20 | +import org.elasticsearch.xpack.inference.services.ServiceUtils; |
| 21 | +import org.elasticsearch.xpack.inference.services.voyageai.VoyageAIServiceSettings; |
| 22 | +import org.elasticsearch.xpack.inference.services.settings.RateLimitSettings; |
| 23 | +import org.elasticsearch.xpack.inference.services.settings.RateLimitSettingsTests; |
| 24 | +import org.hamcrest.MatcherAssert; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +import static org.hamcrest.Matchers.containsString; |
| 31 | +import static org.hamcrest.Matchers.is; |
| 32 | + |
| 33 | +public class VoyageAIServiceSettingsTests extends AbstractWireSerializingTestCase<VoyageAIServiceSettings> { |
| 34 | + |
| 35 | + public static VoyageAIServiceSettings createRandomWithNonNullUrl() { |
| 36 | + return createRandom(randomAlphaOfLength(15)); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * The created settings can have a url set to null. |
| 41 | + */ |
| 42 | + public static VoyageAIServiceSettings createRandom() { |
| 43 | + var url = randomBoolean() ? randomAlphaOfLength(15) : null; |
| 44 | + return createRandom(url); |
| 45 | + } |
| 46 | + |
| 47 | + private static VoyageAIServiceSettings createRandom(String url) { |
| 48 | + var model = randomAlphaOfLength(15); |
| 49 | + |
| 50 | + return new VoyageAIServiceSettings(ServiceUtils.createOptionalUri(url), model, RateLimitSettingsTests.createRandom()); |
| 51 | + } |
| 52 | + |
| 53 | + public void testFromMap() { |
| 54 | + var url = "https://www.abc.com"; |
| 55 | + var model = "model"; |
| 56 | + var serviceSettings = VoyageAIServiceSettings.fromMap( |
| 57 | + new HashMap<>(Map.of(ServiceFields.URL, url, VoyageAIServiceSettings.MODEL_ID, model)), |
| 58 | + ConfigurationParseContext.REQUEST |
| 59 | + ); |
| 60 | + |
| 61 | + MatcherAssert.assertThat(serviceSettings, is(new VoyageAIServiceSettings(ServiceUtils.createUri(url), model, null))); |
| 62 | + } |
| 63 | + |
| 64 | + public void testFromMap_WithRateLimit() { |
| 65 | + var url = "https://www.abc.com"; |
| 66 | + var model = "model"; |
| 67 | + var serviceSettings = VoyageAIServiceSettings.fromMap( |
| 68 | + new HashMap<>( |
| 69 | + Map.of( |
| 70 | + ServiceFields.URL, |
| 71 | + url, |
| 72 | + VoyageAIServiceSettings.MODEL_ID, |
| 73 | + model, |
| 74 | + RateLimitSettings.FIELD_NAME, |
| 75 | + new HashMap<>(Map.of(RateLimitSettings.REQUESTS_PER_MINUTE_FIELD, 3)) |
| 76 | + ) |
| 77 | + ), |
| 78 | + ConfigurationParseContext.REQUEST |
| 79 | + ); |
| 80 | + |
| 81 | + MatcherAssert.assertThat( |
| 82 | + serviceSettings, |
| 83 | + is(new VoyageAIServiceSettings(ServiceUtils.createUri(url), model, new RateLimitSettings(3))) |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + public void testFromMap_WhenUsingModelId() { |
| 88 | + var url = "https://www.abc.com"; |
| 89 | + var model = "model"; |
| 90 | + var serviceSettings = VoyageAIServiceSettings.fromMap( |
| 91 | + new HashMap<>(Map.of(ServiceFields.URL, url, VoyageAIServiceSettings.MODEL_ID, model)), |
| 92 | + ConfigurationParseContext.PERSISTENT |
| 93 | + ); |
| 94 | + |
| 95 | + MatcherAssert.assertThat(serviceSettings, is(new VoyageAIServiceSettings(ServiceUtils.createUri(url), model, null))); |
| 96 | + } |
| 97 | + |
| 98 | + public void testFromMap_MissingUrl_DoesNotThrowException() { |
| 99 | + var serviceSettings = VoyageAIServiceSettings.fromMap( |
| 100 | + new HashMap<>(Map.of(VoyageAIServiceSettings.MODEL_ID, "model")), |
| 101 | + ConfigurationParseContext.PERSISTENT |
| 102 | + ); |
| 103 | + assertNull(serviceSettings.uri()); |
| 104 | + } |
| 105 | + |
| 106 | + public void testFromMap_EmptyUrl_ThrowsError() { |
| 107 | + var thrownException = expectThrows( |
| 108 | + ValidationException.class, |
| 109 | + () -> VoyageAIServiceSettings.fromMap(new HashMap<>(Map.of(ServiceFields.URL, "")), ConfigurationParseContext.PERSISTENT) |
| 110 | + ); |
| 111 | + |
| 112 | + MatcherAssert.assertThat( |
| 113 | + thrownException.getMessage(), |
| 114 | + containsString( |
| 115 | + Strings.format( |
| 116 | + "Validation Failed: 1: [service_settings] Invalid value empty string. [%s] must be a non-empty string;", |
| 117 | + ServiceFields.URL |
| 118 | + ) |
| 119 | + ) |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + public void testFromMap_InvalidUrl_ThrowsError() { |
| 124 | + var url = "https://www.abc^.com"; |
| 125 | + var thrownException = expectThrows( |
| 126 | + ValidationException.class, |
| 127 | + () -> VoyageAIServiceSettings.fromMap(new HashMap<>(Map.of(ServiceFields.URL, url)), ConfigurationParseContext.PERSISTENT) |
| 128 | + ); |
| 129 | + |
| 130 | + MatcherAssert.assertThat( |
| 131 | + thrownException.getMessage(), |
| 132 | + containsString( |
| 133 | + Strings.format("Validation Failed: 1: [service_settings] Invalid url [%s] received for field [%s]", url, ServiceFields.URL) |
| 134 | + ) |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + public void testXContent_WritesModelId() throws IOException { |
| 139 | + var entity = new VoyageAIServiceSettings((String) null, "model", new RateLimitSettings(1)); |
| 140 | + |
| 141 | + XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); |
| 142 | + entity.toXContent(builder, null); |
| 143 | + String xContentResult = Strings.toString(builder); |
| 144 | + |
| 145 | + assertThat(xContentResult, is(""" |
| 146 | + {"model_id":"model","rate_limit":{"requests_per_minute":1}}""")); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + protected Writeable.Reader<VoyageAIServiceSettings> instanceReader() { |
| 151 | + return VoyageAIServiceSettings::new; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + protected VoyageAIServiceSettings createTestInstance() { |
| 156 | + return createRandomWithNonNullUrl(); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + protected VoyageAIServiceSettings mutateInstance(VoyageAIServiceSettings instance) throws IOException { |
| 161 | + return randomValueOtherThan(instance, VoyageAIServiceSettingsTests::createRandom); |
| 162 | + } |
| 163 | + |
| 164 | + public static Map<String, Object> getServiceSettingsMap(@Nullable String url, String model) { |
| 165 | + var map = new HashMap<String, Object>(); |
| 166 | + |
| 167 | + if (url != null) { |
| 168 | + map.put(ServiceFields.URL, url); |
| 169 | + } |
| 170 | + |
| 171 | + map.put(VoyageAIServiceSettings.MODEL_ID, model); |
| 172 | + |
| 173 | + return map; |
| 174 | + } |
| 175 | +} |
0 commit comments