Amazon Bedrock examples using SDK for JavaScript (v3) - Amazon SDK for JavaScript
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

The Amazon SDK for JavaScript V3 API Reference Guide describes in detail all the API operations for the Amazon SDK for JavaScript version 3 (V3).

Amazon Bedrock examples using SDK for JavaScript (v3)

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for JavaScript (v3) with Amazon Bedrock.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios and cross-service examples.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Get started

The following code examples show how to get started using Amazon Bedrock.

SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

// 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(); }
Topics

Actions

The following code example shows how to use GetFoundationModel.

SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Get details about a foundation model.

// 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); }

The following code example shows how to use ListFoundationModels.

SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

List the available foundation models.

// 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); }