Skip to content

Commit 3231e9f

Browse files
authored
add lambda event agent for amazon bedrock (awslabs#746)
* add lambda event agent for amazon bedrock * add comment for properties * add comment for AgentEvent * add check-event-features for bedrock * remove duplicated module * fix invalid naming * apply formatting
1 parent 6e953eb commit 3231e9f

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ check-event-features:
6969
cargo test --package aws_lambda_events --no-default-features --features apigw
7070
cargo test --package aws_lambda_events --no-default-features --features appsync
7171
cargo test --package aws_lambda_events --no-default-features --features autoscaling
72+
cargo test --package aws_lambda_events --no-default-features --features bedrock_agent_runtime
7273
cargo test --package aws_lambda_events --no-default-features --features chime_bot
7374
cargo test --package aws_lambda_events --no-default-features --features clientvpn
7475
cargo test --package aws_lambda_events --no-default-features --features cloudwatch_events

lambda-events/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ default = [
4343
"apigw",
4444
"appsync",
4545
"autoscaling",
46+
"bedrock_agent_runtime",
4647
"chime_bot",
4748
"clientvpn",
4849
"cloudformation",
@@ -85,6 +86,7 @@ alb = ["bytes", "http", "http-body", "http-serde", "query_map"]
8586
apigw = ["bytes", "http", "http-body", "http-serde", "query_map"]
8687
appsync = []
8788
autoscaling = ["chrono"]
89+
bedrock_agent_runtime = []
8890
chime_bot = ["chrono"]
8991
clientvpn = []
9092
cloudformation = []
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
use serde::{Deserialize, Serialize};
2+
use std::collections::HashMap;
3+
4+
/// The Event sent to Lambda from Agents for Amazon Bedrock.
5+
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
6+
#[serde(rename_all = "camelCase")]
7+
pub struct AgentEvent {
8+
///The version of the message that identifies the format of the event data going into the Lambda function and the expected format of the response from a Lambda function. Amazon Bedrock only supports version 1.0.
9+
pub message_version: String,
10+
///Contains information about the name, ID, alias, and version of the agent that the action group belongs to.
11+
pub agent: Agent,
12+
///The user input for the conversation turn.
13+
pub input_text: String,
14+
/// The unique identifier of the agent session.
15+
pub session_id: String,
16+
/// The name of the action group.
17+
pub action_group: String,
18+
/// The path to the API operation, as defined in the OpenAPI schema.
19+
pub api_path: String,
20+
/// The method of the API operation, as defined in the OpenAPI schema.
21+
pub http_method: String,
22+
/// Contains a list of objects. Each object contains the name, type, and value of a parameter in the API operation, as defined in the OpenAPI schema.
23+
pub parameters: Vec<Parameter>,
24+
/// Contains the request body and its properties, as defined in the OpenAPI schema.
25+
pub request_body: RequestBody,
26+
/// Contains session attributes and their values.
27+
pub session_attributes: HashMap<String, String>,
28+
/// Contains prompt attributes and their values.
29+
pub prompt_session_attributes: HashMap<String, String>,
30+
}
31+
32+
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
33+
#[serde(rename_all = "camelCase")]
34+
pub struct RequestBody {
35+
/// Contains the request body and its properties
36+
pub content: HashMap<String, Content>,
37+
}
38+
39+
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
40+
#[serde(rename_all = "camelCase")]
41+
pub struct Content {
42+
/// The content of the request body
43+
pub properties: Vec<Property>,
44+
}
45+
46+
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
47+
#[serde(rename_all = "camelCase")]
48+
pub struct Property {
49+
/// The name of the parameter
50+
pub name: String,
51+
/// The type of the parameter
52+
pub r#type: String,
53+
/// The value of the parameter
54+
pub value: String,
55+
}
56+
57+
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
58+
#[serde(rename_all = "camelCase")]
59+
pub struct Parameter {
60+
/// The name of the parameter
61+
pub name: String,
62+
/// The type of the parameter
63+
pub r#type: String,
64+
/// The value of the parameter
65+
pub value: String,
66+
}
67+
68+
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
69+
#[serde(rename_all = "camelCase")]
70+
pub struct Agent {
71+
/// The name of the agent.
72+
pub name: String,
73+
/// The unique identifier of the agent.
74+
pub id: String,
75+
/// The alias of the agent.
76+
pub alias: String,
77+
/// The version of the agent.
78+
pub version: String,
79+
}
80+
81+
#[cfg(test)]
82+
mod tests {
83+
use serde_json;
84+
85+
#[test]
86+
#[cfg(feature = "bedrock-agent-runtime")]
87+
fn example_bedrock_agent__runtime_event() {
88+
let data = include!("../../fixtures/example-bedrock-agent-runtime-event.json");
89+
let parsed: AgentEvent = serde_json::from_str(&data).unwrap();
90+
let output: String = serde_json::to_string(&parsed).unwrap();
91+
let reparsed: AgentEvent = serde_json::from_slice(&output.as_bytes()).unwrap();
92+
assert_eq!(parsed, reparsed);
93+
}
94+
}

lambda-events/src/event/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ pub mod appsync;
1717
#[cfg(feature = "autoscaling")]
1818
pub mod autoscaling;
1919

20+
/// AWS Lambda event definitions for agent for amazon bedrock
21+
#[cfg(feature = "bedrock_agent_runtime")]
22+
pub mod bedrock_agent_runtime;
23+
2024
/// AWS Lambda event definitions for chime_bot.
2125
#[cfg(feature = "chime_bot")]
2226
pub mod chime_bot;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"messageVersion": "1.0",
3+
"agent": {
4+
"name": "AgentName",
5+
"id": "AgentID",
6+
"alias": "AgentAlias",
7+
"version": "AgentVersion"
8+
},
9+
"inputText": "InputText",
10+
"sessionId": "SessionID",
11+
"actionGroup": "ActionGroup",
12+
"apiPath": "/api/path",
13+
"httpMethod": "POST",
14+
"parameters": [
15+
{
16+
"name": "param1",
17+
"type": "string",
18+
"value": "value1"
19+
}
20+
],
21+
"requestBody": {
22+
"content": {
23+
"application/json": {
24+
"properties": [
25+
{
26+
"name": "prop1",
27+
"type": "string",
28+
"value": "value1"
29+
}
30+
]
31+
}
32+
}
33+
},
34+
"sessionAttributes": {
35+
"attr1": "value1"
36+
},
37+
"promptSessionAttributes": {
38+
"promptAttr1": "value1"
39+
}
40+
}

0 commit comments

Comments
 (0)