avichaym commented on code in PR #533: URL: https://github.com/apache/flink-agents/pull/533#discussion_r2872143432
########## 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"); Review Comment: Good point. Made index optional — when null, operations use the collection parameter passed at call time. This supports use cases where the index is determined per-operation (e.g., Long-Term Memory collection management) -- 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]
