本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
以下代码示例展示了如何使用 Bedrock 的 Converse API 向 Amazon Nova 发送短信并实时处理响应流。
- .NET
-
- SDK for .NET
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 使用 Bedrock 的 Converse API 向 Amazon Nova 发送短信,并实时处理响应流。
// 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; }
-
有关 API 的详细信息,请参阅 AWS SDK for .NET API 参考ConverseStream中的。
-
- Java
-
- 适用于 Java 的 SDK 2.x
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 使用 Bedrock 的 Converse API 向 Amazon Nova 发送短信,并实时处理响应流。
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(); } }
-
有关 API 的详细信息,请参阅 AWS SDK for Java 2.x API 参考ConverseStream中的。
-
- JavaScript
-
- 适用于 JavaScript (v3) 的软件开发工具包
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 使用 Bedrock 的 Converse API 向 Amazon Nova 发送短信,并实时处理响应流。
// 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; }
-
有关 API 的详细信息,请参阅 AWS SDK for JavaScript API 参考ConverseStream中的。
-
- Kotlin
-
- 适用于 Kotlin 的 SDK
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 使用 Bedrock 的 Converse API 向 Amazon Nova 发送短信,并实时处理响应流。
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() }
-
有关 API 的详细信息,请参阅适用ConverseStream
于 K otlin 的AWS SDK API 参考。
-
- Python
-
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 使用 Bedrock 的 Converse API 向 Amazon Nova 发送短信,并实时处理响应流。
# 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)
-
有关 API 的详细信息,请参阅适用ConverseStream于 Python 的AWS SDK (Boto3) API 参考。
-
- SDK for .NET
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 使用 Bedrock 的 Converse API 向 Amazon Nova 发送短信,并实时处理响应流。
// 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; }
-
有关 API 的详细信息,请参阅 AWS SDK for .NET API 参考ConverseStream中的。
-
有关 S AWS DK 开发者指南和代码示例的完整列表,请参阅将 Amazon Bedrock 与 SD AWS K 配合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。