Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.
Die folgenden Codebeispiele zeigen, wie Sie mithilfe der Converse-API von Bedrock eine Textnachricht an Amazon Nova senden.
- SDK for .NET
-
Anmerkung
Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository
einrichten und ausführen. Senden Sie mithilfe der Converse-API von Bedrock eine Textnachricht an Amazon Nova.
// Use the Converse API to send a text message to Amazon Nova. using System; using System.Collections.Generic; 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 ConverseRequest { 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.ConverseAsync(request); // Extract and print the response text. string responseText = response?.Output?.Message?.Content?[0]?.Text ?? ""; Console.WriteLine(responseText); } catch (AmazonBedrockRuntimeException e) { Console.WriteLine($"ERROR: Can't invoke '{modelId}'. Reason: {e.Message}"); throw; }
Senden Sie mithilfe der Converse-API von Bedrock mit einer Toolkonfiguration eine Konversation mit Nachrichten an Amazon Nova.
/// <summary> /// Wrapper class for interacting with the Amazon Bedrock Converse API. /// </summary> public class BedrockActionsWrapper { private readonly IAmazonBedrockRuntime _bedrockClient; private readonly ILogger<BedrockActionsWrapper> _logger; /// <summary> /// Initializes a new instance of the <see cref="BedrockActionsWrapper"/> class. /// </summary> /// <param name="bedrockClient">The Bedrock Converse API client.</param> /// <param name="logger">The logger instance.</param> public BedrockActionsWrapper(IAmazonBedrockRuntime bedrockClient, ILogger<BedrockActionsWrapper> logger) { _bedrockClient = bedrockClient; _logger = logger; } /// <summary> /// Sends a Converse request to the Amazon Bedrock Converse API. /// </summary> /// <param name="modelId">The Bedrock Model Id.</param> /// <param name="systemPrompt">A system prompt instruction.</param> /// <param name="conversation">The array of messages in the conversation.</param> /// <param name="toolSpec">The specification for a tool.</param> /// <returns>The response of the model.</returns> public async Task<ConverseResponse> SendConverseRequestAsync(string modelId, string systemPrompt, List<Message> conversation, ToolSpecification toolSpec) { try { var request = new ConverseRequest() { ModelId = modelId, System = new List<SystemContentBlock>() { new SystemContentBlock() { Text = systemPrompt } }, Messages = conversation, ToolConfig = new ToolConfiguration() { Tools = new List<Tool>() { new Tool() { ToolSpec = toolSpec } } } }; var response = await _bedrockClient.ConverseAsync(request); return response; } catch (ModelNotReadyException ex) { _logger.LogError(ex, "Model not ready, please wait and try again."); throw; } catch (AmazonBedrockRuntimeException ex) { _logger.LogError(ex, "Error occurred while sending Converse request."); throw; } } }
-
Einzelheiten zur API finden Sie unter Converse in AWS SDK for .NET der API-Referenz.
-
Eine vollständige Liste der AWS SDK-Entwicklerhandbücher und Codebeispiele finden Sie unter. Amazon Bedrock mit einem AWS SDK verwenden Dieses Thema enthält auch Informationen zu den ersten Schritten und Details zu früheren SDK-Versionen.