Generative AI is no longer a buzzword. It has become an integral part of our everyday conversations. Typically, new technologies spark excitement within the technical circle; however, Generative AI transcended that boundary and quickly became mainstream. Enterprises are also exploring its practical applications from a business perspective. You can love it or hate it, but one thing is clear - you can't ignore it. So I decided to try it myself. This blog post is about my experimentation with Amazon Bedrock's capabilities.
Let's use the example of building a RewardBot to illustrate the process, and if you're interested in learning how to build a chatbot using AWS services then this blog post is perfect for you.
Retail Rewards Inc., a fictitious retail company, is taking generative AI to its customers. The functionalities of the first version of RewardBot for customers are to check their loyalty card balance, and help them fully understand the benefits of the loyalty program. Our goal is to create a pilot that we can iterate and improve over time.
Key Features of RewardBot:
Membership Enrollment: Guide users through the process of becoming loyalty card members.
Balance Inquiry: Allow users to check their loyalty card balance in real-time.
FAQs and Support: Provide answers to common questions and support for loyalty program issues.
Membership Enrollment feature will be launched in next version.
Now that we’ve set the stage, it’s time to dive deeper into the technical details of our Generative AI Assistant. First, let's look at few important concepts-
Retrieval-Augmented Generation (RAG)
Imagine you're asking a large language model a question. Generally, it would just give you an answer based on what it's learned from its training data. However, it won't know anything about your customer data, or about your business policies around loyalty reward programs. That's where Retrieval-Augmented Generation (RAG) comes in. It is a technique to leverage your enterprise-specific data to enhance the responses of large language models (LLMs) without retraining a model.
Knowledge Bases for Amazon Bedrock
Amazon Bedrock Knowledge Bases are centralized repositories for structured and unstructured data, allowing AI models to access up-to-date information. They enhance the accuracy of AI responses without needing frequent retraining.
In a retail company's Loyalty Reward System, an FAQ document stored in Amazon S3 can be part of the knowledge base. When a customer asks the AI assistant a policy question, it retrieves the relevant information from this document to provide an accurate answer.
Agents for Amazon Bedrock
Agents for Amazon Bedrock plan and execute multistep tasks and help orchestrate interactions between foundation models, knowledge bases, and Lambda functions to securely execute APIs. An agent analyzes the user request and automatically calls the necessary APIs and data sources to fulfill the request. Thus, agents reduce significant development efforts for developers and speed up generative AI application deployments.
System Architecture:
Implementation Steps -
- Create a Bedrock agent
- Create Knowledge Base
- Associate Knowledge Base with the agent
- Create an action group
- API Gateway and backend Lambda
You should test agent at every step to understand the concepts of Knoweldge base and an action group.
Let's prepare couple of test prompts to use during our testing and compare results as we move through our implementation steps.
Prompt 1: Do my loyalty points expire?
Prompt 2: What is my reward balance?
Deployment Instructions
Step1: Create a Bedrock agent
Where the magic happens. The Bedrock agent orchestrates interactions between foundation models, knowledge bases, and action groups. An action group defines actions that the agent can help the user perform. For example, you could define an action group called "ManageLoyaltyPoints" that helps users carry out actions that you can define, such as getting a point balance or redeeming a balance.
Steps to Create a Bedrock Agent:
- Log in to the AWS Management Console.
Select Agents under "Builder tools" on the left and click "Create Agent".
Fill in the necessary details such as the agent's name and description.
Create a new or choose existing agent IAM role. Select model and provide agent instructions.
Make sure you save changes.
You can click on 'show trace' to understand the trace of the agent's actions.
Step2. Create a Knowledge Base
The Knowledge Base is a repository of information that the Bedrock agent will use to respond to user queries. This can include FAQs, documentation, and other relevant data.
Steps to Create a Knowledge Base:
- Knowledge base in this case would be used to host the Loyalty Program FAQ file. So, first create an S3 bucket and upload the FAQ document to it. I'll provide the Git repo for all the files and code used in this tutorial.
- In the Amazon Bedrock console, navigate to "Knowledge Bases." under Builder tools and click on "Create Knowledge Base."
With above recommended selection, KB creates OpenSearch Index as a vector database. Titan Embeddings model is used to convert FAQ document into embeddings and store in vector database. You can create a new vector database or used existing one. Here I will create a new one.
Review your selections and create knowledge base.
SYNC data source in Knowledge base.
Vector Database:
Make sure you have access to the Tital Embeddings model; otherwise, you may encounter the error.
Click on "Model Access" in the bottom left corner and request access to the model. Usually access will be granted almost immediately.
Step3: Associate Knowledge Base with the agent
Associate the Knowledge Base with the agent to enable the agent to leverage its contents for more accurate and context-aware interactions.
Prepare agent again. Click on Prepare.
Try same prompts. Those are our testcases.
If you noticed, both queries are answered based on the knowledge base. Therefore, the response to the second query is general information from the FAQ document and not tailored to the customer's account.
Step4: Create an action group
Bedrock Action Group defines specific tasks an agent can perform to help users, like managing loyalty points or answering account questions.
Steps to Create an action group:
- Click "Edit in Agent Builder" and go to the action group section.
THree things you would need here -
1.1 OpenAPI schema. Either you can refer to a file with OpenAPI specification from S3 or use visual editor. Here I have a file in S3.
1.2 DynamoDB table Users to query Loyalty Points for Users.
1.3 Lambda function which agent will invoke through an action group. This is a function (action-grp-business-function) where business logic is written to pull loyalty points balance for a user based on UserID.
I'll provide Git repository for OpenAPI specification file and the Lambda code.
One important consideration is, the resource permission for the agent and Bedrock service to invoke this lambda function.
On Lambda function console page -
OK, Let's get back to Action group creation with OpenAPI specification file and Lambda function ready for us.
Also ensure, Lambda Execution role has access to DynamoDB table. I have given full access to DynamoDB to simplify (AmazonDynamoDBFullAccess).
Click on Save and Prepare so that agent is ready with last changes.
Optionally, you can go back to agent page and create an Alias.
An alias points to a specific version of your Agent.
Let's check our prompts again -
Step5: API Gateway and backend Lambda
Awesome, the user can now interact with RewardBot to get their loyalty point balance. Next, let's build an API Gateway to expose the /getloyaltypoints API. The Lambda function (call-agent) code will be in the Git repo. Ensure the Lambda execution role has Bedrock access and increase the timeout from the default 3 seconds to a higher value.
API Gateway
- API: REST API GenAIService with Lambda Integration
- Resource: getloyaltypoints (path /)
- Method: GET.
The most of the settings used are default except below changes -
Method request
Integration request
{
"prompt":"$input.params('prompt')",
"session_id":"$input.params('session_id')"
}
Using postman to invoke API /getloyaltypoints.
For those who prefer video tutorials, please watch this video -
Top comments (0)