Seleccione sus preferencias de cookies

Usamos cookies esenciales y herramientas similares que son necesarias para proporcionar nuestro sitio y nuestros servicios. Usamos cookies de rendimiento para recopilar estadísticas anónimas para que podamos entender cómo los clientes usan nuestro sitio y hacer mejoras. Las cookies esenciales no se pueden desactivar, pero puede hacer clic en “Personalizar” o “Rechazar” para rechazar las cookies de rendimiento.

Si está de acuerdo, AWS y los terceros aprobados también utilizarán cookies para proporcionar características útiles del sitio, recordar sus preferencias y mostrar contenido relevante, incluida publicidad relevante. Para aceptar o rechazar todas las cookies no esenciales, haga clic en “Aceptar” o “Rechazar”. Para elegir opciones más detalladas, haga clic en “Personalizar”.

Invoque Amazon Nova en Amazon Bedrock mediante la API Converse de Bedrock con un flujo de respuesta

Modo de enfoque
Invoque Amazon Nova en Amazon Bedrock mediante la API Converse de Bedrock con un flujo de respuesta - AWS Ejemplos de código de SDK

Hay más ejemplos de AWS SDK disponibles en el GitHub repositorio de ejemplos de AWS Doc SDK.

Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.

Hay más ejemplos de AWS SDK disponibles en el GitHub repositorio de ejemplos de AWS Doc SDK.

Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.

Los siguientes ejemplos de código muestran cómo enviar un mensaje de texto a Amazon Nova mediante la API Converse de Bedrock y procesar el flujo de respuestas en tiempo real.

.NET
SDK para .NET
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

Envíe un mensaje de texto a Amazon Nova mediante la API Converse de Bedrock y procese el flujo de respuestas en tiempo real.

// Use the Converse API to send a text message to Amazon Nova // and print the response stream. using System; using System.Collections.Generic; using System.Linq; using Amazon; using Amazon.BedrockRuntime; using Amazon.BedrockRuntime.Model; // Create a Bedrock Runtime client in the AWS Region you want to use. var client = new AmazonBedrockRuntimeClient(RegionEndpoint.USEast1); // Set the model ID, e.g., Amazon Nova Lite. var modelId = "amazon.nova-lite-v1:0"; // Define the user message. var userMessage = "Describe the purpose of a 'hello world' program in one line."; // Create a request with the model ID, the user message, and an inference configuration. var request = new ConverseStreamRequest { ModelId = modelId, Messages = new List<Message> { new Message { Role = ConversationRole.User, Content = new List<ContentBlock> { new ContentBlock { Text = userMessage } } } }, InferenceConfig = new InferenceConfiguration() { MaxTokens = 512, Temperature = 0.5F, TopP = 0.9F } }; try { // Send the request to the Bedrock Runtime and wait for the result. var response = await client.ConverseStreamAsync(request); // Extract and print the streamed response text in real-time. foreach (var chunk in response.Stream.AsEnumerable()) { if (chunk is ContentBlockDeltaEvent) { Console.Write((chunk as ContentBlockDeltaEvent).Delta.Text); } } } catch (AmazonBedrockRuntimeException e) { Console.WriteLine($"ERROR: Can't invoke '{modelId}'. Reason: {e.Message}"); throw; }
  • Para obtener más información sobre la API, consulte la Referencia de ConverseStreamla AWS SDK para .NET API.

Java
SDK para Java 2.x
nota

Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

Envíe un mensaje de texto a Amazon Nova mediante la API Converse de Bedrock y procese el flujo de respuestas en tiempo real.

import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; 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; /** * This example demonstrates how to use the Amazon Nova foundation models with an * asynchronous Amazon Bedrock runtime client 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 ConverseStream { public static void converseStream() { // Step 1: Create the Amazon Bedrock runtime client // The runtime client handles the communication with AI models on Amazon Bedrock BedrockRuntimeAsyncClient client = BedrockRuntimeAsyncClient.builder() .credentialsProvider(DefaultCredentialsProvider.create()) .region(Region.US_EAST_1) .build(); // Step 2: Specify which model to use // Available Amazon Nova models and their characteristics: // - Amazon Nova Micro: Text-only model optimized for lowest latency and cost // - Amazon Nova Lite: Fast, low-cost multimodal model for image, video, and text // - Amazon Nova Pro: Advanced multimodal model balancing accuracy, speed, and cost // // For the latest available models, see: // https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html String modelId = "amazon.nova-lite-v1:0"; // Step 3: Create the message // The message includes the text prompt and specifies that it comes from the user var inputText = "Describe the purpose of a 'hello world' program in one paragraph"; var message = Message.builder() .content(ContentBlock.fromText(inputText)) .role(ConversationRole.USER) .build(); // Step 4: Configure the request // Optional parameters to control the model's response: // - maxTokens: maximum number of tokens to generate // - temperature: randomness (max: 1.0, default: 0.7) // OR // - topP: diversity of word choice (max: 1.0, default: 0.9) // Note: Use either temperature OR topP, but not both ConverseStreamRequest request = ConverseStreamRequest.builder() .modelId(modelId) .messages(message) .inferenceConfig(config -> config .maxTokens(500) // The maximum response length .temperature(0.5F) // Using temperature for randomness control //.topP(0.9F) // Alternative: use topP instead of temperature ).build(); // Step 5: Set up the stream handler // The stream handler processes chunks of the response as they arrive // - onContentBlockDelta: Processes each text chunk // - onError: Handles any errors during streaming var streamHandler = ConverseStreamResponseHandler.builder() .subscriber(ConverseStreamResponseHandler.Visitor.builder() .onContentBlockDelta(chunk -> { System.out.print(chunk.delta().text()); System.out.flush(); // Ensure immediate output of each chunk }).build()) .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(); } catch (ExecutionException | InterruptedException e) { System.err.printf("Can't invoke '%s': %s", modelId, e.getCause().getMessage()); } } public static void main(String[] args) { converseStream(); } }
  • Para obtener más información sobre la API, consulte la referencia de ConverseStreamla AWS SDK for Java 2.x API.

JavaScript
SDK para JavaScript (v3)
nota

Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

Envíe un mensaje de texto a Amazon Nova mediante la API Converse de Bedrock y procese el flujo de respuestas en tiempo real.

// This example demonstrates how to use the Amazon Nova foundation models // to generate streaming text responses. // It shows how to: // - Set up the Amazon Bedrock runtime client // - Create a message // - Configure a streaming request // - Process the streaming response import { BedrockRuntimeClient, ConversationRole, ConverseStreamCommand, } from "@aws-sdk/client-bedrock-runtime"; // Step 1: Create the Amazon Bedrock runtime client // Credentials will be automatically loaded from the environment const client = new BedrockRuntimeClient({ region: "us-east-1" }); // Step 2: Specify which model to use // Available Amazon Nova models and their characteristics: // - Amazon Nova Micro: Text-only model optimized for lowest latency and cost // - Amazon Nova Lite: Fast, low-cost multimodal model for image, video, and text // - Amazon Nova Pro: Advanced multimodal model balancing accuracy, speed, and cost // // For the most current model IDs, see: // https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html const modelId = "amazon.nova-lite-v1:0"; // Step 3: Create the message // The message includes the text prompt and specifies that it comes from the user const inputText = "Describe the purpose of a 'hello world' program in one paragraph"; const message = { content: [{ text: inputText }], role: ConversationRole.USER, }; // Step 4: Configure the streaming request // Optional parameters to control the model's response: // - maxTokens: maximum number of tokens to generate // - temperature: randomness (max: 1.0, default: 0.7) // OR // - topP: diversity of word choice (max: 1.0, default: 0.9) // Note: Use either temperature OR topP, but not both const request = { modelId, messages: [message], inferenceConfig: { maxTokens: 500, // The maximum response length temperature: 0.5, // Using temperature for randomness control //topP: 0.9, // Alternative: use topP instead of temperature }, }; // Step 5: Send and process the streaming request // - Send the request to the model // - Process each chunk of the streaming response try { const response = await client.send(new ConverseStreamCommand(request)); for await (const chunk of response.stream) { if (chunk.contentBlockDelta) { // Print each text chunk as it arrives process.stdout.write(chunk.contentBlockDelta.delta?.text || ""); } } } catch (error) { console.error(`ERROR: Can't invoke '${modelId}'. Reason: ${error.message}`); process.exitCode = 1; }
  • Para obtener más información sobre la API, consulte la referencia de ConverseStreamla AWS SDK para JavaScript API.

Kotlin
SDK para Kotlin
nota

Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

Envíe un mensaje de texto a Amazon Nova mediante la API Converse de Bedrock y procese el flujo de respuestas en tiempo real.

import aws.sdk.kotlin.services.bedrockruntime.BedrockRuntimeClient import aws.sdk.kotlin.services.bedrockruntime.model.ContentBlock import aws.sdk.kotlin.services.bedrockruntime.model.ConversationRole import aws.sdk.kotlin.services.bedrockruntime.model.ConverseStreamOutput import aws.sdk.kotlin.services.bedrockruntime.model.ConverseStreamRequest import aws.sdk.kotlin.services.bedrockruntime.model.Message /** * This example demonstrates how to use the Amazon Nova foundation models * to generate streaming text responses. * It shows how to: * - Set up the Amazon Bedrock runtime client * - Create a message with a prompt * - Configure a streaming request with parameters * - Process the response stream in real time */ suspend fun main() { converseStream() } suspend fun converseStream(): String { // A buffer to collect the complete response val completeResponseBuffer = StringBuilder() // Create and configure the Bedrock runtime client BedrockRuntimeClient { region = "us-east-1" }.use { client -> // Specify the model ID. For the latest available models, see: // https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html val modelId = "amazon.nova-lite-v1:0" // Create the message with the user's prompt val prompt = "Describe the purpose of a 'hello world' program in a paragraph." val message = Message { role = ConversationRole.User content = listOf(ContentBlock.Text(prompt)) } // Configure the request with optional model parameters val request = ConverseStreamRequest { this.modelId = modelId messages = listOf(message) inferenceConfig { maxTokens = 500 // Maximum response length temperature = 0.5F // Lower values: more focused output // topP = 0.8F // Alternative to temperature } } // Process the streaming response runCatching { client.converseStream(request) { response -> response.stream?.collect { chunk -> when (chunk) { is ConverseStreamOutput.ContentBlockDelta -> { // Process each text chunk as it arrives chunk.value.delta?.asText()?.let { text -> print(text) System.out.flush() // Ensure immediate output completeResponseBuffer.append(text) } } else -> {} // Other output block types can be handled as needed } } } }.onFailure { error -> error.message?.let { e -> System.err.println("ERROR: Can't invoke '$modelId'. Reason: $e") } throw RuntimeException("Failed to generate text with model $modelId: $error", error) } } return completeResponseBuffer.toString() }
  • Para obtener más información sobre la API, consulte la referencia sobre la API de Kotlin ConverseStreamen el AWS SDK.

Python
SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

Envíe un mensaje de texto a Amazon Nova mediante la API Converse de Bedrock y procese el flujo de respuestas en tiempo real.

# Use the Conversation API to send a text message to Amazon Nova Text # and print the response stream. import boto3 from botocore.exceptions import ClientError # Create a Bedrock Runtime client in the AWS Region you want to use. client = boto3.client("bedrock-runtime", region_name="us-east-1") # Set the model ID, e.g., Amazon Nova Lite. model_id = "amazon.nova-lite-v1:0" # Start a conversation with the user message. user_message = "Describe the purpose of a 'hello world' program in one line." conversation = [ { "role": "user", "content": [{"text": user_message}], } ] try: # Send the message to the model, using a basic inference configuration. streaming_response = client.converse_stream( modelId=model_id, messages=conversation, inferenceConfig={"maxTokens": 512, "temperature": 0.5, "topP": 0.9}, ) # Extract and print the streamed response text in real-time. for chunk in streaming_response["stream"]: if "contentBlockDelta" in chunk: text = chunk["contentBlockDelta"]["delta"]["text"] print(text, end="") except (ClientError, Exception) as e: print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}") exit(1)
  • Para obtener más información sobre la API, consulta ConverseStreamla AWS Referencia de API de SDK for Python (Boto3).

SDK para .NET
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

Envíe un mensaje de texto a Amazon Nova mediante la API Converse de Bedrock y procese el flujo de respuestas en tiempo real.

// Use the Converse API to send a text message to Amazon Nova // and print the response stream. using System; using System.Collections.Generic; using System.Linq; using Amazon; using Amazon.BedrockRuntime; using Amazon.BedrockRuntime.Model; // Create a Bedrock Runtime client in the AWS Region you want to use. var client = new AmazonBedrockRuntimeClient(RegionEndpoint.USEast1); // Set the model ID, e.g., Amazon Nova Lite. var modelId = "amazon.nova-lite-v1:0"; // Define the user message. var userMessage = "Describe the purpose of a 'hello world' program in one line."; // Create a request with the model ID, the user message, and an inference configuration. var request = new ConverseStreamRequest { ModelId = modelId, Messages = new List<Message> { new Message { Role = ConversationRole.User, Content = new List<ContentBlock> { new ContentBlock { Text = userMessage } } } }, InferenceConfig = new InferenceConfiguration() { MaxTokens = 512, Temperature = 0.5F, TopP = 0.9F } }; try { // Send the request to the Bedrock Runtime and wait for the result. var response = await client.ConverseStreamAsync(request); // Extract and print the streamed response text in real-time. foreach (var chunk in response.Stream.AsEnumerable()) { if (chunk is ContentBlockDeltaEvent) { Console.Write((chunk as ContentBlockDeltaEvent).Delta.Text); } } } catch (AmazonBedrockRuntimeException e) { Console.WriteLine($"ERROR: Can't invoke '{modelId}'. Reason: {e.Message}"); throw; }
  • Para obtener más información sobre la API, consulte la Referencia de ConverseStreamla AWS SDK para .NET API.

PrivacidadTérminos del sitioPreferencias de cookies
© 2025, Amazon Web Services, Inc o sus afiliados. Todos los derechos reservados.