AI Agents on AWS

Carlo Peluso
Storm Reply
Published in
8 min readMay 14, 2024

TLDR: in this article, we will dive into the world of AI Agents, highlighting the capabilities and utilisation scopes of this tools.
We will then explore how AI Agents can be implemented on AWS and, finally, we will guide you through the creation of an AI Agent from scratch!

📚 Introduction to AI Agents

AI agents leverage the capabilities of Generative AI models to execute specific tasks or fulfill pre-defined roles, producing human-like conversations.

AI agents coordinate a sequence of requests to Generative AI models and knowledge bases, automatically figuring out the actions that should be taken to fulfill a request.
Indeed, AI agents act as orchestrators, receiving questions from the users and translating them into actionable commands for the underlying generative models.

This ability to interpret and respond to user requests empowers agents to automate workflows, enhance user experiences, and drive innovation across a wide spectrum of applications.

🚀 AI Agents on AWS

Amazon Bedrock Agents stand out as a powerful toolkit within the landscape of generative AI agents. Built upon the robust infrastructure of Amazon Web Services (AWS), Bedrock Agents offer a seamless and scalable solution for developing and deploying AI agents on cloud.

Before going into the details of how an AI agent can be implemented on Amazon Bedrock, it is necessary to introduce the fundamental modules orchestrated by the agent itself.

  1. Action groups define the tasks that can be implemented. Each Action Group allows us to program a specific agent behaviour.
    Action groups are implemented as Lambda Functions and described using an OpenAPI schema.
  2. Knowledge bases contain documents and information that can be queried by the system to increase the response generation capabilities. These data are stored in the form of vectors within a vector database.
    Knowledge bases could be built on top of different data sources and rely on vector databases such as OpenSearch and Pinecone.
  3. Foundation models are invoked and used by the AI agent to
    (1.) validate user requests — for instance, verifying that they are not malicious,
    (2.) determine whether it is possible to respond with the information captured at a given time or whether it is necessary to ask the user for further information,
    (3.) decide which operation needs to be carried out to enhance the current response — for instance, call an Action Group or search within the Knowledge Base.

Amazon Bedrock Agents workflow

Foundation models, knowledge bases and action groups are exploited within an Amazon Bedrock Agents system following this workflow:

  1. Contextualization and Validation: the agent verifies that the user input is not malicious.
  2. Orchestration Prompt creation: if the user’s input is valid, an orchestration prompt is created and sent to a Foundation Model, using (a.) the user’s input, (b.) the conversation history, (c.) information regarding the knowledge bases, the list of available action groups and instructions provided by the system developer (e.g., generic prompts defining at a high level how the agent should behave).
  3. Foundation Model decision: the response of the Foundation Model defines which actions are to be taken, such as searching the knowledge base or invoking an Action Group via the API.
  4. Observation production: once the necessary action has been taken, an observation is produced. The observation is used to enrich the original Orchestration Prompt and re-iterate.
  5. Looping and Refinement: the new Orchestration Prompt is submitted to the Foundation Model which, again, will decide what is the best action to take to satisfy the user’s request. If the Agent has all the necessary informations to answer the user’s question, the response is provided and the loop ends.

💡 Building a Sample Agent on Amazon Bedrock

Now that we have seen what AI Agents are, what they are used for and what services are offered by AWS to create them, let’s see how they can be implemented directly from the AWS console.

⚡️ For the sake of this example, we will create a simple inventory system that can give information about the Pokèmon puppets sold by your company. ⚡️

Indeed, in the following sections we will:

  1. Set up a Knowledge Base on Amazon Bedrock
  2. Create the Amazon Bedrock Agent
  3. Configure the Agent to use the Knowledge Base
  4. Create an Amazon Bedrock Action Group
  5. Configure the Agent to use the Action Group
  6. Test the solution!

🔎 Set up a Knowledge Base

Let’s start by creating a Knowledge Base.
In this example, we will use these two files:

  1. puppets.csv, that contains 10 records about the Pokèmon puppets available
  2. puppets_description.pdf, that contains some catchy descriptions about each puppet.
data.csv
puppets_description.pdf

Before creating the Knowledge Base within Amazon Bedrock, we need to store these files within an Amazon S3 bucket.

Once uploaded the files within S3, we can finally create the Knowledge Base within Amazon Bedrock.

To do so,

  1. Go to Amazon Bedrock, select Knowledge bases and click on Create knowledge base.
  2. Give a name to the Knowledge Base.
  3. Configure a Data Source using the S3 URI in which the data is stored.
  4. Select an Embedding Model and a Vector Database.
    In this example, we use Titan Embeddings G1 and Amazon OpenSearch Serverless, respectively.
  5. Finish by clicking on Create knowledge base. This operation might take some time.
  6. Once finished, select the Data Source and then click on Sync.

🧠 Create the Agent

Let’s now start with the configuration of the Agent.

  1. Go to Amazon Bedrock, select Agents and click on Create agent.
  2. Give a name to the Agent.
  3. Select a Text Generation Model and include some instructions for the Agent.
    In this case, we instructed the Agent as follows:
    “You are an helpful agent that can answer questions related to the catalog of Pokemon puppets and inventory”.
  4. Click on Add knowledge base and select the Knowledge Base created before.

✍🏼 Create an Action Group

Now that we have our Agent and Knowledge Base configured, let’s create an Action Group for handling requests about the puppets inventory.

As anticipated before, Action Groups are based on Lambda Functions and OpenAPI schemas.

As follows, a simple OpenAPI schema for our API /GetPokemonPuppetsInventory and the corresponding Lambda Function handler.

We will store the OpenAPI schema on an Amazon S3 bucket and we will use the code for defining our Lambda Function.

{
"openapi":"3.0.0",
"info":{
"title":"Pokemon Store",
"version":"1.0.0",
"description":"APIs for managing Pokemon puppets inventory"
},
"paths":{
"/GetPokemonPuppetsInventory":{
"get":{
"summary":"Gets Pokemon puppets inventory",
"description":"Gets all Pokemon puppets available",
"operationId":"getPokemonPuppetsInventory",
"parameters":[

],
"responses":{
"200":{
"description":"Returns inventory of all Pokemon Puppets available",
"content":{
"application/json":{
"schema":{
"type":"array",
"items":{
"type":"object",
"properties":{
"puppetId":{
"type":"string",
"description":"Puppet Id"
},
"pokemonName":{
"type":"string",
"description":"Pokemon Name"
},
"quantity":{
"type":"number",
"description":"Puppet quantity"
}
}
}
}
}
}
}
}
}
}
}
}
OpenAPI schema saved on Amazon S3 bucket
import json

def lambda_handler(event, context):
print("Event: " + json.dumps(event))

api_path = event['apiPath']

if api_path == "/GetPokemonPuppetsInventory":

# Query could be made on an external database
# containing the informations needed.
response_data = [
{
"Puppet Id": 1,
"Pokemon Name": "Bulbasaur",
"Quantity": 5,
},
{
"Puppet Id": 2,
"Pokemon Name": "Ivysaur",
"Quantity": 3,
},
{
"Puppet Id": 3,
"Pokemon Name": "Venusaur",
"Quantity": 2,
},
{
"Puppet Id": 4,
"Pokemon Name": "Charmander",
"Quantity": 8,
},
{
"Puppet Id": 5,
"Pokemon Name": "Charmeleon",
"Quantity": 6,
},
{
"Puppet Id": 6,
"Pokemon Name": "Charizard",
"Quantity": 1,
},
{
"Puppet Id": 7,
"Pokemon Name": "Squirtle",
"Quantity": 7,
},
{
"Puppet Id": 8,
"Pokemon Name": "Wartortle",
"Quantity": 4,
},
{
"Puppet Id": 9,
"Pokemon Name": "Blastoise",
"Quantity": 1,
},
{
"Puppet Id": 10,
"Pokemon Name": "Pikachu",
"Quantity": 10,
},
]

else:
response_data = {"message": "API Path not known."}

response = {
'actionGroup': event['actionGroup'],
'apiPath': event['apiPath'],
'httpMethod': event['httpMethod'],
'httpStatusCode': 200,
'responseBody': {
'application/json': {
'body': json.dumps(response_data)
}
}
}

return {
'messageVersion': '1.0',
'response': response,
'sessionAttributes': event['sessionAttributes'],
'promptSessionAttributes': event['promptSessionAttributes']
}
pokemon-puppets-inventory Lambda Function

We are now able to configure our Action Group!

To do so,

  1. Go to Amazon Bedrock, select Agents and click on the Agent just created.
  2. Click on Edit in Agent builder.
  3. On Action Groups, click Add.
  4. Enter an Action Group name.
  5. Select Define with API schemas as Action group type.
  6. Select the just created Lambda function as Action group invocation.
  7. Select an existing API schema as the Action group schema option.
    Then, specify the S3 URL where the OpenAPI schema is located.
  8. Click on Create.

Test the Agent!

🎉 Finally, we can test our Agent! 🎉

The first question we can ask to the Agent is:
“which Pokemon puppets are available?

The goal of this question is to test that the Agent is correctly using the Lambda Function we defined.

As you can see, the Agent is correctly returning the list of available Pokemon puppets.

The second question we can ask to the Agent, instead, aims to test that the Knowledge Base is correctly exploited.

Indeed, we can now ask for a description about a Pokemon puppet we want to buy: “I want to buy the Charizard one. Give me a description of the puppet!

As you can see from the response, our Agent is able to describe the puppet we choose and give us the information found within the Knowledge Base.

🔎 Key Takeaways

  • AI Agents use generative AI models to perform tasks or fulfill roles.
    They coordinate requests to generative models and knowledge bases to complete user requests.
    They can automate workflows, improve user experiences, and drive innovation across applications.
  • AI Agents on AWS:
    Amazon Bedrock Agents is a powerful toolkit for developing and deploying AI agents on the cloud, by exploiting Action Groups, Knowledge Bases and Foundation Models.
  • The article guides you through creating a simple Pokemon puppet inventory system using Bedrock Agents.
  • The system utilizes a knowledge base with product information and a Lambda function to access inventory data.
  • The user interacts with the agent through questions, and the agent retrieves information from the knowledge base or inventory API.

--

--