使用 SDK 的 Amazon 基岩的代碼示例 AWS - Amazon Bedrock

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 SDK 的 Amazon 基岩的代碼示例 AWS

下列程式碼範例說明如何搭配 AWS 軟體開發套件 (SDK) 使用 Amazon 基岩。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境和跨服務範例中查看內容中的動作。

Scenarios (案例) 是向您展示如何呼叫相同服務中的多個函數來完成特定任務的程式碼範例。

如需 AWS SDK 開發人員指南和程式碼範例的完整清單,請參閱搭配 AWS SDK 使用此服務。此主題也包含入門相關資訊和舊版 SDK 的詳細資訊。

開始使用

下列程式碼範例說明如何開始使用 Amazon 基岩。

.NET
AWS SDK for .NET
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

using Amazon; using Amazon.Bedrock; using Amazon.Bedrock.Model; namespace ListFoundationModelsExample { /// <summary> /// This example shows how to list foundation models. /// </summary> internal class HelloBedrock { /// <summary> /// Main method to call the ListFoundationModelsAsync method. /// </summary> /// <param name="args"> The command line arguments. </param> static async Task Main(string[] args) { // Specify a region endpoint where Amazon Bedrock is available. For a list of supported region see https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html#bedrock-regions AmazonBedrockClient bedrockClient = new(RegionEndpoint.USWest2); await ListFoundationModelsAsync(bedrockClient); } /// <summary> /// List foundation models. /// </summary> /// <param name="bedrockClient"> The Amazon Bedrock client. </param> private static async Task ListFoundationModelsAsync(AmazonBedrockClient bedrockClient) { Console.WriteLine("List foundation models with no filter"); try { ListFoundationModelsResponse response = await bedrockClient.ListFoundationModelsAsync(new ListFoundationModelsRequest() { }); if (response?.HttpStatusCode == System.Net.HttpStatusCode.OK) { foreach (var fm in response.ModelSummaries) { WriteToConsole(fm); } } else { Console.WriteLine("Something wrong happened"); } } catch (AmazonBedrockException e) { Console.WriteLine(e.Message); } } /// <summary> /// Write the foundation model summary to console. /// </summary> /// <param name="foundationModel"> The foundation model summary to write to console. </param> private static void WriteToConsole(FoundationModelSummary foundationModel) { Console.WriteLine($"{foundationModel.ModelId}, Customization: {String.Join(", ", foundationModel.CustomizationsSupported)}, Stream: {foundationModel.ResponseStreamingSupported}, Input: {String.Join(", ", foundationModel.InputModalities)}, Output: {String.Join(", ", foundationModel.OutputModalities)}"); } } }
Go
SDK for Go V2
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

package main import ( "context" "fmt" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/bedrock" ) const region = "us-east-1" // main uses the AWS SDK for Go (v2) to create an Amazon Bedrock client and // list the available foundation models in your account and the chosen region. // This example uses the default settings specified in your shared credentials // and config files. func main() { sdkConfig, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region)) if err != nil { fmt.Println("Couldn't load default configuration. Have you set up your AWS account?") fmt.Println(err) return } bedrockClient := bedrock.NewFromConfig(sdkConfig) result, err := bedrockClient.ListFoundationModels(context.TODO(), &bedrock.ListFoundationModelsInput{}) if err != nil { fmt.Printf("Couldn't list foundation models. Here's why: %v\n", err) return } if len(result.ModelSummaries) == 0 { fmt.Println("There are no foundation models.")} for _, modelSummary := range result.ModelSummaries { fmt.Println(*modelSummary.ModelId) } }
JavaScript
適用於 JavaScript (v3) 的開發套件
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import { fileURLToPath } from "url"; import { BedrockClient, ListFoundationModelsCommand, } from "@aws-sdk/client-bedrock"; const REGION = "us-east-1"; const client = new BedrockClient({ region: REGION }); export const main = async () => { const command = new ListFoundationModelsCommand({}); const response = await client.send(command); const models = response.modelSummaries; console.log("Listing the available Bedrock foundation models:"); for (let model of models) { console.log("=".repeat(42)); console.log(` Model: ${model.modelId}`); console.log("-".repeat(42)); console.log(` Name: ${model.modelName}`); console.log(` Provider: ${model.providerName}`); console.log(` Model ARN: ${model.modelArn}`); console.log(` Input modalities: ${model.inputModalities}`); console.log(` Output modalities: ${model.outputModalities}`); console.log(` Supported customizations: ${model.customizationsSupported}`); console.log(` Supported inference types: ${model.inferenceTypesSupported}`); console.log(` Lifecycle status: ${model.modelLifecycle.status}`); console.log("=".repeat(42) + "\n"); } const active = models.filter( (m) => m.modelLifecycle.status === "ACTIVE", ).length; const legacy = models.filter( (m) => m.modelLifecycle.status === "LEGACY", ).length; console.log( `There are ${active} active and ${legacy} legacy foundation models in ${REGION}.`, ); return response; }; // Invoke main function if this file was run directly. if (process.argv[1] === fileURLToPath(import.meta.url)) { await main(); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for JavaScript API 參考ListFoundationModels中的。