wenjin272 commented on code in PR #534: URL: https://github.com/apache/flink-agents/pull/534#discussion_r2875758557
########## integrations/chat-models/bedrock/src/main/java/org/apache/flink/agents/integrations/chatmodels/bedrock/BedrockChatModelConnection.java: ########## @@ -0,0 +1,428 @@ +/* + * 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.chatmodels.bedrock; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.flink.agents.api.RetryExecutor; +import org.apache.flink.agents.api.chat.messages.ChatMessage; +import org.apache.flink.agents.api.chat.messages.MessageRole; +import org.apache.flink.agents.api.chat.model.BaseChatModelConnection; +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.tools.Tool; +import org.apache.flink.agents.api.tools.ToolMetadata; +import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; +import software.amazon.awssdk.core.SdkNumber; +import software.amazon.awssdk.core.document.Document; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient; +import software.amazon.awssdk.services.bedrockruntime.model.ContentBlock; +import software.amazon.awssdk.services.bedrockruntime.model.ConversationRole; +import software.amazon.awssdk.services.bedrockruntime.model.ConverseRequest; +import software.amazon.awssdk.services.bedrockruntime.model.ConverseResponse; +import software.amazon.awssdk.services.bedrockruntime.model.InferenceConfiguration; +import software.amazon.awssdk.services.bedrockruntime.model.Message; +import software.amazon.awssdk.services.bedrockruntime.model.SystemContentBlock; +import software.amazon.awssdk.services.bedrockruntime.model.ToolConfiguration; +import software.amazon.awssdk.services.bedrockruntime.model.ToolInputSchema; +import software.amazon.awssdk.services.bedrockruntime.model.ToolResultBlock; +import software.amazon.awssdk.services.bedrockruntime.model.ToolResultContentBlock; +import software.amazon.awssdk.services.bedrockruntime.model.ToolSpecification; +import software.amazon.awssdk.services.bedrockruntime.model.ToolUseBlock; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.function.BiFunction; +import java.util.stream.Collectors; + +/** + * Bedrock Converse API chat model connection for flink-agents. + * + * <p>Uses the Converse API which provides a unified interface across all Bedrock models with native + * tool calling support. Authentication is handled via SigV4 using the default AWS credentials + * chain. + * + * <p>Future work: support reasoning content blocks (Claude extended thinking), citation blocks, and + * image/document content blocks. + * + * <p>Supported connection parameters: + * + * <ul> + * <li><b>region</b> (optional): AWS region (defaults to us-east-1) + * <li><b>model</b> (optional): Default model ID (e.g. us.anthropic.claude-sonnet-4-20250514-v1:0) + * </ul> + * + * <p>Example usage: + * + * <pre>{@code + * @ChatModelConnection + * public static ResourceDescriptor bedrockConnection() { + * return ResourceDescriptor.Builder.newBuilder(BedrockChatModelConnection.class.getName()) + * .addInitialArgument("region", "us-east-1") + * .addInitialArgument("model", "us.anthropic.claude-sonnet-4-20250514-v1:0") + * .build(); + * } + * }</pre> + */ +public class BedrockChatModelConnection extends BaseChatModelConnection { + + private static final ObjectMapper MAPPER = new ObjectMapper(); + private final BedrockRuntimeClient client; + private final String defaultModel; + private final RetryExecutor retryExecutor; + + public BedrockChatModelConnection( + ResourceDescriptor descriptor, BiFunction<String, ResourceType, Resource> getResource) { + super(descriptor, getResource); + + String region = descriptor.getArgument("region"); + if (region == null || region.isBlank()) { + region = "us-east-1"; + } + + this.client = + BedrockRuntimeClient.builder() + .region(Region.of(region)) + .credentialsProvider(DefaultCredentialsProvider.create()) + .build(); + + this.defaultModel = descriptor.getArgument("model"); + this.retryExecutor = + RetryExecutor.builder() + .maxRetries(5) Review Comment: Should this be configurable? ########## integrations/embedding-models/bedrock/src/main/java/org/apache/flink/agents/integrations/embeddingmodels/bedrock/BedrockEmbeddingModelConnection.java: ########## @@ -0,0 +1,187 @@ +/* + * 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.embeddingmodels.bedrock; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.apache.flink.agents.api.RetryExecutor; +import org.apache.flink.agents.api.embedding.model.BaseEmbeddingModelConnection; +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 software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; +import software.amazon.awssdk.core.SdkBytes; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient; +import software.amazon.awssdk.services.bedrockruntime.model.InvokeModelRequest; +import software.amazon.awssdk.services.bedrockruntime.model.InvokeModelResponse; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.function.BiFunction; + +/** + * Bedrock embedding model connection using Amazon Titan Text Embeddings V2. + * + * <p>Uses the InvokeModel API to generate embeddings. Supports configurable dimensions (256, 512, + * or 1024) and normalization. Since Titan V2 processes one text per API call, batch embedding is + * parallelized via a configurable thread pool. + * + * <p>Supported connection parameters: + * + * <ul> + * <li><b>region</b> (optional): AWS region, defaults to us-east-1 + * <li><b>model</b> (optional): default model ID, defaults to amazon.titan-embed-text-v2:0 + * <li><b>embed_concurrency</b> (optional): thread pool size for parallel embedding (default: 4) + * </ul> + * + * <p>Example usage: + * + * <pre>{@code + * @EmbeddingModelConnection + * public static ResourceDescriptor bedrockEmbedding() { + * return ResourceDescriptor.Builder.newBuilder(BedrockEmbeddingModelConnection.class.getName()) + * .addInitialArgument("region", "us-east-1") + * .addInitialArgument("model", "amazon.titan-embed-text-v2:0") + * .addInitialArgument("embed_concurrency", 8) + * .build(); + * } + * }</pre> + */ +public class BedrockEmbeddingModelConnection extends BaseEmbeddingModelConnection { + + private static final ObjectMapper MAPPER = new ObjectMapper(); + private static final String DEFAULT_MODEL = "amazon.titan-embed-text-v2:0"; + + private final BedrockRuntimeClient client; + private final String defaultModel; + private final ExecutorService embedPool; + private final RetryExecutor retryExecutor; + + public BedrockEmbeddingModelConnection( + ResourceDescriptor descriptor, BiFunction<String, ResourceType, Resource> getResource) { + super(descriptor, getResource); + + String region = descriptor.getArgument("region"); + if (region == null || region.isBlank()) { + region = "us-east-1"; + } + + this.client = + BedrockRuntimeClient.builder() + .region(Region.of(region)) + .credentialsProvider(DefaultCredentialsProvider.create()) + .build(); + + String model = descriptor.getArgument("model"); + this.defaultModel = (model != null && !model.isBlank()) ? model : DEFAULT_MODEL; + + Integer concurrency = descriptor.getArgument("embed_concurrency"); + int threads = concurrency != null ? concurrency : 4; + this.embedPool = Executors.newFixedThreadPool(threads); + + this.retryExecutor = + RetryExecutor.builder() + .maxRetries(5) Review Comment: ditto -- 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]
