diff --git a/.doc_gen/metadata/bedrock-agent-runtime_metadata.yaml b/.doc_gen/metadata/bedrock-agent-runtime_metadata.yaml index f2630639ae0..0feeb82992f 100644 --- a/.doc_gen/metadata/bedrock-agent-runtime_metadata.yaml +++ b/.doc_gen/metadata/bedrock-agent-runtime_metadata.yaml @@ -25,6 +25,14 @@ bedrock-agent-runtime_InvokeAgent: - description: snippet_tags: - bdz.abapv1.invokeagent + Rust: + versions: + - sdk_version: 1 + github: rustv1/examples/bedrock-agent-runtime + excerpts: + - description: + snippet_files: + - rustv1/examples/bedrock-agent-runtime/src/bin/invoke-agent.rs services: bedrock-agent-runtime: {InvokeAgent} diff --git a/rustv1/examples/Cargo.toml b/rustv1/examples/Cargo.toml index 92a620b4185..6d85442cae7 100644 --- a/rustv1/examples/Cargo.toml +++ b/rustv1/examples/Cargo.toml @@ -9,7 +9,8 @@ members = [ "aurora", "auto-scaling", "autoscalingplans", - "batch", + "batch", + "bedrock-agent-runtime", "bedrock-runtime", "cloudformation", "cloudwatch", diff --git a/rustv1/examples/bedrock-agent-runtime/Cargo.toml b/rustv1/examples/bedrock-agent-runtime/Cargo.toml new file mode 100644 index 00000000000..0cf2f78060f --- /dev/null +++ b/rustv1/examples/bedrock-agent-runtime/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "bedrock-agent-runtime" +version = "0.1.0" +edition = "2021" + +[dependencies] +aws-config = "1.6.3" +aws-sdk-bedrockagentruntime = "1.98.0" +tokio = { version = "1.45.1", features = ["full"] } diff --git a/rustv1/examples/bedrock-agent-runtime/README.md b/rustv1/examples/bedrock-agent-runtime/README.md new file mode 100644 index 00000000000..fe766081bbd --- /dev/null +++ b/rustv1/examples/bedrock-agent-runtime/README.md @@ -0,0 +1,79 @@ +# Amazon Bedrock Agents Runtime code examples for the SDK for Rust + +## Overview + +Shows how to use the AWS SDK for Rust to work with Amazon Bedrock Agents Runtime. + + + + +_Amazon Bedrock Agents Runtime offers you the ability to run autonomous agents in your application._ + +## ⚠ Important + +* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/). +* Running the tests might result in charges to your AWS account. +* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). +* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). + + + + +## Code examples + +### Prerequisites + +For prerequisites, see the [README](../../README.md#Prerequisites) in the `rustv1` folder. + + + + + +### Single actions + +Code excerpts that show you how to call individual service functions. + +- [InvokeAgent](src/bin/invoke-agent.rs) + + + + + +## Run the examples + +### Instructions + + + + + + + +### Tests + +⚠ Running tests might result in charges to your AWS account. + + +To find instructions for running these tests, see the [README](../../README.md#Tests) +in the `rustv1` folder. + + + + + + +## Additional resources + +- [Amazon Bedrock Agents Runtime User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/agents.html) +- [Amazon Bedrock Agents Runtime API Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Agents_for_Amazon_Bedrock_Runtime.html) +- [SDK for JavaScript (v3) Amazon Bedrock Agents Runtime reference](https://crates.io/crates/aws-sdk-bedrockagentruntime) + + + + +--- + +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 + diff --git a/rustv1/examples/bedrock-agent-runtime/src/bin/invoke-agent.rs b/rustv1/examples/bedrock-agent-runtime/src/bin/invoke-agent.rs new file mode 100644 index 00000000000..2148b280419 --- /dev/null +++ b/rustv1/examples/bedrock-agent-runtime/src/bin/invoke-agent.rs @@ -0,0 +1,59 @@ +use aws_config::{BehaviorVersion, SdkConfig}; +use aws_sdk_bedrockagentruntime::{self as bedrockagentruntime, types::ResponseStream}; + +const BEDROCK_AGENT_ID: &str = "AJBHXXILZN"; +const BEDROCK_AGENT_ALIAS_ID: &str = "AVKP1ITZAA"; +const BEDROCK_AGENT_REGION: &str = "us-east-1"; + +#[tokio::main] +async fn main() -> Result<(), Box> { + 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 { + 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 mut response_stream = response.completion; + let mut full_agent_text_response = String::new(); + + println!("Processing Bedrock agent response stream..."); + while let Some(event_result) = response_stream.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); + } + } + } + } + _ => { + println!("Received an unhandled event type from Bedrock stream."); + } + } + } + + Ok(full_agent_text_response) +}