Skip to content

Rust: add invoke-agent example in bedrock-agent-runtime #7479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .doc_gen/metadata/bedrock-agent-runtime_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
3 changes: 2 additions & 1 deletion rustv1/examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ members = [
"aurora",
"auto-scaling",
"autoscalingplans",
"batch",
"batch",
"bedrock-agent-runtime",
"bedrock-runtime",
"cloudformation",
"cloudwatch",
Expand Down
9 changes: 9 additions & 0 deletions rustv1/examples/bedrock-agent-runtime/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"] }
79 changes: 79 additions & 0 deletions rustv1/examples/bedrock-agent-runtime/README.md
Original file line number Diff line number Diff line change
@@ -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.

<!--custom.overview.start-->
<!--custom.overview.end-->

_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).

<!--custom.important.start-->
<!--custom.important.end-->

## Code examples

### Prerequisites

For prerequisites, see the [README](../../README.md#Prerequisites) in the `rustv1` folder.


<!--custom.prerequisites.start-->
<!--custom.prerequisites.end-->

### Single actions

Code excerpts that show you how to call individual service functions.

- [InvokeAgent](src/bin/invoke-agent.rs)


<!--custom.examples.start-->
<!--custom.examples.end-->

## Run the examples

### Instructions


<!--custom.instructions.start-->
<!--custom.instructions.end-->



### 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.



<!--custom.tests.start-->
<!--custom.tests.end-->

## 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)

<!--custom.resources.start-->
<!--custom.resources.end-->

---

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0

59 changes: 59 additions & 0 deletions rustv1/examples/bedrock-agent-runtime/src/bin/invoke-agent.rs
Original file line number Diff line number Diff line change
@@ -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<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 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)
}