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


##########
integrations/vector-stores/s3vectors/src/main/java/org/apache/flink/agents/integrations/vectorstores/s3vectors/S3VectorsVectorStore.java:
##########
@@ -0,0 +1,339 @@
+/*
+ * 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.s3vectors;
+
+import org.apache.flink.agents.api.RetryExecutor;
+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.Document;
+import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.s3vectors.S3VectorsClient;
+import software.amazon.awssdk.services.s3vectors.model.DeleteVectorsRequest;
+import software.amazon.awssdk.services.s3vectors.model.GetOutputVector;
+import software.amazon.awssdk.services.s3vectors.model.GetVectorsRequest;
+import software.amazon.awssdk.services.s3vectors.model.GetVectorsResponse;
+import software.amazon.awssdk.services.s3vectors.model.PutInputVector;
+import software.amazon.awssdk.services.s3vectors.model.PutVectorsRequest;
+import software.amazon.awssdk.services.s3vectors.model.QueryOutputVector;
+import software.amazon.awssdk.services.s3vectors.model.QueryVectorsRequest;
+import software.amazon.awssdk.services.s3vectors.model.QueryVectorsResponse;
+import software.amazon.awssdk.services.s3vectors.model.VectorData;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import java.util.function.BiFunction;
+
+/**
+ * Amazon S3 Vectors vector store for flink-agents.
+ *
+ * <p>Uses the S3 Vectors SDK for 
PutVectors/QueryVectors/GetVectors/DeleteVectors. PutVectors calls
+ * are chunked at 500 vectors per request (API limit).
+ *
+ * <p>Supported parameters:
+ *
+ * <ul>
+ *   <li><b>embedding_model</b> (required): name of the embedding model 
resource
+ *   <li><b>vector_bucket</b> (required): S3 Vectors bucket name
+ *   <li><b>vector_index</b> (required): S3 Vectors index name
+ *   <li><b>region</b> (optional): AWS region (default: us-east-1)
+ * </ul>
+ *
+ * <p>Example usage:
+ *
+ * <pre>{@code
+ * @VectorStore
+ * public static ResourceDescriptor s3VectorsStore() {
+ *     return 
ResourceDescriptor.Builder.newBuilder(S3VectorsVectorStore.class.getName())
+ *             .addInitialArgument("embedding_model", "bedrockEmbeddingSetup")
+ *             .addInitialArgument("vector_bucket", "my-vector-bucket")
+ *             .addInitialArgument("vector_index", "my-index")
+ *             .addInitialArgument("region", "us-east-1")
+ *             .build();
+ * }
+ * }</pre>
+ */
+public class S3VectorsVectorStore extends BaseVectorStore {
+
+    private static final int MAX_PUT_VECTORS_BATCH = 500;
+
+    private final S3VectorsClient client;
+    private final String vectorBucket;
+    private final String vectorIndex;
+    private final RetryExecutor retryExecutor;
+
+    public S3VectorsVectorStore(
+            ResourceDescriptor descriptor, BiFunction<String, ResourceType, 
Resource> getResource) {
+        super(descriptor, getResource);
+
+        this.vectorBucket = descriptor.getArgument("vector_bucket");
+        if (this.vectorBucket == null || this.vectorBucket.isBlank()) {
+            throw new IllegalArgumentException(
+                    "vector_bucket is required for S3VectorsVectorStore");
+        }
+
+        this.vectorIndex = descriptor.getArgument("vector_index");
+        if (this.vectorIndex == null || this.vectorIndex.isBlank()) {
+            throw new IllegalArgumentException("vector_index is required for 
S3VectorsVectorStore");
+        }
+
+        String regionStr = descriptor.getArgument("region");
+        this.client =
+                S3VectorsClient.builder()
+                        .region(Region.of(regionStr != null ? regionStr : 
"us-east-1"))
+                        
.credentialsProvider(DefaultCredentialsProvider.builder().build())
+                        .build();
+
+        this.retryExecutor =
+                RetryExecutor.builder()
+                        .maxRetries(5)

Review Comment:
   ditto



##########
integrations/vector-stores/opensearch/src/main/java/org/apache/flink/agents/integrations/vectorstores/opensearch/OpenSearchVectorStore.java:
##########
@@ -0,0 +1,558 @@
+/*
+ * 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.RetryExecutor;
+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 static final int DEFAULT_GET_LIMIT = 10000;
+
+    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;
+    private final RetryExecutor retryExecutor;
+
+    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");
+
+        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.builder().build();
+
+        Integer bulkMb = descriptor.getArgument("max_bulk_mb");
+        this.maxBulkBytes = (bulkMb != null ? bulkMb : 5) * 1024 * 1024;
+
+        this.retryExecutor =
+                RetryExecutor.builder()
+                        .maxRetries(5)

Review Comment:
   Should this be configuable?



-- 
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