AWS SDK 또는 InvokeAgent CLI와 함께 사용 - Amazon Bedrock

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

AWS SDK 또는 InvokeAgent CLI와 함께 사용

다음 코드 예제는 InvokeAgent의 사용 방법을 보여줍니다.

JavaScript
JavaScript (v3) 용 SDK
참고

더 많은 내용이 있습니다. 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 세부 정보는 AWS SDK for JavaScript API InvokeAgent참조를 참조하십시오.

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: 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에 대한 자세한 내용은 파이썬용AWS SDK (Boto3) API 레퍼런스를 참조하십시오 InvokeAgent.

AWS SDK 개발자 가이드 및 코드 예제의 전체 목록은 을 참조하십시오. AWS SDK와 함께 이 서비스 사용 이 주제에는 시작하기에 대한 정보와 이전 SDK 버전에 대한 세부 정보도 포함되어 있습니다.