$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill aws-sdk-java-v2-bedrockProvides Amazon Bedrock patterns using AWS SDK for Java 2.x. Invokes foundation models (Claude, Llama, Titan), generates text and images, creates embeddings for RAG, streams real-time responses, and configures Spring Boot integration. Use when asking about Bedrock integration, Ja
| 1 | # AWS SDK for Java 2.x - Amazon Bedrock |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Invokes foundation models through AWS SDK for Java 2.x. Configures clients, builds model-specific JSON payloads, handles streaming responses with error recovery, creates embeddings for RAG, integrates generative AI into Spring Boot applications, and implements exponential backoff for resilience. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - Invoke Claude, Llama, Titan, or Stable Diffusion for text/image generation |
| 10 | - Configure BedrockClient and BedrockRuntimeClient instances |
| 11 | - Build and parse model-specific payloads (Claude, Titan, Llama formats) |
| 12 | - Stream real-time AI responses with async handlers and error recovery |
| 13 | - Create embeddings for retrieval-augmented generation |
| 14 | - Integrate generative AI into Spring Boot microservices |
| 15 | - Handle throttling with exponential backoff retry logic |
| 16 | |
| 17 | ## Quick Start |
| 18 | |
| 19 | ### Dependencies |
| 20 | |
| 21 | ```xml |
| 22 | <!-- Bedrock (model management) --> |
| 23 | <dependency> |
| 24 | <groupId>software.amazon.awssdk</groupId> |
| 25 | <artifactId>bedrock</artifactId> |
| 26 | </dependency> |
| 27 | |
| 28 | <!-- Bedrock Runtime (model invocation) --> |
| 29 | <dependency> |
| 30 | <groupId>software.amazon.awssdk</groupId> |
| 31 | <artifactId>bedrockruntime</artifactId> |
| 32 | </dependency> |
| 33 | |
| 34 | <!-- For JSON processing --> |
| 35 | <dependency> |
| 36 | <groupId>org.json</groupId> |
| 37 | <artifactId>json</artifactId> |
| 38 | <version>20231013</version> |
| 39 | </dependency> |
| 40 | ``` |
| 41 | |
| 42 | ### Client Setup |
| 43 | |
| 44 | ```java |
| 45 | import software.amazon.awssdk.regions.Region; |
| 46 | import software.amazon.awssdk.services.bedrock.BedrockClient; |
| 47 | import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient; |
| 48 | |
| 49 | // Model management client |
| 50 | BedrockClient bedrockClient = BedrockClient.builder() |
| 51 | .region(Region.US_EAST_1) |
| 52 | .build(); |
| 53 | |
| 54 | // Model invocation client |
| 55 | BedrockRuntimeClient bedrockRuntimeClient = BedrockRuntimeClient.builder() |
| 56 | .region(Region.US_EAST_1) |
| 57 | .build(); |
| 58 | ``` |
| 59 | |
| 60 | ## Instructions |
| 61 | |
| 62 | Follow these steps for production-ready Bedrock integration: |
| 63 | |
| 64 | 1. **Configure AWS Credentials** - Set up IAM roles with Bedrock permissions (avoid access keys) |
| 65 | 2. **Enable Model Access** - Request access to specific foundation models in AWS Console |
| 66 | 3. **Initialize Clients** - Create reusable `BedrockClient` and `BedrockRuntimeClient` instances |
| 67 | 4. **Validate Model Availability** - Test with a simple invocation before production use |
| 68 | 5. **Build Payloads** - Create model-specific JSON payloads with proper format |
| 69 | 6. **Handle Responses** - Parse response structure and extract content |
| 70 | 7. **Implement Streaming** - Use response stream handlers for real-time generation |
| 71 | 8. **Add Error Handling** - Implement retry logic with exponential backoff |
| 72 | |
| 73 | **Validation Checkpoint**: Always test with a simple prompt (e.g., "Hello") before production use to verify model access and response parsing. |
| 74 | |
| 75 | ## Examples |
| 76 | |
| 77 | ### Text Generation with Claude |
| 78 | |
| 79 | ```java |
| 80 | public String generateWithClaude(BedrockRuntimeClient client, String prompt) { |
| 81 | JSONObject payload = new JSONObject() |
| 82 | .put("anthropic_version", "bedrock-2023-05-31") |
| 83 | .put("max_tokens", 1000) |
| 84 | .put("messages", new JSONObject[]{ |
| 85 | new JSONObject().put("role", "user").put("content", prompt) |
| 86 | }); |
| 87 | |
| 88 | InvokeModelResponse response = client.invokeModel(InvokeModelRequest.builder() |
| 89 | .modelId("anthropic.claude-sonnet-4-5-20250929-v1:0") |
| 90 | .body(SdkBytes.fromUtf8String(payload.toString())) |
| 91 | .build()); |
| 92 | |
| 93 | JSONObject responseBody = new JSONObject(response.body().asUtf8String()); |
| 94 | return responseBody.getJSONArray("content") |
| 95 | .getJSONObject(0) |
| 96 | .getString("text"); |
| 97 | } |
| 98 | ``` |
| 99 | |
| 100 | ### Model Discovery |
| 101 | |
| 102 | ```java |
| 103 | import software.amazon.awssdk.services.bedrock.model.*; |
| 104 | |
| 105 | public List<FoundationModelSummary> listFoundationModels(BedrockClient bedrockClient) { |
| 106 | return bedrockClient.listFoundationModels().modelSummaries(); |
| 107 | } |
| 108 | ``` |
| 109 | |
| 110 | ### Multi-Model Invocation |
| 111 | |
| 112 | ```java |
| 113 | public String invokeModel(BedrockRuntimeClient client, String modelId, String prompt) { |
| 114 | JSONObject payload = createPayload(modelId, prompt); |
| 115 | |
| 116 | InvokeModelResponse response = client.invokeModel(request -> request |
| 117 | .modelId(modelId) |
| 118 | .body(SdkBytes.fromUtf8String(payload.toString()))); |
| 119 | |
| 120 | return extractTextFromResponse(modelId, response.body().asUtf8String()); |
| 121 | } |
| 122 | |
| 123 | private JSONObject createPayload(String modelId, String prompt) { |
| 124 | if (modelId.startsWith("anthropic.claude")) { |
| 125 | return new JSONObject() |
| 126 | .put("anthropic_version", "bedrock-2023-05-31") |
| 127 | .put("max_tokens", 1000) |
| 128 | .put("messages", new JSONObject[]{ |
| 129 | new |