Utilisation ListAgents avec un AWS SDK ou une CLI - Amazon Bedrock

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

Utilisation ListAgents avec un AWS SDK ou une CLI

Les exemples de code suivants montrent comment utiliserListAgents.

Les exemples d’actions sont des extraits de code de programmes de plus grande envergure et doivent être exécutés en contexte. Vous pouvez voir cette action en contexte dans l’exemple de code suivant :

JavaScript
SDK pour JavaScript (v3)
Note

Il y en a plus à ce sujet GitHub. Trouvez l'exemple complet et découvrez comment le configurer et l'exécuter dans le référentiel d'exemples de code AWS.

Répertoriez les agents associés à un compte.

// 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); } }
  • Pour plus de détails sur l'API, reportez-vous ListAgentsà la section Référence des AWS SDK for JavaScript API.

Python
SDK pour Python (Boto3)
Note

Il y en a plus à ce sujet GitHub. Trouvez l'exemple complet et découvrez comment le configurer et l'exécuter dans le référentiel d'exemples de code AWS.

Répertoriez les agents associés à un compte.

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
  • Pour plus de détails sur l'API, consultez ListAgentsle AWS manuel de référence de l'API SDK for Python (Boto3).

Pour obtenir la liste complète des guides de développement du AWS SDK et des exemples de code, consultezUtilisation de ce service avec un AWS SDK. Cette rubrique comprend également des informations sur le démarrage et sur les versions précédentes de SDK.