avichaym commented on code in PR #533:
URL: https://github.com/apache/flink-agents/pull/533#discussion_r2872675407


##########
integrations/vector-stores/opensearch/src/main/java/org/apache/flink/agents/integrations/vectorstores/opensearch/OpenSearchVectorStore.java:
##########
@@ -0,0 +1,568 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.agents.integrations.vectorstores.opensearch;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.flink.agents.api.embedding.model.BaseEmbeddingModelSetup;
+import org.apache.flink.agents.api.resource.Resource;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.resource.ResourceType;
+import org.apache.flink.agents.api.vectorstores.BaseVectorStore;
+import 
org.apache.flink.agents.api.vectorstores.CollectionManageableVectorStore;
+import org.apache.flink.agents.api.vectorstores.Document;
+import software.amazon.awssdk.auth.credentials.AwsCredentials;
+import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
+import software.amazon.awssdk.auth.signer.Aws4Signer;
+import software.amazon.awssdk.auth.signer.params.Aws4SignerParams;
+import software.amazon.awssdk.http.HttpExecuteRequest;
+import software.amazon.awssdk.http.HttpExecuteResponse;
+import software.amazon.awssdk.http.SdkHttpClient;
+import software.amazon.awssdk.http.SdkHttpFullRequest;
+import software.amazon.awssdk.http.SdkHttpMethod;
+import software.amazon.awssdk.http.apache.ApacheHttpClient;
+import software.amazon.awssdk.regions.Region;
+
+import javax.annotation.Nullable;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Base64;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Objects;
+import java.util.UUID;
+import java.util.function.BiFunction;
+
+/**
+ * OpenSearch vector store supporting both OpenSearch Serverless (AOSS) and 
OpenSearch Service
+ * domains, with IAM (SigV4) or basic auth.
+ *
+ * <p>Implements {@link CollectionManageableVectorStore} for Long-Term Memory 
support. Collections
+ * map to OpenSearch indices. Collection metadata is stored in a dedicated 
{@code
+ * flink_agents_collection_metadata} index.
+ *
+ * <p>Supported parameters:
+ *
+ * <ul>
+ *   <li><b>embedding_model</b> (required): name of the embedding model 
resource
+ *   <li><b>endpoint</b> (required): OpenSearch endpoint URL
+ *   <li><b>index</b> (required): default index name
+ *   <li><b>service_type</b> (optional): "serverless" (default) or "domain"
+ *   <li><b>auth</b> (optional): "iam" (default) or "basic"
+ *   <li><b>username</b> (required if auth=basic): basic auth username
+ *   <li><b>password</b> (required if auth=basic): basic auth password
+ *   <li><b>vector_field</b> (optional): vector field name (default: 
"embedding")
+ *   <li><b>content_field</b> (optional): content field name (default: 
"content")
+ *   <li><b>region</b> (optional): AWS region (default: us-east-1)
+ *   <li><b>dims</b> (optional): vector dimensions for index creation 
(default: 1024)
+ *   <li><b>max_bulk_mb</b> (optional): max bulk payload size in MB (default: 
5)
+ * </ul>
+ *
+ * <p>Example usage:
+ *
+ * <pre>{@code
+ * @VectorStore
+ * public static ResourceDescriptor opensearchStore() {
+ *     return 
ResourceDescriptor.Builder.newBuilder(OpenSearchVectorStore.class.getName())
+ *             .addInitialArgument("embedding_model", "bedrockEmbeddingSetup")
+ *             .addInitialArgument("endpoint", 
"https://my-domain.us-east-1.es.amazonaws.com";)
+ *             .addInitialArgument("index", "my-vectors")
+ *             .addInitialArgument("service_type", "domain")
+ *             .addInitialArgument("auth", "iam")
+ *             .addInitialArgument("dims", 1024)
+ *             .build();
+ * }
+ * }</pre>
+ */
+public class OpenSearchVectorStore extends BaseVectorStore
+        implements CollectionManageableVectorStore {
+
+    private static final ObjectMapper MAPPER = new ObjectMapper();
+    private static final String METADATA_INDEX = 
"flink_agents_collection_metadata";
+
+    private final String endpoint;
+    private final String index;
+    private final String vectorField;
+    private final String contentField;
+    private final int dims;
+    private final Region region;
+    private final boolean serverless;
+    private final boolean useIamAuth;
+    private final String basicAuthHeader;
+    private final int maxBulkBytes;
+
+    private final SdkHttpClient httpClient;
+    // TODO: Aws4Signer is legacy; migrate to AwsV4HttpSigner from 
http-auth-aws in a follow-up.
+    private final Aws4Signer signer;
+    private final DefaultCredentialsProvider credentialsProvider;
+
+    public OpenSearchVectorStore(
+            ResourceDescriptor descriptor, BiFunction<String, ResourceType, 
Resource> getResource) {
+        super(descriptor, getResource);
+
+        this.endpoint = descriptor.getArgument("endpoint");
+        if (this.endpoint == null || this.endpoint.isBlank()) {
+            throw new IllegalArgumentException("endpoint is required for 
OpenSearchVectorStore");
+        }
+
+        this.index = descriptor.getArgument("index");
+        if (this.index == null || this.index.isBlank()) {
+            throw new IllegalArgumentException("index is required for 
OpenSearchVectorStore");
+        }
+
+        this.vectorField =
+                
Objects.requireNonNullElse(descriptor.getArgument("vector_field"), "embedding");
+        this.contentField =
+                
Objects.requireNonNullElse(descriptor.getArgument("content_field"), "content");
+        Integer dimsArg = descriptor.getArgument("dims");
+        this.dims = dimsArg != null ? dimsArg : 1024;
+
+        String regionStr = descriptor.getArgument("region");
+        this.region = Region.of(regionStr != null ? regionStr : "us-east-1");
+
+        String serviceType =
+                
Objects.requireNonNullElse(descriptor.getArgument("service_type"), 
"serverless");
+        this.serverless = serviceType.equalsIgnoreCase("serverless");
+
+        String auth = 
Objects.requireNonNullElse(descriptor.getArgument("auth"), "iam");
+        this.useIamAuth = auth.equalsIgnoreCase("iam");
+
+        if (!useIamAuth) {
+            String username = descriptor.getArgument("username");
+            String password = descriptor.getArgument("password");
+            if (username == null || password == null) {
+                throw new IllegalArgumentException("username and password 
required for basic auth");
+            }
+            this.basicAuthHeader =
+                    "Basic "
+                            + Base64.getEncoder()
+                                    .encodeToString(
+                                            (username + ":" + password)
+                                                    
.getBytes(StandardCharsets.UTF_8));
+        } else {
+            this.basicAuthHeader = null;
+        }
+
+        this.httpClient = ApacheHttpClient.create();
+        this.signer = Aws4Signer.create();
+        this.credentialsProvider = DefaultCredentialsProvider.create();
+
+        Integer bulkMb = descriptor.getArgument("max_bulk_mb");
+        this.maxBulkBytes = (bulkMb != null ? bulkMb : 5) * 1024 * 1024;
+    }
+
+    @Override
+    public void close() throws Exception {
+        this.httpClient.close();
+        this.credentialsProvider.close();
+    }
+
+    /**
+     * Batch-embeds all documents in a single call, then delegates to 
addEmbedding.
+     *
+     * <p>TODO: This batch embedding logic is duplicated in 
S3VectorsVectorStore. Consider
+     * extracting to BaseVectorStore in a follow-up (would also benefit 
ElasticsearchVectorStore).

Review Comment:
    Agreed. Will submit as a follow-up PR.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to