AWS SDK または CLI InvokeAgentで を使用する - Amazon Bedrock

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

AWS SDK または CLI InvokeAgentで を使用する

以下のコード例は、InvokeAgent の使用方法を示しています。

JavaScript
SDK for JavaScript (v3)
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import { BedrockAgentRuntimeClient, InvokeAgentCommand, } from "@aws-sdk/client-bedrock-agent-runtime"; /** * @typedef {Object} ResponseBody * @property {string} completion */ /** * Invokes a Bedrock agent to run an inference using the input * provided in the request body. * * @param {string} prompt - The prompt that you want the Agent to complete. * @param {string} sessionId - An arbitrary identifier for the session. */ export const invokeBedrockAgent = async (prompt, sessionId) => { const client = new BedrockAgentRuntimeClient({ region: "us-east-1" }); // const client = new BedrockAgentRuntimeClient({ // region: "us-east-1", // credentials: { // accessKeyId: "accessKeyId", // permission to invoke agent // secretAccessKey: "accessKeySecret", // }, // }); const agentId = "AJBHXXILZN"; const agentAliasId = "AVKP1ITZAA"; const command = new InvokeAgentCommand({ agentId, agentAliasId, sessionId, inputText: prompt, }); try { let completion = ""; const response = await client.send(command); if (response.completion === undefined) { throw new Error("Completion is undefined"); } for await (let chunkEvent of response.completion) { const chunk = chunkEvent.chunk; console.log(chunk); const decodedResponse = new TextDecoder("utf-8").decode(chunk.bytes); completion += decodedResponse; } return { sessionId: sessionId, completion }; } catch (err) { console.error(err); } }; // Call function if run directly import { fileURLToPath } from "url"; if (process.argv[1] === fileURLToPath(import.meta.url)) { const result = await invokeBedrockAgent("I need help.", "123"); console.log(result); }
  • API の詳細については、「 API リファレンスInvokeAgent」の「」を参照してください。 AWS SDK for JavaScript

Python
SDK for Python (Boto3)
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

エージェントを呼び出します。

def invoke_agent(self, agent_id, agent_alias_id, session_id, prompt): """ Sends a prompt for the agent to process and respond to. :param agent_id: The unique identifier of the agent to use. :param agent_alias_id: The alias of the agent to use. :param session_id: The unique identifier of the session. Use the same value across requests to continue the same conversation. :param prompt: The prompt that you want Claude to complete. :return: Inference response from the model. """ try: # Note: The execution time depends on the foundation model, complexity of the agent, # and the length of the prompt. In some cases, it can take up to a minute or more to # generate a response. response = self.agents_runtime_client.invoke_agent( agentId=agent_id, agentAliasId=agent_alias_id, sessionId=session_id, inputText=prompt, ) completion = "" for event in response.get("completion"): chunk = event["chunk"] completion = completion + chunk["bytes"].decode() except ClientError as e: logger.error(f"Couldn't invoke agent. {e}") raise return completion
  • API の詳細については、 InvokeAgent AWS SDK for Python (Boto3) API リファレンスの「」を参照してください。

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