Agents for Amazon Bedrock
Create Agents for Amazon Bedrock using event handlers and auto generation of OpenAPI schemas.
flowchart LR
Bedrock[LLM] <-- uses --> Agent
You[User input] --> Agent
Agent -- consults --> OpenAPI
Agent[Agents for Amazon Bedrock] -- invokes --> Lambda
subgraph OpenAPI
Schema
end
subgraph Lambda[Lambda Function]
direction TB
Parsing[Parameter Parsing] --> Validation
Validation[Parameter Validation] --> Routing
Routing --> Code[Your code]
Code --> ResponseValidation[Response Validation]
ResponseValidation --> ResponseBuilding[Response Building]
end
subgraph ActionGroup[Action Group]
OpenAPI -. generated from .-> Lambda
end
style Code fill:#ffa500,color:black,font-weight:bold,stroke-width:3px
style You stroke:#0F0,stroke-width:2px
Key features¶
- Minimal boilerplate to build Agents for Amazon Bedrock
- Automatic generation of OpenAPI schemas from your business logic code
- Built-in data validation for requests and responses
- Similar experience to authoring REST and HTTP APIs
Terminology¶
Data validation automatically validates the user input and the response of your AWS Lambda function against a set of constraints defined by you.
Event handler is a Powertools for AWS feature that processes an event, runs data parsing and validation, routes the request to a specific function, and returns a response to the caller in the proper format.
OpenAPI schema is an industry standard JSON-serialized string that represents the structure and parameters of your API.
Action group is a collection of two resources where you define the actions that the agent should carry out: an OpenAPI schema to define the APIs that the agent can invoke to carry out its tasks, and a Lambda function to execute those actions.
Large Language Models (LLM) are very large deep learning models that are pre-trained on vast amounts of data, capable of extracting meanings from a sequence of text and understanding the relationship between words and phrases on it.
Agent for Amazon Bedrock is an Amazon Bedrock feature to build and deploy conversational agents that can interact with your customers using Large Language Models (LLM) and AWS Lambda functions.
Getting started¶
All examples shared in this documentation are available within the project repository
Install¶
This is unnecessary if you're installing Powertools for AWS Lambda (Python) via Lambda Layer/SAR.
You need to add pydantic
as a dependency in your preferred tool e.g., requirements.txt, pyproject.toml. At this time, we only support Pydantic V1, due to an incompatibility with Pydantic V2 generated schemas and the Agents' API.
Required resources¶
To build Agents for Amazon Bedrock, you will need:
Requirement | Description | SAM Supported | CDK Supported |
---|---|---|---|
Lambda Function | Defines your business logic for the action group | ✅ | ✅ |
OpenAPI Schema | API description, structure, and action group parameters | ❌ | ✅ |
Bedrock Service Role | Allows Amazon Bedrock to invoke foundation models | ✅ | ✅ |
Agents for Bedrock | The service that will combine all the above to create the conversational agent | ❌ | ✅ |
Using AWS SAM you can create your Lambda function and the necessary permissions. However, you still have to create your Agent for Amazon Bedrock using the AWS console.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
- Amazon Bedrock needs permissions to invoke this Lambda function
- Check the supported foundational models
- You need the role ARN when creating the Agent for Amazon Bedrock
This example uses the Generative AI CDK constructs to create your Agent with AWS CDK. These constructs abstract the underlying permission setup and code bundling of your Lambda function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
- The path to your Lambda function handler
- The path to the OpenAPI schema describing your API
Your first Agent¶
To create an agent, use the BedrockAgentResolver
to annotate your actions.
This is similar to the way all the other Event Handler resolvers work.
You are required to add a description
parameter in each endpoint, doing so will improve Bedrock's understanding of your actions.
The resolvers used by Agents for Amazon Bedrock are compatible with all Powertools for AWS Lambda features. For reference, we use Logger and Tracer in this example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
description
is a required field that should contain a human readable description of your action- We take care of parsing, validating, routing and responding to the request.
Powertools for AWS Lambda generates this automatically from the Lambda handler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
What happens under the hood?
Powertools will handle the request from the Agent, parse, validate, and route it to the correct method in your code. The response is then validated and formatted back to the Agent.
sequenceDiagram
actor User
User->>Agent: What is the current time?
Agent->>OpenAPI schema: consults
OpenAPI schema-->>Agent: GET /current_time
Agent-->>Agent: LLM interaction
box Powertools
participant Lambda
participant Parsing
participant Validation
participant Routing
participant Your Code
end
Agent->>Lambda: GET /current_time
activate Lambda
Lambda->>Parsing: parses parameters
Parsing->>Validation: validates input
Validation->>Routing: finds method to call
Routing->>Your Code: executes
activate Your Code
Your Code->>Routing: 1709215709
deactivate Your Code
Routing->>Validation: returns output
Validation->>Parsing: validates output
Parsing->>Lambda: formats response
Lambda->>Agent: 1709215709
deactivate Lambda
Agent-->>Agent: LLM interaction
Agent->>User: "The current time is 14:08:29 GMT"
Validating input and output¶
You can define the expected format for incoming data and responses by using type annotations. Define constraints using standard Python types, dataclasses or Pydantic models. Pydantic is a popular library for data validation using Python type annotations.
This example uses Pydantic's EmailStr to validate the email address passed to the schedule_meeting
function.
The function then returns a boolean indicating if the meeting was successfully scheduled.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
- No need to add the
enable_validation
parameter, as it's enabled by default. - Describe each input using human-readable descriptions
- Add the typing annotations to your parameters and return types, and let the event handler take care of the rest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
When validation fails¶
If the request validation fails, your event handler will not be called, and an error message is returned to Bedrock. Similarly, if the response fails validation, your handler will abort the response.
What does this mean for my Agent?
The event handler will always return a response according to the OpenAPI schema. A validation failure always results in a 422 response. However, how Amazon Bedrock interprets that failure is non-deterministic, since it depends on the characteristics of the LLM being used.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
sequenceDiagram
Agent->>Lambda: input payload
activate Lambda
Lambda->>Parsing: parses input parameters
Parsing->>Validation: validates input
Validation-->Validation: failure
box BedrockAgentResolver
participant Lambda
participant Parsing
participant Validation
participant Routing
participant Your Code
end
Note right of Validation: Your code is never called
Validation->>Agent: 422 response
deactivate Lambda
Generating OpenAPI schemas¶
Use the get_openapi_json_schema
function provided by the resolver to produce a JSON-serialized string that represents your OpenAPI schema.
You can print this string or save it to a file. You'll use the file later when creating the Agent.
You'll need to regenerate the OpenAPI schema and update your Agent everytime your API changes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
- This ensures that it's only executed when running the file directly, and not when running on the Lambda runtime.
- You can use additional options to customize the OpenAPI schema.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|
To get the OpenAPI schema, run the Python script from your terminal. The script will generate the schema directly to standard output, which you can redirect to a file.
1 |
|
Crafting effective OpenAPI schemas¶
Working with Agents for Amazon Bedrock will introduce non-deterministic behaviour to your system.
Why is that?
Amazon Bedrock uses LLMs to understand and respond to user input. These models are trained on vast amounts of data and are capable of extracting meanings from a sequence of text and understanding the relationship between words and phrases on it. However, this means that the same input can result in different outputs, depending on the characteristics of the LLM being used.
The OpenAPI schema provides context and semantics to the Agent that will support the decision process for invoking our Lambda function. Sparse or ambiguous schemas can result in unexpected outcomes.
We recommend enriching your OpenAPI schema with as many details as possible to help the Agent understand your functions, and make correct invocations. To achieve that, keep the following suggestions in mind:
- Always describe your function behaviour using the
description
field in your annotations - When refactoring, update your description field to match the function outcomes
- Use distinct
description
for each function to have clear separation of semantics
Video walkthrough¶
To create an Agent for Amazon Bedrock, refer to the official documentation provided by AWS.
The following video demonstrates the end-to-end process:
During the creation process, you should use the schema previously generated when prompted for an OpenAPI specification.
Advanced¶
Accessing custom request fields¶
The event sent by Agents for Amazon Bedrock into your Lambda function contains a number of extra event fields, exposed in the app.current_event
field.
Why is this useful?
You can for instance identify new conversations (session_id
) or store and analyze entire conversations (input_text
).
In this example, we append correlation data to all generated logs.
This can be used to aggregate logs by session_id
and observe the entire conversation between a user and the Agent.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Name | Type | Description |
---|---|---|
message_version | str |
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. |
agent | BedrockAgentInfo |
Contains information about the name, ID, alias, and version of the agent that the action group belongs to. |
input_text | str |
The user input for the conversation turn. |
session_id | str |
The unique identifier of the agent session. |
action_group | str |
The name of the action group. |
api_path | str |
The path to the API operation, as defined in the OpenAPI schema. |
http_method | str |
The method of the API operation, as defined in the OpenAPI schema. |
parameters | List[BedrockAgentProperty] |
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. |
request_body | BedrockAgentRequestBody |
Contains the request body and its properties, as defined in the OpenAPI schema. |
session_attributes | Dict[str, str] |
Contains session attributes and their values. |
prompt_session_attributes | Dict[str, str] |
Contains prompt attributes and their values. |
Additional metadata¶
To enrich the view that Agents for Amazon Bedrock has of your Lambda functions, use a combination of Pydantic Models and OpenAPI type annotations to add constraints to your APIs parameters.
When is this useful?
Adding constraints to your function parameters can help you to enforce data validation and improve the understanding of your APIs by Amazon Bedrock.
Customizing OpenAPI parameters¶
Whenever you use OpenAPI parameters to validate query strings or path parameters, you can enhance validation and OpenAPI documentation by using any of these parameters:
Field name | Type | Description |
---|---|---|
alias |
str |
Alternative name for a field, used when serializing and deserializing data |
validation_alias |
str |
Alternative name for a field during validation (but not serialization) |
serialization_alias |
str |
Alternative name for a field during serialization (but not during validation) |
description |
str |
Human-readable description |
gt |
float |
Greater than. If set, value must be greater than this. Only applicable to numbers |
ge |
float |
Greater than or equal. If set, value must be greater than or equal to this. Only applicable to numbers |
lt |
float |
Less than. If set, value must be less than this. Only applicable to numbers |
le |
float |
Less than or equal. If set, value must be less than or equal to this. Only applicable to numbers |
min_length |
int |
Minimum length for strings |
max_length |
int |
Maximum length for strings |
pattern |
string |
A regular expression that the string must match. |
strict |
bool |
If True , strict validation is applied to the field. See Strict Mode for details |
multiple_of |
float |
Value must be a multiple of this. Only applicable to numbers |
allow_inf_nan |
bool |
Allow inf , -inf , nan . Only applicable to numbers |
max_digits |
int |
Maximum number of allow digits for strings |
decimal_places |
int |
Maximum number of decimal places allowed for numbers |
examples |
List[Any] |
List of examples of the field |
deprecated |
bool |
Marks the field as deprecated |
include_in_schema |
bool |
If False the field will not be part of the exported OpenAPI schema |
json_schema_extra |
JsonDict |
Any additional JSON schema data for the schema property |
To implement these customizations, include extra constraints when defining your parameters:
Customizing API parameters | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
- Title should not be larger than 200 characters and strict mode is activated
Customizing API operations¶
Customize your API endpoints by adding metadata to endpoint definitions.
Here's a breakdown of various customizable fields:
Field Name | Type | Description |
---|---|---|
summary |
str |
A concise overview of the main functionality of the endpoint. This brief introduction is usually displayed in autogenerated API documentation and helps consumers quickly understand what the endpoint does. |
description |
str |
A more detailed explanation of the endpoint, which can include information about the operation's behavior, including side effects, error states, and other operational guidelines. |
responses |
Dict[int, Dict[str, OpenAPIResponse]] |
A dictionary that maps each HTTP status code to a Response Object as defined by the OpenAPI Specification. This allows you to describe expected responses, including default or error messages, and their corresponding schemas or models for different status codes. |
response_description |
str |
Provides the default textual description of the response sent by the endpoint when the operation is successful. It is intended to give a human-readable understanding of the result. |
tags |
List[str] |
Tags are a way to categorize and group endpoints within the API documentation. They can help organize the operations by resources or other heuristic. |
operation_id |
str |
A unique identifier for the operation, which can be used for referencing this operation in documentation or code. This ID must be unique across all operations described in the API. |
include_in_schema |
bool |
A boolean value that determines whether or not this operation should be included in the OpenAPI schema. Setting it to False can hide the endpoint from generated documentation and schema exports, which might be useful for private or experimental endpoints. |
To implement these customizations, include extra parameters when defining your routes:
Customzing API operations | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
Testing your code¶
Test your routes by passing an Agent for Amazon Bedrock proxy event request:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|