使用 SDK 的 Amazon 基岩代理程式的程式碼範例 AWS - Amazon Bedrock

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

使用 SDK 的 Amazon 基岩代理程式的程式碼範例 AWS

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

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

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

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

開始使用

下列程式碼範例顯示如何開始使用 Amazon 基岩代理程式。

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 { BedrockAgentClient, GetAgentCommand, paginateListAgents, } from "@aws-sdk/client-bedrock-agent"; /** * @typedef {Object} AgentSummary */ /** * A simple scenario to demonstrate basic setup and interaction with the Bedrock Agents Client. * * This function first initializes the Amazon Bedrock Agents client for a specific region. * It then retrieves a list of existing agents using the streamlined paginator approach. * For each agent found, it retrieves detailed information using a command object. * * Demonstrates: * - Use of the Bedrock Agents client to initialize and communicate with the AWS service. * - Listing resources in a paginated response pattern. * - Accessing an individual resource using a command object. * * @returns {Promise<void>} A promise that resolves when the function has completed execution. */ export const main = async () => { const region = "us-east-1"; console.log("=".repeat(68)); console.log(`Initializing Amazon Bedrock Agents client for ${region}...`); const client = new BedrockAgentClient({ region }); console.log(`Retrieving the list of existing agents...`); const paginatorConfig = { client }; const pages = paginateListAgents(paginatorConfig, {}); /** @type {AgentSummary[]} */ const agentSummaries = []; for await (const page of pages) { agentSummaries.push(...page.agentSummaries); } console.log(`Found ${agentSummaries.length} agents in ${region}.`); if (agentSummaries.length > 0) { for (const agentSummary of agentSummaries) { const agentId = agentSummary.agentId; console.log("=".repeat(68)); console.log(`Retrieving agent with ID: ${agentId}:`); console.log("-".repeat(68)); const command = new GetAgentCommand({ agentId }); const response = await client.send(command); const agent = response.agent; console.log(` Name: ${agent.agentName}`); console.log(` Status: ${agent.agentStatus}`); console.log(` ARN: ${agent.agentArn}`); console.log(` Foundation model: ${agent.foundationModel}`); } } console.log("=".repeat(68)); }; // 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 參考》中的下列主題。