使用 Amazon SDK for .NET 的 Amazon Bedrock 运行时系统示例 - Amazon SDK for .NET
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 Amazon SDK for .NET 的 Amazon Bedrock 运行时系统示例

以下代码示例演示如何将 Amazon SDK for .NET 与 Amazon Bedrock 运行时系统结合使用来执行操作和实现常见场景。

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景和跨服务示例的上下文查看操作。

场景是指显示如何通过在同一服务中调用多个函数来完成特定任务的代码示例。

每个示例都包含一个指向的链接 GitHub,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

操作

以下代码示例演示了如何通过在 Amazon Bedrock 上调用 AI21 Labs Jurassic-2 模型来生成文本。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

异步调用 AI21 Labs Jurassic-2 基础模型。

/// <summary> /// Asynchronously invokes the AI21 Labs Jurassic-2 model to run an inference based on the provided input. /// </summary> /// <param name="prompt">The prompt that you want Claude to complete.</param> /// <returns>The inference response from the model</returns> /// <remarks> /// The different model providers have individual request and response formats. /// For the format, ranges, and default values for AI21 Labs Jurassic-2, refer to: /// https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-jurassic2.html /// </remarks> public static async Task<string> InvokeJurassic2Async(string prompt) { string jurassic2ModelId = "ai21.j2-mid-v1"; AmazonBedrockRuntimeClient client = new(RegionEndpoint.USEast1); string payload = new JsonObject() { { "prompt", prompt }, { "maxTokens", 200 }, { "temperature", 0.5 } }.ToJsonString(); string generatedText = ""; try { InvokeModelResponse response = await client.InvokeModelAsync(new InvokeModelRequest() { ModelId = jurassic2ModelId, Body = AWSSDKUtils.GenerateMemoryStreamFromString(payload), ContentType = "application/json", Accept = "application/json" }); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { return JsonNode.ParseAsync(response.Body) .Result?["completions"]? .AsArray()[0]?["data"]? .AsObject()["text"]?.GetValue<string>() ?? ""; } else { Console.WriteLine("InvokeModelAsync failed with status code " + response.HttpStatusCode); } } catch (AmazonBedrockRuntimeException e) { Console.WriteLine(e.Message); } return generatedText; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考InvokeModel中的。

以下代码示例演示了如何通过在 Amazon Bedrock 上调用 Anthropic Claude 2 模型来生成文本。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

异步调用 Anthropic Claude 2 基础模型来生成文本。

/// <summary> /// Asynchronously invokes the Anthropic Claude 2 model to run an inference based on the provided input. /// </summary> /// <param name="prompt">The prompt that you want Claude to complete.</param> /// <returns>The inference response from the model</returns> /// <remarks> /// The different model providers have individual request and response formats. /// For the format, ranges, and default values for Anthropic Claude, refer to: /// https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html /// </remarks> public static async Task<string> InvokeClaudeAsync(string prompt) { string claudeModelId = "anthropic.claude-v2"; // Claude requires you to enclose the prompt as follows: string enclosedPrompt = "Human: " + prompt + "\n\nAssistant:"; AmazonBedrockRuntimeClient client = new(RegionEndpoint.USEast1); string payload = new JsonObject() { { "prompt", enclosedPrompt }, { "max_tokens_to_sample", 200 }, { "temperature", 0.5 }, { "stop_sequences", new JsonArray("\n\nHuman:") } }.ToJsonString(); string generatedText = ""; try { InvokeModelResponse response = await client.InvokeModelAsync(new InvokeModelRequest() { ModelId = claudeModelId, Body = AWSSDKUtils.GenerateMemoryStreamFromString(payload), ContentType = "application/json", Accept = "application/json" }); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { return JsonNode.ParseAsync(response.Body).Result?["completion"]?.GetValue<string>() ?? ""; } else { Console.WriteLine("InvokeModelAsync failed with status code " + response.HttpStatusCode); } } catch (AmazonBedrockRuntimeException e) { Console.WriteLine(e.Message); } return generatedText; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考InvokeModel中的。

以下代码示例演示了如何通过在 Amazon Bedrock 上调用 Meta Llama 2 Chat 模型来生成文本。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

异步调用 Meta Llama 2 基础模型以生成文本。

/// <summary> /// Asynchronously invokes the Meta Llama 2 Chat model to run an inference based on the provided input. /// </summary> /// <param name="prompt">The prompt that you want Llama 2 to complete.</param> /// <returns>The inference response from the model</returns> /// <remarks> /// The different model providers have individual request and response formats. /// For the format, ranges, and default values for Meta Llama 2 Chat, refer to: /// https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-meta.html /// </remarks> public static async Task<string> InvokeLlama2Async(string prompt) { string llama2ModelId = "meta.llama2-13b-chat-v1"; AmazonBedrockRuntimeClient client = new(RegionEndpoint.USEast1); string payload = new JsonObject() { { "prompt", prompt }, { "max_gen_len", 512 }, { "temperature", 0.5 }, { "top_p", 0.9 } }.ToJsonString(); string generatedText = ""; try { InvokeModelResponse response = await client.InvokeModelAsync(new InvokeModelRequest() { ModelId = llama2ModelId, Body = AWSSDKUtils.GenerateMemoryStreamFromString(payload), ContentType = "application/json", Accept = "application/json" }); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { return JsonNode.ParseAsync(response.Body) .Result?["generation"]?.GetValue<string>() ?? ""; } else { Console.WriteLine("InvokeModelAsync failed with status code " + response.HttpStatusCode); } } catch (AmazonBedrockRuntimeException e) { Console.WriteLine(e.Message); } return generatedText; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考InvokeModel中的。

场景

以下代码示例演示如何创建操场,以通过不同模态与 Amazon Bedrock 基础模型交互。

Amazon SDK for .NET

.NET Foundation Model (FM) Playground 是一个.NET MAUI Blazor 示例应用程序,演示了如何通过 C# 代码使用 Amazon Bedrock。此示例演示了 .NET 和 C# 开发人员如何使用 Amazon Bedrock 来构建生成式人工智能赋能的应用程序。您可以使用以下四个操场测试 Amazon Bedrock 基础模型并与之交互:

  • 文本操场。

  • 聊天操场。

  • 语音聊天操场。

  • 图像操场。

该示例还列出并显示了您可以访问的基础模型及其特征。有关源代码和部署说明,请参阅中的项目GitHub

本示例中使用的服务
  • Amazon Bedrock 运行时系统