Skip to content

Commit 1c4de2b

Browse files
authored
Java: Reasoning examples for Claude 3.7 on Amazon Bedrock (#7260)
* Add Java reasoning examples for Claude 3.7 * Update README.md * Update README.md
1 parent 17b6404 commit 1c4de2b

21 files changed

+442
-91
lines changed

.doc_gen/metadata/bedrock-runtime_metadata.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,44 @@ bedrock-runtime_InvokeModelWithResponseStream_MistralAi:
12361236
services:
12371237
bedrock-runtime: {InvokeModelWithResponseStream}
12381238

1239+
# Reasoning
1240+
bedrock-runtime_Converse_AnthropicClaudeReasoning:
1241+
title: Use Anthropic Claude 3.7 Sonnet's reasoning capability on &BR;
1242+
title_abbrev: Reasoning
1243+
synopsis: use Anthropic Claude 3.7 Sonnet's reasoning capability on &BR;
1244+
category: Anthropic Claude
1245+
languages:
1246+
Java:
1247+
versions:
1248+
- sdk_version: 2
1249+
github: javav2/example_code/bedrock-runtime
1250+
excerpts:
1251+
- description: Use Anthropic Claude 3.7 Sonnet's reasoning capability with the asynchronous Bedrock runtime client.
1252+
snippet_tags:
1253+
- bedrock-runtime.java2.ConverseAsync_AnthropicClaudeReasoning
1254+
- description: Use Anthropic Claude 3.7 Sonnet's reasoning capability with the synchronous Bedrock runtime client.
1255+
snippet_tags:
1256+
- bedrock-runtime.java2.Converse_AnthropicClaudeReasoning
1257+
services:
1258+
bedrock-runtime: {Converse}
1259+
1260+
bedrock-runtime_ConverseStream_AnthropicClaudeReasoning:
1261+
title: Use Anthropic Claude 3.7 Sonnet's reasoning capability on &BR;
1262+
title_abbrev: Reasoning with a streaming response
1263+
synopsis: use Anthropic Claude 3.7 Sonnet's reasoning capability on &BR;
1264+
category: Anthropic Claude
1265+
languages:
1266+
Java:
1267+
versions:
1268+
- sdk_version: 2
1269+
github: javav2/example_code/bedrock-runtime
1270+
excerpts:
1271+
- description: Use Anthropic Claude 3.7 Sonnet's reasoning capability to generate streaming text responses.
1272+
snippet_tags:
1273+
- bedrock-runtime.java2.ConverseStream_AnthropicClaudeReasoning
1274+
services:
1275+
bedrock-runtime: {Converse}
1276+
12391277
# Image Generation Models
12401278
bedrock-runtime_InvokeModel_AmazonNovaImageGeneration:
12411279
title: Invoke Amazon Nova Canvas on &BR; to generate an image

javav2/example_code/bedrock-runtime/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ _Amazon Bedrock Runtime is a fully managed service that makes it easy to use fou
1717
* 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).
1818

1919
<!--custom.important.start-->
20+
**Note: This project uses JDK 21**
2021
<!--custom.important.end-->
2122

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

7275
### Cohere Command
7376

javav2/example_code/bedrock-runtime/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<version>1.0-SNAPSHOT</version>
99
<properties>
1010
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11-
<java.version>17</java.version>
11+
<java.version>21</java.version>
1212
<maven.compiler.target>${java.version}</maven.compiler.target>
1313
<maven.compiler.source>${java.version}</maven.compiler.source>
1414
</properties>
@@ -30,7 +30,7 @@
3030
<dependency>
3131
<groupId>software.amazon.awssdk</groupId>
3232
<artifactId>bom</artifactId>
33-
<version>2.30.22</version>
33+
<version>2.30.27</version>
3434
<type>pom</type>
3535
<scope>import</scope>
3636
</dependency>

javav2/example_code/bedrock-runtime/src/main/java/com/example/bedrockruntime/models/amazonTitanText/InvokeModelWithResponseStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
public class InvokeModelWithResponseStream {
2424

25-
public static String invokeModelWithResponseStream() throws ExecutionException, InterruptedException {
25+
public static String invokeModelWithResponseStream() {
2626

2727
// Create a Bedrock Runtime client in the AWS Region you want to use.
2828
// Replace the DefaultCredentialsProvider with your preferred credentials provider.

javav2/example_code/bedrock-runtime/src/main/java/com/example/bedrockruntime/models/anthropicClaude/Converse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static String converse() {
4848
.topP(0.9F)));
4949

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

5454
return responseText;

javav2/example_code/bedrock-runtime/src/main/java/com/example/bedrockruntime/models/anthropicClaude/ConverseAsync.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static String converseAsync() {
5555
request.whenComplete((response, error) -> {
5656
if (error == null) {
5757
// Extract the generated text from Bedrock's response object.
58-
String responseText = response.output().message().content().get(0).text();
58+
String responseText = response.output().message().content().getFirst().text();
5959
future.complete(responseText);
6060
} else {
6161
future.completeExceptionally(error);

javav2/example_code/bedrock-runtime/src/main/java/com/example/bedrockruntime/models/anthropicClaude/InvokeModelWithResponseStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
public class InvokeModelWithResponseStream {
2525

26-
public static String invokeModelWithResponseStream() throws ExecutionException, InterruptedException {
26+
public static String invokeModelWithResponseStream() {
2727

2828
// Create a Bedrock Runtime client in the AWS Region you want to use.
2929
// Replace the DefaultCredentialsProvider with your preferred credentials provider.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package com.example.bedrockruntime.models.anthropicClaude;
5+
6+
// snippet-start:[bedrock-runtime.java2.Converse_AnthropicClaudeReasoning]
7+
8+
import com.example.bedrockruntime.models.anthropicClaude.lib.ReasoningResponse;
9+
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
10+
import software.amazon.awssdk.core.document.Document;
11+
import software.amazon.awssdk.core.exception.SdkClientException;
12+
import software.amazon.awssdk.regions.Region;
13+
import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient;
14+
import software.amazon.awssdk.services.bedrockruntime.model.*;
15+
16+
/**
17+
* This example demonstrates how to use Anthropic Claude 3.7 Sonnet's reasoning capability
18+
* with the synchronous Amazon Bedrock runtime client.
19+
* It shows how to:
20+
* - Set up the Amazon Bedrock runtime client
21+
* - Create a message
22+
* - Configure reasoning parameters
23+
* - Send a request with reasoning enabled
24+
* - Process both the reasoning output and final response
25+
*/
26+
public class Reasoning {
27+
28+
public static ReasoningResponse reasoning() {
29+
30+
// Create the Amazon Bedrock runtime client
31+
var client = BedrockRuntimeClient.builder()
32+
.credentialsProvider(DefaultCredentialsProvider.create())
33+
.region(Region.US_EAST_1)
34+
.build();
35+
36+
// Specify the model ID. For the latest available models, see:
37+
// https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html
38+
var modelId = "us.anthropic.claude-3-7-sonnet-20250219-v1:0";
39+
40+
// Create the message with the user's prompt
41+
var prompt = "Describe the purpose of a 'hello world' program in one line.";
42+
var message = Message.builder()
43+
.content(ContentBlock.fromText(prompt))
44+
.role(ConversationRole.USER)
45+
.build();
46+
47+
// Configure reasoning parameters with a 2000 token budget
48+
Document reasoningConfig = Document.mapBuilder()
49+
.putDocument("thinking", Document.mapBuilder()
50+
.putString("type", "enabled")
51+
.putNumber("budget_tokens", 2000)
52+
.build())
53+
.build();
54+
55+
try {
56+
// Send message and reasoning configuration to the model
57+
ConverseResponse bedrockResponse = client.converse(request -> request
58+
.additionalModelRequestFields(reasoningConfig)
59+
.messages(message)
60+
.modelId(modelId)
61+
);
62+
63+
64+
// Extract both reasoning and final response
65+
var content = bedrockResponse.output().message().content();
66+
ReasoningContentBlock reasoning = null;
67+
String text = null;
68+
69+
// Process each content block to find reasoning and response text
70+
for (ContentBlock block : content) {
71+
if (block.reasoningContent() != null) {
72+
reasoning = block.reasoningContent();
73+
} else if (block.text() != null) {
74+
text = block.text();
75+
}
76+
}
77+
78+
return new ReasoningResponse(reasoning, text);
79+
80+
} catch (SdkClientException e) {
81+
System.err.printf("ERROR: Can't invoke '%s'. Reason: %s", modelId, e.getMessage());
82+
throw new RuntimeException(e);
83+
}
84+
}
85+
86+
public static void main(String[] args) {
87+
// Execute the example and display reasoning and final response
88+
ReasoningResponse response = reasoning();
89+
System.out.println("\n<thinking>");
90+
System.out.println(response.reasoning().reasoningText());
91+
System.out.println("</thinking>\n");
92+
System.out.println(response.text());
93+
}
94+
}
95+
96+
// snippet-end:[bedrock-runtime.java2.Converse_AnthropicClaudeReasoning]
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package com.example.bedrockruntime.models.anthropicClaude;
5+
6+
// snippet-start:[bedrock-runtime.java2.ConverseAsync_AnthropicClaudeReasoning]
7+
8+
import com.example.bedrockruntime.models.anthropicClaude.lib.ReasoningResponse;
9+
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
10+
import software.amazon.awssdk.core.document.Document;
11+
import software.amazon.awssdk.regions.Region;
12+
import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeAsyncClient;
13+
import software.amazon.awssdk.services.bedrockruntime.model.*;
14+
15+
import java.util.concurrent.CompletableFuture;
16+
17+
/**
18+
* This example demonstrates how to use Anthropic Claude 3.7 Sonnet's reasoning capability
19+
* with an asynchronous Amazon Bedrock runtime client.
20+
* It shows how to:
21+
* - Set up the Amazon Bedrock async runtime client
22+
* - Create a message
23+
* - Configure reasoning parameters
24+
* - Send an asynchronous request with reasoning enabled
25+
* - Process both the reasoning output and final response
26+
*/
27+
public class ReasoningAsync {
28+
29+
public static ReasoningResponse reasoningAsync() {
30+
31+
// Create the Amazon Bedrock runtime client
32+
var client = BedrockRuntimeAsyncClient.builder()
33+
.credentialsProvider(DefaultCredentialsProvider.create())
34+
.region(Region.US_EAST_1)
35+
.build();
36+
37+
// Specify the model ID. For the latest available models, see:
38+
// https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html
39+
var modelId = "us.anthropic.claude-3-7-sonnet-20250219-v1:0";
40+
41+
// Create the message with the user's prompt
42+
var prompt = "Describe the purpose of a 'hello world' program in one line.";
43+
var message = Message.builder()
44+
.content(ContentBlock.fromText(prompt))
45+
.role(ConversationRole.USER)
46+
.build();
47+
48+
// Configure reasoning parameters with a 2000 token budget
49+
Document reasoningConfig = Document.mapBuilder()
50+
.putDocument("thinking", Document.mapBuilder()
51+
.putString("type", "enabled")
52+
.putNumber("budget_tokens", 2000)
53+
.build())
54+
.build();
55+
56+
try {
57+
// Send message and reasoning configuration to the model
58+
CompletableFuture<ConverseResponse> asyncResponse = client.converse(request -> request
59+
.additionalModelRequestFields(reasoningConfig)
60+
.messages(message)
61+
.modelId(modelId)
62+
);
63+
64+
// Process the response asynchronously
65+
return asyncResponse.thenApply(response -> {
66+
67+
var content = response.output().message().content();
68+
ReasoningContentBlock reasoning = null;
69+
String text = null;
70+
71+
// Process each content block to find reasoning and response text
72+
for (ContentBlock block : content) {
73+
if (block.reasoningContent() != null) {
74+
reasoning = block.reasoningContent();
75+
} else if (block.text() != null) {
76+
text = block.text();
77+
}
78+
}
79+
80+
return new ReasoningResponse(reasoning, text);
81+
}
82+
).get();
83+
84+
} catch (Exception e) {
85+
System.err.printf("Can't invoke '%s': %s", modelId, e.getMessage());
86+
throw new RuntimeException(e);
87+
}
88+
}
89+
90+
public static void main(String[] args) {
91+
// Execute the example and display reasoning and final response
92+
ReasoningResponse response = reasoningAsync();
93+
System.out.println("\n<thinking>");
94+
System.out.println(response.reasoning().reasoningText());
95+
System.out.println("</thinking>\n");
96+
System.out.println(response.text());
97+
}
98+
}
99+
// snippet-end:[bedrock-runtime.java2.ConverseAsync_AnthropicClaudeReasoning]

0 commit comments

Comments
 (0)