InvokeAgent 搭配 AWS SDK 使用 - Amazon Bedrock

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

InvokeAgent 搭配 AWS SDK 使用

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

JavaScript
SDK for JavaScript (v3)
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

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 (const 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 "node:url"; if (process.argv[1] === fileURLToPath(import.meta.url)) { const result = await invokeBedrockAgent("I need help.", "123"); console.log(result); }
  • 如需 API 詳細資訊,請參閱適用於 JavaScript 的 AWS SDK 《 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: # 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 詳細資訊,請參閱《適用於 AWS Python (Boto3) 的 SDK API 參考》中的 InvokeAgent

Rust
SDK for Rust
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

use aws_config::{BehaviorVersion, SdkConfig}; use aws_sdk_bedrockagentruntime::{ self as bedrockagentruntime, types::{error::ResponseStreamError, ResponseStream}, }; #[allow(unused_imports)] use mockall::automock; const BEDROCK_AGENT_ID: &str = "AJBHXXILZN"; const BEDROCK_AGENT_ALIAS_ID: &str = "AVKP1ITZAA"; const BEDROCK_AGENT_REGION: &str = "us-east-1"; #[cfg(not(test))] pub use EventReceiverImpl as EventReceiver; #[cfg(test)] pub use MockEventReceiverImpl as EventReceiver; pub struct EventReceiverImpl { inner: aws_sdk_bedrockagentruntime::primitives::event_stream::EventReceiver< ResponseStream, ResponseStreamError, >, } #[cfg_attr(test, automock)] impl EventReceiverImpl { #[allow(dead_code)] pub fn new( inner: aws_sdk_bedrockagentruntime::primitives::event_stream::EventReceiver< ResponseStream, ResponseStreamError, >, ) -> Self { Self { inner } } pub async fn recv( &mut self, ) -> Result< Option<ResponseStream>, aws_sdk_bedrockagentruntime::error::SdkError< ResponseStreamError, aws_smithy_types::event_stream::RawMessage, >, > { self.inner.recv().await } } #[tokio::main] async fn main() -> Result<(), Box<bedrockagentruntime::Error>> { let result = invoke_bedrock_agent("I need help.".to_string(), "123".to_string()).await?; println!("{}", result); Ok(()) } async fn invoke_bedrock_agent( prompt: String, session_id: String, ) -> Result<String, bedrockagentruntime::Error> { let sdk_config: SdkConfig = aws_config::defaults(BehaviorVersion::latest()) .region(BEDROCK_AGENT_REGION) .load() .await; let bedrock_client = bedrockagentruntime::Client::new(&sdk_config); let command_builder = bedrock_client .invoke_agent() .agent_id(BEDROCK_AGENT_ID) .agent_alias_id(BEDROCK_AGENT_ALIAS_ID) .session_id(session_id) .input_text(prompt); let response = command_builder.send().await?; let response_stream = response.completion; let event_receiver = EventReceiver::new(response_stream); process_agent_response_stream(event_receiver).await } async fn process_agent_response_stream( mut event_receiver: EventReceiver, ) -> Result<String, bedrockagentruntime::Error> { let mut full_agent_text_response = String::new(); while let Some(event_result) = event_receiver.recv().await? { match event_result { ResponseStream::Chunk(chunk) => { if let Some(bytes) = chunk.bytes { match String::from_utf8(bytes.into_inner()) { Ok(text_chunk) => { full_agent_text_response.push_str(&text_chunk); } Err(e) => { eprintln!("UTF-8 decoding error for chunk: {}", e); } } } } _ => { panic!("received an unhandled event type from Bedrock stream",); } } } Ok(full_agent_text_response) } #[cfg(test)] mod test { use super::*; #[tokio::test] async fn test_process_agent_response_stream() { let mut mock = MockEventReceiverImpl::default(); mock.expect_recv().times(1).returning(|| { Ok(Some( aws_sdk_bedrockagentruntime::types::ResponseStream::Chunk( aws_sdk_bedrockagentruntime::types::PayloadPart::builder() .set_bytes(Some(aws_smithy_types::Blob::new(vec![ 116, 101, 115, 116, 32, 99, 111, 109, 112, 108, 101, 116, 105, 111, 110, ]))) .build(), ), )) }); // end the stream mock.expect_recv().times(1).returning(|| Ok(None)); let response = process_agent_response_stream(mock).await.unwrap(); assert_eq!("test completion", response); } #[tokio::test] #[should_panic(expected = "received an unhandled event type from Bedrock stream")] async fn test_process_agent_response_stream_error() { let mut mock = MockEventReceiverImpl::default(); mock.expect_recv().times(1).returning(|| { Ok(Some( aws_sdk_bedrockagentruntime::types::ResponseStream::Trace( aws_sdk_bedrockagentruntime::types::TracePart::builder().build(), ), )) }); let _ = process_agent_response_stream(mock).await.unwrap(); } }
  • 如需 API 詳細資訊,請參閱《適用於 AWS Rust 的 SDK API 參考》中的 InvokeAgent

SAP ABAP
適用於 SAP ABAP 的開發套件
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

DATA(lo_result) = lo_bdz->invokeagent( iv_agentid = iv_agentid iv_agentaliasid = iv_agentaliasid iv_enabletrace = abap_true iv_sessionid = CONV #( cl_system_uuid=>create_uuid_c26_static( ) ) iv_inputtext = |Let's play "rock, paper, scissors". I choose rock.| ). DATA(lo_stream) = lo_result->get_completion( ). TRY. " loop while there are still events in the stream WHILE lo_stream->/aws1/if_rt_stream_reader~data_available( ) = abap_true. DATA(lo_evt) = lo_stream->read( ). " each /AWS1/CL_BDZRESPONSESTREAM_EV event contains exactly one member " all others are INITIAL. For each event, process the non-initial " member if desired IF lo_evt->get_chunk( ) IS NOT INITIAL. " Process a Chunk event DATA(lv_xstr) = lo_evt->get_chunk( )->get_bytes( ). DATA(lv_answer) = /aws1/cl_rt_util=>xstring_to_string( lv_xstr ). " the answer says something like "I chose paper, so you lost" ELSEIF lo_evt->get_files( ) IS NOT INITIAL. " process a Files event if desired ELSEIF lo_evt->get_returncontrol( ) IS NOT INITIAL. " process a ReturnControl event if desired ELSEIF lo_evt->get_trace( ) IS NOT INITIAL. " process a Trace event if desired ENDIF. ENDWHILE. " the stream of events can possibly contain an exception " which will be raised to break the loop " catch /AWS1/CX_BDZACCESSDENIEDEX. " catch /AWS1/CX_BDZINTERNALSERVEREX. " catch /AWS1/CX_BDZMODELNOTREADYEX. " catch /AWS1/CX_BDZVALIDATIONEX. " catch /AWS1/CX_BDZTHROTTLINGEX. " catch /AWS1/CX_BDZDEPENDENCYFAILEDEX. " catch /AWS1/CX_BDZBADGATEWAYEX. " catch /AWS1/CX_BDZRESOURCENOTFOUNDEX. " catch /AWS1/CX_BDZSERVICEQUOTAEXCDEX. " catch /AWS1/CX_BDZCONFLICTEXCEPTION. ENDTRY.
  • 如需 API 詳細資訊,請參閱《適用於 AWS SAP ABAP 的 SDK API 參考》中的 InvokeAgent

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