Verwenden Sie ListAgents mit einem AWS SDK oder CLI - Amazon Bedrock

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.

Verwenden Sie ListAgents mit einem AWS SDK oder CLI

Die folgenden Codebeispiele zeigen, wie man es benutztListAgents.

Beispiele für Aktionen sind Codeauszüge aus größeren Programmen und müssen im Kontext ausgeführt werden. Im folgenden Codebeispiel können Sie diese Aktion im Kontext sehen:

JavaScript
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 Agenten auf, die zu einem Konto gehören.

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import { fileURLToPath } from "url"; import { BedrockAgentClient, ListAgentsCommand, paginateListAgents, } from "@aws-sdk/client-bedrock-agent"; /** * Retrieves a list of available Amazon Bedrock agents utilizing the paginator function. * * This function leverages a paginator, which abstracts the complexity of pagination, providing * a straightforward way to handle paginated results inside a `for await...of` loop. * * @param {string} [region='us-east-1'] - The AWS region in use. * @returns {Promise<AgentSummary[]>} An array of agent summaries. */ export const listAgentsWithPaginator = async (region = "us-east-1") => { const client = new BedrockAgentClient({ region }); const paginatorConfig = { client, pageSize: 10, // optional, added for demonstration purposes }; const pages = paginateListAgents(paginatorConfig, {}); // Paginate until there are no more results const agentSummaries = []; for await (const page of pages) { agentSummaries.push(...page.agentSummaries); } return agentSummaries; }; /** * Retrieves a list of available Amazon Bedrock agents utilizing the ListAgentsCommand. * * This function demonstrates the manual approach, sending a command to the client and processing the response. * Pagination must manually be managed. For a simplified approach that abstracts away pagination logic, see * the `listAgentsWithPaginator()` example below. * * @param {string} [region='us-east-1'] - The AWS region in use. * @returns {Promise<AgentSummary[]>} An array of agent summaries. */ export const listAgentsWithCommandObject = async (region = "us-east-1") => { const client = new BedrockAgentClient({ region }); let nextToken; const agentSummaries = []; do { const command = new ListAgentsCommand({ nextToken, maxResults: 10, // optional, added for demonstration purposes }); /** @type {{agentSummaries: AgentSummary[], nextToken?: string}} */ const paginatedResponse = await client.send(command); agentSummaries.push(...(paginatedResponse.agentSummaries || [])); nextToken = paginatedResponse.nextToken; } while (nextToken); return agentSummaries; }; // Invoke main function if this file was run directly. if (process.argv[1] === fileURLToPath(import.meta.url)) { console.log("=".repeat(68)); console.log("Listing agents using ListAgentsCommand:"); for (const agent of await listAgentsWithCommandObject()) { console.log(agent); } console.log("=".repeat(68)); console.log("Listing agents using the paginateListAgents function:"); for (const agent of await listAgentsWithPaginator()) { console.log(agent); } }
  • APIEinzelheiten finden Sie ListAgentsunter AWS SDK for JavaScript APIReferenz.

Python
SDKfür Python (Boto3)
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 Agenten auf, die zu einem Konto gehören.

def list_agents(self): """ List the available Amazon Bedrock Agents. :return: The list of available bedrock agents. """ try: all_agents = [] paginator = self.client.get_paginator("list_agents") for page in paginator.paginate(PaginationConfig={"PageSize": 10}): all_agents.extend(page["agentSummaries"]) except ClientError as e: logger.error(f"Couldn't list agents. {e}") raise else: return all_agents
  • APIEinzelheiten finden Sie unter ListAgentsPython (Boto3) API -Referenz.AWS SDK

Eine vollständige Liste der AWS SDK Entwicklerhandbücher und Codebeispiele finden Sie unter. Nutzung dieses Dienstes mit einem AWS SDK Dieses Thema enthält auch Informationen zu den ersten Schritten und Details zu früheren SDK Versionen.