AWS SDKsコード例 - Amazon Bedrock

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

AWS SDKsコード例

次のコード例は、 AWS Software Development Kit (SDK) で Agents for Amazon Bedrock を使用する方法を示しています。

アクションはより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、関連するシナリオやサービス間の例ではアクションのコンテキストが確認できます。

「シナリオ」は、同じサービス内で複数の関数を呼び出して、特定のタスクを実行する方法を示すコード例です。

AWS SDK デベロッパーガイドとコード例の完全なリストについては、「」を参照してくださいAWS SDK でこのサービスを使用する。このトピックには、使用開始方法に関する情報と、以前の SDK バージョンの詳細も含まれています。

開始方法

次のコード例は、Agents for Amazon Bedrock の使用を開始する方法を示しています。

JavaScript
SDK for 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 リファレンス』の以下のトピックを参照してください。