Amazon Bedrock-Beispiele für die Verwendung von SDK for JavaScript (v3) - AWS SDK-Codebeispiele

Weitere AWS SDK Beispiele sind im Repo AWS Doc SDK Examples GitHub verfügbar.

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.

Amazon Bedrock-Beispiele für die Verwendung von SDK for JavaScript (v3)

Die folgenden Codebeispiele zeigen Ihnen, wie Sie mithilfe von AWS SDK for JavaScript (v3) mit Amazon Bedrock Aktionen ausführen und allgemeine Szenarien implementieren.

Aktionen sind Codeauszüge aus größeren Programmen und müssen im Kontext ausgeführt werden. Während Aktionen Ihnen zeigen, wie Sie einzelne Servicefunktionen aufrufen, können Sie Aktionen im Kontext der zugehörigen Szenarien und serviceübergreifenden Beispiele sehen.

Szenarien sind Codebeispiele, die Ihnen zeigen, wie Sie eine bestimmte Aufgabe ausführen können, indem Sie mehrere Funktionen innerhalb desselben Services aufrufen.

Jedes Beispiel enthält einen Link zu GitHub, wo Sie Anweisungen zur Einrichtung und Ausführung des Codes im Kontext finden.

Erste Schritte

Die folgenden Codebeispiele zeigen, wie Sie mit Amazon Bedrock beginnen können.

SDKfür JavaScript (v3)
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.

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

Aktionen

Das folgende Codebeispiel zeigt die VerwendungGetFoundationModel.

SDKfür JavaScript (v3)
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.

Erfahren Sie mehr über ein Gründungsmodell.

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

Das folgende Codebeispiel zeigt die VerwendungListFoundationModels.

SDKfür JavaScript (v3)
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.

Listet die verfügbaren Fundamentmodelle auf.

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