Selecione suas preferências de cookies

Usamos cookies essenciais e ferramentas semelhantes que são necessárias para fornecer nosso site e serviços. Usamos cookies de desempenho para coletar estatísticas anônimas, para que possamos entender como os clientes usam nosso site e fazer as devidas melhorias. Cookies essenciais não podem ser desativados, mas você pode clicar em “Personalizar” ou “Recusar” para recusar cookies de desempenho.

Se você concordar, a AWS e terceiros aprovados também usarão cookies para fornecer recursos úteis do site, lembrar suas preferências e exibir conteúdo relevante, incluindo publicidade relevante. Para aceitar ou recusar todos os cookies não essenciais, clique em “Aceitar” ou “Recusar”. Para fazer escolhas mais detalhadas, clique em “Personalizar”.

Use a capacidade de raciocínio do Anthropic Claude 3.7 Sonnet no Amazon Bedrock - AWS Exemplos de código do SDK

Há mais exemplos de AWS SDK disponíveis no repositório AWS Doc SDK Examples GitHub .

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

Há mais exemplos de AWS SDK disponíveis no repositório AWS Doc SDK Examples GitHub .

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

Use a capacidade de raciocínio do Anthropic Claude 3.7 Sonnet no Amazon Bedrock

O exemplo de código a seguir mostra como usar a capacidade de raciocínio do Anthropic Claude 3.7 Sonnet no Amazon Bedrock

Java
SDK para Java 2.x
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

Use a capacidade de raciocínio do Anthropic Claude 3.7 Sonnet para gerar respostas de streaming de texto.

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(); } }
  • Para obter detalhes da API, consulte Converse na Referência da API do AWS SDK for Java 2.x .

SDK para Java 2.x
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

Use a capacidade de raciocínio do Anthropic Claude 3.7 Sonnet para gerar respostas de streaming de texto.

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(); } }
  • Para obter detalhes da API, consulte Converse na Referência da API do AWS SDK for Java 2.x .

PrivacidadeTermos do sitePreferências de cookies
© 2025, Amazon Web Services, Inc. ou suas afiliadas. Todos os direitos reservados.