Cookie の設定を選択する

当社は、当社のサイトおよびサービスを提供するために必要な必須 Cookie および類似のツールを使用しています。当社は、パフォーマンス Cookie を使用して匿名の統計情報を収集することで、お客様が当社のサイトをどのように利用しているかを把握し、改善に役立てています。必須 Cookie は無効化できませんが、[カスタマイズ] または [拒否] をクリックしてパフォーマンス Cookie を拒否することはできます。

お客様が同意した場合、AWS および承認された第三者は、Cookie を使用して便利なサイト機能を提供したり、お客様の選択を記憶したり、関連する広告を含む関連コンテンツを表示したりします。すべての必須ではない Cookie を受け入れるか拒否するには、[受け入れる] または [拒否] をクリックしてください。より詳細な選択を行うには、[カスタマイズ] をクリックしてください。

Amazon Bedrock で Anthropic Claude 3.7 Sonnet の推論機能を使用する - AWS SDK コードの例

Doc AWS SDK Examples GitHub リポジトリには、他にも SDK の例があります。 AWS

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

Doc AWS SDK Examples GitHub リポジトリには、他にも SDK の例があります。 AWS

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

Amazon Bedrock で Anthropic Claude 3.7 Sonnet の推論機能を使用する

次のコード例は、Amazon Bedrock で Anthropic Claude 3.7 Sonnet の推論機能を使用する方法を示しています。

Java
SDK for Java 2.x
注記

GitHub には、その他のリソースもあります。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

Anthropic Claude 3.7 Sonnet の推論機能を使用して、ストリーミングテキストレスポンスを生成します。

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.ExecutionException; import java.util.concurrent.atomic.AtomicReference; /** * This example demonstrates how to use Anthropic Claude 3.7 Sonnet's reasoning * capability to generate streaming text responses. * It shows how to: * - Set up the Amazon Bedrock runtime client * - Create a message * - Configure a streaming request * - Set up a stream handler to process the response chunks * - Process the streaming response */ public class ReasoningStream { public static ReasoningResponse reasoningStream() { // 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(); // Configure the request with the message, model ID, and reasoning config ConverseStreamRequest request = ConverseStreamRequest.builder() .additionalModelRequestFields(reasoningConfig) .messages(message) .modelId(modelId) .build(); StringBuilder reasoning = new StringBuilder(); StringBuilder text = new StringBuilder(); AtomicReference<ReasoningResponse> finalresponse = new AtomicReference<>(); // Set up the stream handler to processes chunks of the response as they arrive var streamHandler = ConverseStreamResponseHandler.builder() .subscriber(ConverseStreamResponseHandler.Visitor.builder() .onContentBlockDelta(chunk -> { ContentBlockDelta delta = chunk.delta(); if (delta.reasoningContent() != null) { if (reasoning.isEmpty()) { System.out.println("\n<thinking>"); } if (delta.reasoningContent().text() != null) { System.out.print(delta.reasoningContent().text()); reasoning.append(delta.reasoningContent().text()); } } else if (delta.text() != null) { if (text.isEmpty()) { System.out.println("\n</thinking>\n"); } System.out.print(delta.text()); text.append(delta.text()); } System.out.flush(); // Ensure immediate output of each chunk }).build()) .onComplete(() -> finalresponse.set(new ReasoningResponse( ReasoningContentBlock.fromReasoningText(t -> t.text(reasoning.toString())), text.toString() ))) .onError(err -> System.err.printf("Can't invoke '%s': %s", modelId, err.getMessage())) .build(); // Step 6: Send the streaming request and process the response // - Send the request to the model // - Attach the handler to process response chunks as they arrive // - Handle any errors during streaming try { client.converseStream(request, streamHandler).get(); return finalresponse.get(); } catch (ExecutionException | InterruptedException e) { System.err.printf("Can't invoke '%s': %s", modelId, e.getCause().getMessage()); throw new RuntimeException(e); } catch (Exception e) { System.err.printf("Can't invoke '%s': %s", modelId, e.getMessage()); throw new RuntimeException(e); } } public static void main(String[] args) { reasoningStream(); } }
  • API の詳細については、「AWS SDK for Java 2.x API リファレンス」の「Converse」を参照してください。

SDK for Java 2.x
注記

GitHub には、その他のリソースもあります。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

Anthropic Claude 3.7 Sonnet の推論機能を使用して、ストリーミングテキストレスポンスを生成します。

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.ExecutionException; import java.util.concurrent.atomic.AtomicReference; /** * This example demonstrates how to use Anthropic Claude 3.7 Sonnet's reasoning * capability to generate streaming text responses. * It shows how to: * - Set up the Amazon Bedrock runtime client * - Create a message * - Configure a streaming request * - Set up a stream handler to process the response chunks * - Process the streaming response */ public class ReasoningStream { public static ReasoningResponse reasoningStream() { // 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(); // Configure the request with the message, model ID, and reasoning config ConverseStreamRequest request = ConverseStreamRequest.builder() .additionalModelRequestFields(reasoningConfig) .messages(message) .modelId(modelId) .build(); StringBuilder reasoning = new StringBuilder(); StringBuilder text = new StringBuilder(); AtomicReference<ReasoningResponse> finalresponse = new AtomicReference<>(); // Set up the stream handler to processes chunks of the response as they arrive var streamHandler = ConverseStreamResponseHandler.builder() .subscriber(ConverseStreamResponseHandler.Visitor.builder() .onContentBlockDelta(chunk -> { ContentBlockDelta delta = chunk.delta(); if (delta.reasoningContent() != null) { if (reasoning.isEmpty()) { System.out.println("\n<thinking>"); } if (delta.reasoningContent().text() != null) { System.out.print(delta.reasoningContent().text()); reasoning.append(delta.reasoningContent().text()); } } else if (delta.text() != null) { if (text.isEmpty()) { System.out.println("\n</thinking>\n"); } System.out.print(delta.text()); text.append(delta.text()); } System.out.flush(); // Ensure immediate output of each chunk }).build()) .onComplete(() -> finalresponse.set(new ReasoningResponse( ReasoningContentBlock.fromReasoningText(t -> t.text(reasoning.toString())), text.toString() ))) .onError(err -> System.err.printf("Can't invoke '%s': %s", modelId, err.getMessage())) .build(); // Step 6: Send the streaming request and process the response // - Send the request to the model // - Attach the handler to process response chunks as they arrive // - Handle any errors during streaming try { client.converseStream(request, streamHandler).get(); return finalresponse.get(); } catch (ExecutionException | InterruptedException e) { System.err.printf("Can't invoke '%s': %s", modelId, e.getCause().getMessage()); throw new RuntimeException(e); } catch (Exception e) { System.err.printf("Can't invoke '%s': %s", modelId, e.getMessage()); throw new RuntimeException(e); } } public static void main(String[] args) { reasoningStream(); } }
  • API の詳細については、「AWS SDK for Java 2.x API リファレンス」の「Converse」を参照してください。

プライバシーサイト規約Cookie の設定
© 2025, Amazon Web Services, Inc. or its affiliates.All rights reserved.