搭CreateAgent配 AWS 開發套件或 CLI 使用 - Amazon Bedrock

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

CreateAgent配 AWS 開發套件或 CLI 使用

下列程式碼範例會示範如何使用CreateAgent

動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:

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 { checkForPlaceholders } from "../lib/utils.js"; import { BedrockAgentClient, CreateAgentCommand, } from "@aws-sdk/client-bedrock-agent"; /** * Creates an Amazon Bedrock Agent. * * @param {string} agentName - A name for the agent that you create. * @param {string} foundationModel - The foundation model to be used by the agent you create. * @param {string} agentResourceRoleArn - The ARN of the IAM role with permissions required by the agent. * @param {string} [region='us-east-1'] - The AWS region in use. * @returns {Promise<import("@aws-sdk/client-bedrock-agent").Agent>} An object containing details of the created agent. */ export const createAgent = async ( agentName, foundationModel, agentResourceRoleArn, region = "us-east-1", ) => { const client = new BedrockAgentClient({ region }); const command = new CreateAgentCommand({ agentName, foundationModel, agentResourceRoleArn, }); const response = await client.send(command); return response.agent; }; // Invoke main function if this file was run directly. if (process.argv[1] === fileURLToPath(import.meta.url)) { // Replace the placeholders for agentName and accountId, and roleName with a unique name for the new agent, // the id of your AWS account, and the name of an existing execution role that the agent can use inside your account. // For foundationModel, specify the desired model. Ensure to remove the brackets '[]' before adding your data. // A string (max 100 chars) that can include letters, numbers, dashes '-', and underscores '_'. const agentName = "[your-bedrock-agent-name]"; // Your AWS account id. const accountId = "[123456789012]"; // The name of the agent's execution role. It must be prefixed by `AmazonBedrockExecutionRoleForAgents_`. const roleName = "[AmazonBedrockExecutionRoleForAgents_your-role-name]"; // The ARN for the agent's execution role. // Follow the ARN format: 'arn:aws:iam::account-id:role/role-name' const roleArn = `arn:aws:iam::${accountId}:role/${roleName}`; // Specify the model for the agent. Change if a different model is preferred. const foundationModel = "anthropic.claude-v2"; // Check for unresolved placeholders in agentName and roleArn. checkForPlaceholders([agentName, roleArn]); console.log(`Creating a new agent...`); const agent = await createAgent(agentName, foundationModel, roleArn); console.log(agent); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for JavaScript API 參考CreateAgent中的。

Python
適用於 Python (Boto3) 的 SDK
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

建立 代理程式。

def create_agent(self, agent_name, foundation_model, role_arn, instruction): """ Creates an agent that orchestrates interactions between foundation models, data sources, software applications, user conversations, and APIs to carry out tasks to help customers. :param agent_name: A name for the agent. :param foundation_model: The foundation model to be used for orchestration by the agent. :param role_arn: The ARN of the IAM role with permissions needed by the agent. :param instruction: Instructions that tell the agent what it should do and how it should interact with users. :return: The response from Agents for Bedrock if successful, otherwise raises an exception. """ try: response = self.client.create_agent( agentName=agent_name, foundationModel=foundation_model, agentResourceRoleArn=role_arn, instruction=instruction, ) except ClientError as e: logger.error(f"Error: Couldn't create agent. Here's why: {e}") raise else: return response["agent"]
  • 如需 API 的詳細資訊,請參閱AWS 開發套件CreateAgent中的 Python (博托 3) API 參考。

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