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

Amazon SDK for JavaScript V3 API 参考指南详细描述了 Amazon SDK for JavaScript 版本 3 (V3) 的所有 API 操作。

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

使用适用于 JavaScript (v3) 的 SDK 的 Amazon Bedrock 示例

以下代码示例向您展示了如何使用带有 Amazon Bedrock 的 Amazon SDK for JavaScript (v3) 来执行操作和实现常见场景。

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

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

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

开始使用

以下代码示例演示了如何开始使用 Amazon Bedrock。

适用于 JavaScript (v3) 的软件开发工具包
注意

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

// 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 的详细信息,请参阅 Amazon SDK for JavaScript API 参考ListFoundationModels中的。

主题

操作

以下代码示例演示了如何获取有关 Amazon Bedrock 基础模型的详细信息。

适用于 JavaScript (v3) 的软件开发工具包
注意

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

获取有关基础模型的详细信息。

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import { fileURLToPath } from "url"; import { BedrockClient, GetFoundationModelCommand, } from "@aws-sdk/client-bedrock"; /** * Get details about an Amazon Bedrock foundation model. * * @return {FoundationModelDetails} - The list of available bedrock foundation models. */ export const getFoundationModel = async () => { const client = new BedrockClient(); const command = new GetFoundationModelCommand({ modelIdentifier: "amazon.titan-embed-text-v1", }); const response = await client.send(command); return response.modelDetails; }; // Invoke main function if this file was run directly. if (process.argv[1] === fileURLToPath(import.meta.url)) { const model = await getFoundationModel(); console.log(model); }
  • 有关 API 的详细信息,请参阅 Amazon SDK for JavaScript API 参考GetFoundationModel中的。

以下代码示例演示了如何列出可用的 Amazon Bedrock 基础模型。

适用于 JavaScript (v3) 的软件开发工具包
注意

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

列出可用的基础模型。

// 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"; /** * List the available Amazon Bedrock foundation models. * * @return {FoundationModelSummary[]} - The list of available bedrock foundation models. */ export const listFoundationModels = async () => { const client = new BedrockClient(); const input = { // byProvider: 'STRING_VALUE', // byCustomizationType: 'FINE_TUNING' || 'CONTINUED_PRE_TRAINING', // byOutputModality: 'TEXT' || 'IMAGE' || 'EMBEDDING', // byInferenceType: 'ON_DEMAND' || 'PROVISIONED', }; const command = new ListFoundationModelsCommand(input); const response = await client.send(command); return response.modelSummaries; }; // Invoke main function if this file was run directly. if (process.argv[1] === fileURLToPath(import.meta.url)) { const models = await listFoundationModels(); console.log(models); }
  • 有关 API 的详细信息,请参阅 Amazon SDK for JavaScript API 参考ListFoundationModels中的。