Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .doc_gen/metadata/bedrock-runtime_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,44 @@ bedrock-runtime_InvokeModelWithResponseStream_MistralAi:
services:
bedrock-runtime: {InvokeModelWithResponseStream}

# Reasoning
bedrock-runtime_Converse_AnthropicClaudeReasoning:
title: Use Anthropic Claude 3.7 Sonnet's reasoning capability on &BR;
title_abbrev: Reasoning
synopsis: use Anthropic Claude 3.7 Sonnet's reasoning capability on &BR;
category: Anthropic Claude
languages:
Java:
versions:
- sdk_version: 2
github: javav2/example_code/bedrock-runtime
excerpts:
- description: Use Anthropic Claude 3.7 Sonnet's reasoning capability with the asynchronous Bedrock runtime client.
snippet_tags:
- bedrock-runtime.java2.ConverseAsync_AnthropicClaudeReasoning
- description: Use Anthropic Claude 3.7 Sonnet's reasoning capability with the synchronous Bedrock runtime client.
snippet_tags:
- bedrock-runtime.java2.Converse_AnthropicClaudeReasoning
services:
bedrock-runtime: {Converse}

bedrock-runtime_ConverseStream_AnthropicClaudeReasoning:
title: Use Anthropic Claude 3.7 Sonnet's reasoning capability on &BR;
title_abbrev: Reasoning with a streaming response
synopsis: use Anthropic Claude 3.7 Sonnet's reasoning capability on &BR;
category: Anthropic Claude
languages:
Java:
versions:
- sdk_version: 2
github: javav2/example_code/bedrock-runtime
excerpts:
- description: Use Anthropic Claude 3.7 Sonnet's reasoning capability to generate streaming text responses.
snippet_tags:
- bedrock-runtime.java2.ConverseStream_AnthropicClaudeReasoning
services:
bedrock-runtime: {Converse}

# Image Generation Models
bedrock-runtime_InvokeModel_AmazonNovaImageGeneration:
title: Invoke Amazon Nova Canvas on &BR; to generate an image
Expand Down
3 changes: 3 additions & 0 deletions javav2/example_code/bedrock-runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ _Amazon Bedrock Runtime is a fully managed service that makes it easy to use fou
* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).

<!--custom.important.start-->
**Note: This project uses JDK 21**
<!--custom.important.end-->

## Code examples
Expand Down Expand Up @@ -68,6 +69,8 @@ For prerequisites, see the [README](../../README.md#Prerequisites) in the `javav
- [ConverseStream](src/main/java/com/example/bedrockruntime/models/anthropicClaude/ConverseStream.java#L6)
- [InvokeModel](src/main/java/com/example/bedrockruntime/models/anthropicClaude/InvokeModel.java#L6)
- [InvokeModelWithResponseStream](src/main/java/com/example/bedrockruntime/models/anthropicClaude/InvokeModelWithResponseStream.java#L6)
- [Reasoning](src/main/java/com/example/bedrockruntime/models/anthropicClaude/ReasoningAsync.java#L6)
- [Reasoning with a streaming response](src/main/java/com/example/bedrockruntime/models/anthropicClaude/ReasoningStream.java#L6)

### Cohere Command

Expand Down
4 changes: 2 additions & 2 deletions javav2/example_code/bedrock-runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>17</java.version>
<java.version>21</java.version>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.source>${java.version}</maven.compiler.source>
</properties>
Expand All @@ -30,7 +30,7 @@
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.30.22</version>
<version>2.30.27</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

public class InvokeModelWithResponseStream {

public static String invokeModelWithResponseStream() throws ExecutionException, InterruptedException {
public static String invokeModelWithResponseStream() {

// Create a Bedrock Runtime client in the AWS Region you want to use.
// Replace the DefaultCredentialsProvider with your preferred credentials provider.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static String converse() {
.topP(0.9F)));

// Retrieve the generated text from Bedrock's response object.
var responseText = response.output().message().content().get(0).text();
var responseText = response.output().message().content().getFirst().text();
System.out.println(responseText);

return responseText;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static String converseAsync() {
request.whenComplete((response, error) -> {
if (error == null) {
// Extract the generated text from Bedrock's response object.
String responseText = response.output().message().content().get(0).text();
String responseText = response.output().message().content().getFirst().text();
future.complete(responseText);
} else {
future.completeExceptionally(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public class InvokeModelWithResponseStream {

public static String invokeModelWithResponseStream() throws ExecutionException, InterruptedException {
public static String invokeModelWithResponseStream() {

// Create a Bedrock Runtime client in the AWS Region you want to use.
// Replace the DefaultCredentialsProvider with your preferred credentials provider.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package com.example.bedrockruntime.models.anthropicClaude;

// snippet-start:[bedrock-runtime.java2.Converse_AnthropicClaudeReasoning]

import com.example.bedrockruntime.models.anthropicClaude.lib.ReasoningResponse;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.core.document.Document;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient;
import software.amazon.awssdk.services.bedrockruntime.model.*;

/**
* This example demonstrates how to use Anthropic Claude 3.7 Sonnet's reasoning capability
* with the synchronous Amazon Bedrock runtime client.
* It shows how to:
* - Set up the Amazon Bedrock runtime client
* - Create a message
* - Configure reasoning parameters
* - Send a request with reasoning enabled
* - Process both the reasoning output and final response
*/
public class Reasoning {

public static ReasoningResponse reasoning() {

// Create the Amazon Bedrock runtime client
var client = BedrockRuntimeClient.builder()
.credentialsProvider(DefaultCredentialsProvider.create())
.region(Region.US_EAST_1)
.build();

// Specify the model ID. For the latest available models, see:
// https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html
var modelId = "us.anthropic.claude-3-7-sonnet-20250219-v1:0";

// Create the message with the user's prompt
var prompt = "Describe the purpose of a 'hello world' program in one line.";
var message = Message.builder()
.content(ContentBlock.fromText(prompt))
.role(ConversationRole.USER)
.build();

// Configure reasoning parameters with a 2000 token budget
Document reasoningConfig = Document.mapBuilder()
.putDocument("thinking", Document.mapBuilder()
.putString("type", "enabled")
.putNumber("budget_tokens", 2000)
.build())
.build();

try {
// Send message and reasoning configuration to the model
ConverseResponse bedrockResponse = client.converse(request -> request
.additionalModelRequestFields(reasoningConfig)
.messages(message)
.modelId(modelId)
);


// Extract both reasoning and final response
var content = bedrockResponse.output().message().content();
ReasoningContentBlock reasoning = null;
String text = null;

// Process each content block to find reasoning and response text
for (ContentBlock block : content) {
if (block.reasoningContent() != null) {
reasoning = block.reasoningContent();
} else if (block.text() != null) {
text = block.text();
}
}

return new ReasoningResponse(reasoning, text);

} catch (SdkClientException e) {
System.err.printf("ERROR: Can't invoke '%s'. Reason: %s", modelId, e.getMessage());
throw new RuntimeException(e);
}
}

public static void main(String[] args) {
// Execute the example and display reasoning and final response
ReasoningResponse response = reasoning();
System.out.println("\n<thinking>");
System.out.println(response.reasoning().reasoningText());
System.out.println("</thinking>\n");
System.out.println(response.text());
}
}

// snippet-end:[bedrock-runtime.java2.Converse_AnthropicClaudeReasoning]
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package com.example.bedrockruntime.models.anthropicClaude;

// snippet-start:[bedrock-runtime.java2.ConverseAsync_AnthropicClaudeReasoning]

import com.example.bedrockruntime.models.anthropicClaude.lib.ReasoningResponse;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.core.document.Document;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeAsyncClient;
import software.amazon.awssdk.services.bedrockruntime.model.*;

import java.util.concurrent.CompletableFuture;

/**
* This example demonstrates how to use Anthropic Claude 3.7 Sonnet's reasoning capability
* with an asynchronous Amazon Bedrock runtime client.
* It shows how to:
* - Set up the Amazon Bedrock async runtime client
* - Create a message
* - Configure reasoning parameters
* - Send an asynchronous request with reasoning enabled
* - Process both the reasoning output and final response
*/
public class ReasoningAsync {

public static ReasoningResponse reasoningAsync() {

// Create the Amazon Bedrock runtime client
var client = BedrockRuntimeAsyncClient.builder()
.credentialsProvider(DefaultCredentialsProvider.create())
.region(Region.US_EAST_1)
.build();

// Specify the model ID. For the latest available models, see:
// https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html
var modelId = "us.anthropic.claude-3-7-sonnet-20250219-v1:0";

// Create the message with the user's prompt
var prompt = "Describe the purpose of a 'hello world' program in one line.";
var message = Message.builder()
.content(ContentBlock.fromText(prompt))
.role(ConversationRole.USER)
.build();

// Configure reasoning parameters with a 2000 token budget
Document reasoningConfig = Document.mapBuilder()
.putDocument("thinking", Document.mapBuilder()
.putString("type", "enabled")
.putNumber("budget_tokens", 2000)
.build())
.build();

try {
// Send message and reasoning configuration to the model
CompletableFuture<ConverseResponse> asyncResponse = client.converse(request -> request
.additionalModelRequestFields(reasoningConfig)
.messages(message)
.modelId(modelId)
);

// Process the response asynchronously
return asyncResponse.thenApply(response -> {

var content = response.output().message().content();
ReasoningContentBlock reasoning = null;
String text = null;

// Process each content block to find reasoning and response text
for (ContentBlock block : content) {
if (block.reasoningContent() != null) {
reasoning = block.reasoningContent();
} else if (block.text() != null) {
text = block.text();
}
}

return new ReasoningResponse(reasoning, text);
}
).get();

} catch (Exception e) {
System.err.printf("Can't invoke '%s': %s", modelId, e.getMessage());
throw new RuntimeException(e);
}
}

public static void main(String[] args) {
// Execute the example and display reasoning and final response
ReasoningResponse response = reasoningAsync();
System.out.println("\n<thinking>");
System.out.println(response.reasoning().reasoningText());
System.out.println("</thinking>\n");
System.out.println(response.text());
}
}
// snippet-end:[bedrock-runtime.java2.ConverseAsync_AnthropicClaudeReasoning]
Loading
Loading