Skip to content

Commit 5e37073

Browse files
committed
feat: add invoke agent in bedrock-agent-runtime example
1 parent d431cfc commit 5e37073

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

rustv1/examples/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ members = [
99
"aurora",
1010
"auto-scaling",
1111
"autoscalingplans",
12-
"batch",
12+
"batch", "bedrock-agent-runtime",
1313
"bedrock-runtime",
1414
"cloudformation",
1515
"cloudwatch",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "bedrock-agent-runtime"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
anyhow = "1.0.98"
8+
aws-config = "1.6.3"
9+
aws-sdk-bedrockagentruntime = "1.98.0"
10+
tokio = { version = "1.45.1", features = ["full"] }
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use aws_config::{BehaviorVersion, SdkConfig};
2+
use aws_sdk_bedrockagentruntime::{self as bedrockagentruntime, types::ResponseStream};
3+
4+
const BEDROCK_AGENT_ID: &str = "AJBHXXILZN";
5+
const BEDROCK_AGENT_ALIAS_ID: &str = "AVKP1ITZAA";
6+
7+
#[::tokio::main]
8+
async fn main() -> Result<(), bedrockagentruntime::Error> {
9+
let result = invoke_bedrock_agent("a prompt".to_string(), "random session id".to_string()).await?;
10+
Ok(println!("{}",result))
11+
}
12+
13+
async fn invoke_bedrock_agent(prompt: String, session_id: String) -> Result<String, bedrockagentruntime::Error> {
14+
let aws_config: SdkConfig = aws_config::load_from_env().await;
15+
let bedrock_client = bedrockagentruntime::Client::new(&aws_config);
16+
17+
let command_builder = bedrock_client
18+
.invoke_agent()
19+
.agent_id(BEDROCK_AGENT_ID)
20+
.agent_alias_id(BEDROCK_AGENT_ALIAS_ID)
21+
.session_id(session_id)
22+
.input_text(prompt);
23+
24+
let response = command_builder.send().await?;
25+
26+
let mut response_stream = response.completion;
27+
let mut full_agent_text_response = String::new();
28+
29+
println!("Processing Bedrock agent response stream...");
30+
while let Some(event_result) = response_stream.recv().await.unwrap() {
31+
match event_result {
32+
ResponseStream::Chunk(chunk) => {
33+
if let Some(bytes) = chunk.bytes {
34+
match String::from_utf8(bytes.into_inner()) {
35+
Ok(text_chunk) => {
36+
full_agent_text_response.push_str(&text_chunk);
37+
}
38+
Err(e) => {
39+
eprintln!("UTF-8 decoding error for chunk: {}", e);
40+
}
41+
}
42+
}
43+
}
44+
_ => {
45+
println!("Received an unhandled event type from Bedrock stream.");
46+
}
47+
}
48+
}
49+
50+
Ok(full_agent_text_response)
51+
}

rustv1/examples/bedrock-runtime/src/bin/tool-use.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ use aws_smithy_types::Document;
2424
use tracing::debug;
2525

2626
// Set the model ID, e.g., Claude 3 Haiku.
27-
const MODEL_ID: &str = "anthropic.claude-3-haiku-20240307-v1:0";
28-
const CLAUDE_REGION: &str = "us-east-1";
27+
const MODEL_ID: &str = "anthropic.claude-3-7-sonnet-20250219-v1:0";
28+
const CLAUDE_REGION: &str = "eu-west-1";
2929

3030
const SYSTEM_PROMPT: &str = "You are a weather assistant that provides current weather data for user-specified locations using only
3131
the Weather_Tool, which expects latitude and longitude. Infer the coordinates from the location yourself.

0 commit comments

Comments
 (0)