瀏覽代碼

[Inference API] Remove GoogleAiStudioSecretSettings and use DefaultSecretSettings (#109296)

Tim Grein 1 年之前
父節點
當前提交
ac0fa4d4d6

+ 2 - 2
x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/external/request/googleaistudio/GoogleAiStudioRequest.java

@@ -11,13 +11,13 @@ import org.apache.http.client.methods.HttpPost;
 import org.apache.http.client.utils.URIBuilder;
 import org.elasticsearch.common.ValidationException;
 import org.elasticsearch.xpack.inference.external.request.Request;
-import org.elasticsearch.xpack.inference.services.googleaistudio.GoogleAiStudioSecretSettings;
+import org.elasticsearch.xpack.inference.services.settings.DefaultSecretSettings;
 
 public interface GoogleAiStudioRequest extends Request {
 
     String API_KEY_PARAMETER = "key";
 
-    static void decorateWithApiKeyParameter(HttpPost httpPost, GoogleAiStudioSecretSettings secretSettings) {
+    static void decorateWithApiKeyParameter(HttpPost httpPost, DefaultSecretSettings secretSettings) {
         try {
             var uri = httpPost.getURI();
             var uriWithApiKey = new URIBuilder().setScheme(uri.getScheme())

+ 0 - 106
x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/googleaistudio/GoogleAiStudioSecretSettings.java

@@ -1,106 +0,0 @@
-/*
- * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
- * or more contributor license agreements. Licensed under the Elastic License
- * 2.0; you may not use this file except in compliance with the Elastic License
- * 2.0.
- */
-
-package org.elasticsearch.xpack.inference.services.googleaistudio;
-
-import org.elasticsearch.TransportVersion;
-import org.elasticsearch.TransportVersions;
-import org.elasticsearch.common.ValidationException;
-import org.elasticsearch.common.io.stream.StreamInput;
-import org.elasticsearch.common.io.stream.StreamOutput;
-import org.elasticsearch.common.settings.SecureString;
-import org.elasticsearch.core.Nullable;
-import org.elasticsearch.inference.ModelSecrets;
-import org.elasticsearch.inference.SecretSettings;
-import org.elasticsearch.xcontent.XContentBuilder;
-
-import java.io.IOException;
-import java.util.Map;
-import java.util.Objects;
-
-import static org.elasticsearch.core.Strings.format;
-import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractOptionalSecureString;
-
-public class GoogleAiStudioSecretSettings implements SecretSettings {
-
-    public static final String NAME = "google_ai_studio_secret_settings";
-    public static final String API_KEY = "api_key";
-
-    private final SecureString apiKey;
-
-    public static GoogleAiStudioSecretSettings fromMap(@Nullable Map<String, Object> map) {
-        if (map == null) {
-            return null;
-        }
-
-        ValidationException validationException = new ValidationException();
-        SecureString secureApiKey = extractOptionalSecureString(map, API_KEY, ModelSecrets.SECRET_SETTINGS, validationException);
-
-        if (secureApiKey == null) {
-            validationException.addValidationError(format("[secret_settings] must have [%s] set", API_KEY));
-        }
-
-        if (validationException.validationErrors().isEmpty() == false) {
-            throw validationException;
-        }
-
-        return new GoogleAiStudioSecretSettings(secureApiKey);
-    }
-
-    public GoogleAiStudioSecretSettings(SecureString apiKey) {
-        Objects.requireNonNull(apiKey);
-        this.apiKey = apiKey;
-    }
-
-    public GoogleAiStudioSecretSettings(StreamInput in) throws IOException {
-        this(in.readOptionalSecureString());
-    }
-
-    public SecureString apiKey() {
-        return apiKey;
-    }
-
-    @Override
-    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
-        builder.startObject();
-
-        if (apiKey != null) {
-            builder.field(API_KEY, apiKey.toString());
-        }
-
-        builder.endObject();
-        return builder;
-    }
-
-    @Override
-    public String getWriteableName() {
-        return NAME;
-    }
-
-    @Override
-    public TransportVersion getMinimalSupportedVersion() {
-        return TransportVersions.ML_INFERENCE_GOOGLE_AI_STUDIO_COMPLETION_ADDED;
-    }
-
-    @Override
-    public void writeTo(StreamOutput out) throws IOException {
-        out.writeOptionalSecureString(apiKey);
-    }
-
-    @Override
-    public boolean equals(Object object) {
-        if (this == object) return true;
-        if (object == null || getClass() != object.getClass()) return false;
-        GoogleAiStudioSecretSettings that = (GoogleAiStudioSecretSettings) object;
-        return Objects.equals(apiKey, that.apiKey);
-    }
-
-    @Override
-    public int hashCode() {
-        return Objects.hash(apiKey);
-    }
-}

+ 6 - 6
x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/googleaistudio/completion/GoogleAiStudioCompletionModel.java

@@ -19,7 +19,7 @@ import org.elasticsearch.xpack.inference.external.action.ExecutableAction;
 import org.elasticsearch.xpack.inference.external.action.googleaistudio.GoogleAiStudioActionVisitor;
 import org.elasticsearch.xpack.inference.external.request.googleaistudio.GoogleAiStudioUtils;
 import org.elasticsearch.xpack.inference.services.googleaistudio.GoogleAiStudioModel;
-import org.elasticsearch.xpack.inference.services.googleaistudio.GoogleAiStudioSecretSettings;
+import org.elasticsearch.xpack.inference.services.settings.DefaultSecretSettings;
 
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -45,7 +45,7 @@ public class GoogleAiStudioCompletionModel extends GoogleAiStudioModel {
             service,
             GoogleAiStudioCompletionServiceSettings.fromMap(serviceSettings),
             EmptyTaskSettings.INSTANCE,
-            GoogleAiStudioSecretSettings.fromMap(secrets)
+            DefaultSecretSettings.fromMap(secrets)
         );
     }
 
@@ -56,7 +56,7 @@ public class GoogleAiStudioCompletionModel extends GoogleAiStudioModel {
         String service,
         GoogleAiStudioCompletionServiceSettings serviceSettings,
         TaskSettings taskSettings,
-        @Nullable GoogleAiStudioSecretSettings secrets
+        @Nullable DefaultSecretSettings secrets
     ) {
         super(
             new ModelConfigurations(inferenceEntityId, taskType, service, serviceSettings, taskSettings),
@@ -78,7 +78,7 @@ public class GoogleAiStudioCompletionModel extends GoogleAiStudioModel {
         String url,
         GoogleAiStudioCompletionServiceSettings serviceSettings,
         TaskSettings taskSettings,
-        @Nullable GoogleAiStudioSecretSettings secrets
+        @Nullable DefaultSecretSettings secrets
     ) {
         super(
             new ModelConfigurations(inferenceEntityId, taskType, service, serviceSettings, taskSettings),
@@ -102,8 +102,8 @@ public class GoogleAiStudioCompletionModel extends GoogleAiStudioModel {
     }
 
     @Override
-    public GoogleAiStudioSecretSettings getSecretSettings() {
-        return (GoogleAiStudioSecretSettings) super.getSecretSettings();
+    public DefaultSecretSettings getSecretSettings() {
+        return (DefaultSecretSettings) super.getSecretSettings();
     }
 
     public static URI buildUri(String model) throws URISyntaxException {

+ 6 - 6
x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/googleaistudio/embeddings/GoogleAiStudioEmbeddingsModel.java

@@ -19,7 +19,7 @@ import org.elasticsearch.xpack.inference.external.action.ExecutableAction;
 import org.elasticsearch.xpack.inference.external.action.googleaistudio.GoogleAiStudioActionVisitor;
 import org.elasticsearch.xpack.inference.external.request.googleaistudio.GoogleAiStudioUtils;
 import org.elasticsearch.xpack.inference.services.googleaistudio.GoogleAiStudioModel;
-import org.elasticsearch.xpack.inference.services.googleaistudio.GoogleAiStudioSecretSettings;
+import org.elasticsearch.xpack.inference.services.settings.DefaultSecretSettings;
 
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -45,7 +45,7 @@ public class GoogleAiStudioEmbeddingsModel extends GoogleAiStudioModel {
             service,
             GoogleAiStudioEmbeddingsServiceSettings.fromMap(serviceSettings),
             EmptyTaskSettings.INSTANCE,
-            GoogleAiStudioSecretSettings.fromMap(secrets)
+            DefaultSecretSettings.fromMap(secrets)
         );
     }
 
@@ -60,7 +60,7 @@ public class GoogleAiStudioEmbeddingsModel extends GoogleAiStudioModel {
         String service,
         GoogleAiStudioEmbeddingsServiceSettings serviceSettings,
         TaskSettings taskSettings,
-        @Nullable GoogleAiStudioSecretSettings secrets
+        @Nullable DefaultSecretSettings secrets
     ) {
         super(
             new ModelConfigurations(inferenceEntityId, taskType, service, serviceSettings, taskSettings),
@@ -82,7 +82,7 @@ public class GoogleAiStudioEmbeddingsModel extends GoogleAiStudioModel {
         String uri,
         GoogleAiStudioEmbeddingsServiceSettings serviceSettings,
         TaskSettings taskSettings,
-        @Nullable GoogleAiStudioSecretSettings secrets
+        @Nullable DefaultSecretSettings secrets
     ) {
         super(
             new ModelConfigurations(inferenceEntityId, taskType, service, serviceSettings, taskSettings),
@@ -102,8 +102,8 @@ public class GoogleAiStudioEmbeddingsModel extends GoogleAiStudioModel {
     }
 
     @Override
-    public GoogleAiStudioSecretSettings getSecretSettings() {
-        return (GoogleAiStudioSecretSettings) super.getSecretSettings();
+    public DefaultSecretSettings getSecretSettings() {
+        return (DefaultSecretSettings) super.getSecretSettings();
     }
 
     public URI uri() {

+ 3 - 3
x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/external/request/googleaistudio/GoogleAiStudioRequestTests.java

@@ -12,7 +12,7 @@ import org.elasticsearch.common.ValidationException;
 import org.elasticsearch.common.settings.SecureString;
 import org.elasticsearch.core.Strings;
 import org.elasticsearch.test.ESTestCase;
-import org.elasticsearch.xpack.inference.services.googleaistudio.GoogleAiStudioSecretSettings;
+import org.elasticsearch.xpack.inference.services.settings.DefaultSecretSettings;
 
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -28,7 +28,7 @@ public class GoogleAiStudioRequestTests extends ESTestCase {
         var uriString = "https://localhost:3000";
         var secureApiKey = new SecureString("api_key".toCharArray());
         var httpPost = new HttpPost(uriString);
-        var secretSettings = new GoogleAiStudioSecretSettings(secureApiKey);
+        var secretSettings = new DefaultSecretSettings(secureApiKey);
 
         GoogleAiStudioRequest.decorateWithApiKeyParameter(httpPost, secretSettings);
 
@@ -45,7 +45,7 @@ public class GoogleAiStudioRequestTests extends ESTestCase {
             ValidationException.class,
             () -> GoogleAiStudioRequest.decorateWithApiKeyParameter(
                 httpPost,
-                new GoogleAiStudioSecretSettings(new SecureString("abc".toCharArray()))
+                new DefaultSecretSettings(new SecureString("abc".toCharArray()))
             )
         );
         assertThat(validationException.getCause(), is(cause));

+ 0 - 71
x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/services/googleaistudio/GoogleAiStudioSecretSettingsTests.java

@@ -1,71 +0,0 @@
-/*
- * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
- * or more contributor license agreements. Licensed under the Elastic License
- * 2.0; you may not use this file except in compliance with the Elastic License
- * 2.0.
- */
-
-package org.elasticsearch.xpack.inference.services.googleaistudio;
-
-import org.elasticsearch.common.ValidationException;
-import org.elasticsearch.common.io.stream.Writeable;
-import org.elasticsearch.common.settings.SecureString;
-import org.elasticsearch.test.AbstractWireSerializingTestCase;
-
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-
-import static org.hamcrest.Matchers.containsString;
-import static org.hamcrest.Matchers.is;
-
-public class GoogleAiStudioSecretSettingsTests extends AbstractWireSerializingTestCase<GoogleAiStudioSecretSettings> {
-
-    public static GoogleAiStudioSecretSettings createRandom() {
-        return new GoogleAiStudioSecretSettings(randomSecureStringOfLength(15));
-    }
-
-    public void testFromMap() {
-        var apiKey = "abc";
-        var secretSettings = GoogleAiStudioSecretSettings.fromMap(new HashMap<>(Map.of(GoogleAiStudioSecretSettings.API_KEY, apiKey)));
-
-        assertThat(new GoogleAiStudioSecretSettings(new SecureString(apiKey.toCharArray())), is(secretSettings));
-    }
-
-    public void testFromMap_ReturnsNull_WhenMapIsNull() {
-        assertNull(GoogleAiStudioSecretSettings.fromMap(null));
-    }
-
-    public void testFromMap_ThrowsError_WhenApiKeyIsNull() {
-        var throwException = expectThrows(ValidationException.class, () -> GoogleAiStudioSecretSettings.fromMap(new HashMap<>()));
-
-        assertThat(throwException.getMessage(), containsString("[secret_settings] must have [api_key] set"));
-    }
-
-    public void testFromMap_ThrowsError_WhenApiKeyIsEmpty() {
-        var thrownException = expectThrows(
-            ValidationException.class,
-            () -> GoogleAiStudioSecretSettings.fromMap(new HashMap<>(Map.of(GoogleAiStudioSecretSettings.API_KEY, "")))
-        );
-
-        assertThat(
-            thrownException.getMessage(),
-            containsString("[secret_settings] Invalid value empty string. [api_key] must be a non-empty string")
-        );
-    }
-
-    @Override
-    protected Writeable.Reader<GoogleAiStudioSecretSettings> instanceReader() {
-        return GoogleAiStudioSecretSettings::new;
-    }
-
-    @Override
-    protected GoogleAiStudioSecretSettings createTestInstance() {
-        return createRandom();
-    }
-
-    @Override
-    protected GoogleAiStudioSecretSettings mutateInstance(GoogleAiStudioSecretSettings instance) throws IOException {
-        return randomValueOtherThan(instance, GoogleAiStudioSecretSettingsTests::createRandom);
-    }
-}

+ 3 - 3
x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/services/googleaistudio/completion/GoogleAiStudioCompletionModelTests.java

@@ -11,7 +11,7 @@ import org.elasticsearch.common.settings.SecureString;
 import org.elasticsearch.inference.EmptyTaskSettings;
 import org.elasticsearch.inference.TaskType;
 import org.elasticsearch.test.ESTestCase;
-import org.elasticsearch.xpack.inference.services.googleaistudio.GoogleAiStudioSecretSettings;
+import org.elasticsearch.xpack.inference.services.settings.DefaultSecretSettings;
 
 import java.net.URISyntaxException;
 import java.util.HashMap;
@@ -48,7 +48,7 @@ public class GoogleAiStudioCompletionModelTests extends ESTestCase {
             "service",
             new GoogleAiStudioCompletionServiceSettings(model, null),
             EmptyTaskSettings.INSTANCE,
-            new GoogleAiStudioSecretSettings(new SecureString(apiKey.toCharArray()))
+            new DefaultSecretSettings(new SecureString(apiKey.toCharArray()))
         );
     }
 
@@ -60,7 +60,7 @@ public class GoogleAiStudioCompletionModelTests extends ESTestCase {
             url,
             new GoogleAiStudioCompletionServiceSettings(model, null),
             EmptyTaskSettings.INSTANCE,
-            new GoogleAiStudioSecretSettings(new SecureString(apiKey.toCharArray()))
+            new DefaultSecretSettings(new SecureString(apiKey.toCharArray()))
         );
     }
 }

+ 4 - 4
x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/services/googleaistudio/embeddings/GoogleAiStudioEmbeddingsModelTests.java

@@ -13,7 +13,7 @@ import org.elasticsearch.inference.EmptyTaskSettings;
 import org.elasticsearch.inference.SimilarityMeasure;
 import org.elasticsearch.inference.TaskType;
 import org.elasticsearch.test.ESTestCase;
-import org.elasticsearch.xpack.inference.services.googleaistudio.GoogleAiStudioSecretSettings;
+import org.elasticsearch.xpack.inference.services.settings.DefaultSecretSettings;
 
 public class GoogleAiStudioEmbeddingsModelTests extends ESTestCase {
 
@@ -25,7 +25,7 @@ public class GoogleAiStudioEmbeddingsModelTests extends ESTestCase {
             url,
             new GoogleAiStudioEmbeddingsServiceSettings(model, null, null, SimilarityMeasure.DOT_PRODUCT, null),
             EmptyTaskSettings.INSTANCE,
-            new GoogleAiStudioSecretSettings(new SecureString(apiKey.toCharArray()))
+            new DefaultSecretSettings(new SecureString(apiKey.toCharArray()))
         );
     }
 
@@ -43,7 +43,7 @@ public class GoogleAiStudioEmbeddingsModelTests extends ESTestCase {
             url,
             new GoogleAiStudioEmbeddingsServiceSettings(model, null, dimensions, similarityMeasure, null),
             EmptyTaskSettings.INSTANCE,
-            new GoogleAiStudioSecretSettings(new SecureString(apiKey.toCharArray()))
+            new DefaultSecretSettings(new SecureString(apiKey.toCharArray()))
         );
     }
 
@@ -59,7 +59,7 @@ public class GoogleAiStudioEmbeddingsModelTests extends ESTestCase {
             "service",
             new GoogleAiStudioEmbeddingsServiceSettings(model, tokenLimit, dimensions, SimilarityMeasure.DOT_PRODUCT, null),
             EmptyTaskSettings.INSTANCE,
-            new GoogleAiStudioSecretSettings(new SecureString(apiKey.toCharArray()))
+            new DefaultSecretSettings(new SecureString(apiKey.toCharArray()))
         );
     }