Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.
Ejemplos de código para agentes de Amazon Bedrock que utilizan AWS SDKs
Los siguientes ejemplos de código muestran cómo utilizar Amazon Bedrock Agents con un kit de desarrollo de AWS software (SDK).
Las acciones son extractos de código de programas más grandes y deben ejecutarse en contexto. Mientras las acciones muestran cómo llamar a las distintas funciones de servicio, es posible ver las acciones en contexto en los escenarios relacionados.
Los escenarios son ejemplos de código que muestran cómo llevar a cabo una tarea específica a través de llamadas a varias funciones dentro del servicio o combinado con otros Servicios de AWS.
Para obtener una lista completa de guías para desarrolladores del AWS SDK y ejemplos de código, consulteUso de Amazon Bedrock con un SDK AWS. En este tema también se incluye información sobre cómo comenzar a utilizar el SDK y detalles sobre sus versiones anteriores.
Introducción
En el siguiente ejemplo de código se muestra cómo empezar a utilizar Agentes de Amazon Bedrock.
- JavaScript
-
- SDK para JavaScript (v3)
-
import { fileURLToPath } from "node: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();
}