AI-Powered Loyalty Rewards Assistant: Amazon Bedrock Agent in Action
Tags: aws, tutorial, ai, api
Published: 2024-06-23 21:48:30

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) <u>without retraining a model</u>.

**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:**

![RewardBot Architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/711typtpw0xdz83oo7bh.png)

Implementation Steps -

1. Create a Bedrock agent
2. Create Knowledge Base
3. Associate Knowledge Base with the agent
4. Create an action group 
5. 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.
- Navigate to Amazon Bedrock.
![getting started](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oamemk7qpxk0dfffs6gl.png)

- Select Agents under "Builder tools" on the left and click "Create Agent". 
![agents](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hwt46zhflei94u0g33xr.png)

- Fill in the necessary details such as the agent's name and description.
![agent popup](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hqk5sr9asw9ycwam03k2.png)

- Create a new or choose existing agent IAM role. Select model and provide agent instructions.

![agent config](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/npho28145sv3m4t2gily.png)

Make sure you save changes.

- "Prepare" the agent. This is important before you could test.
![agent prepare](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ef3qe6aripyizz2nwdo.png)

- Test agents with prompts

![No answer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2t9m3grx6w0bxsjxgjo5.png)

![No answer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n7klcwa8l87b1vqcmd0n.png)

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.

![FAQ doc](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/56zg4zq0s7gavxe8h5rc.png)

- 
In the Amazon Bedrock console, navigate to "Knowledge Bases." under Builder tools and click on "Create Knowledge Base."
![KB1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bjofr6n3v1vso928x72d.png)

![KB2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ylbj6esqn6mt28vb9o6b.png)

![KB3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bhwvyq5lm8hqdg94mn8z.png)

![KB4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bncy02uwo8xd93jbvjth.png)

![KB5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9s6eje7ei52p3w9423oc.png)

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.

![KB6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gzwpht2odtwdfq0o94ci.png)

**Vector Database:**

![Vector DB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4aqcc14bnkfaiiprmt9e.png)

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.

![Model Access](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f53i7vj9di4nfpte8xse.png)

**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.

![Connect KB1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s60knrcejnf82hbus1wj.png)

![Connect KB2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/69a0odwp7l2ez2stw4ad.png)

![Connect KB3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j7abn0c0218npyd2vxqr.png)

Prepare agent again. Click on Prepare.

Try same prompts. Those are our testcases.

![Test02](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a3fql84kx8pbw4r5nfif.png)

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.

![AG1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jf24bbasgr83kushdyv1.png)

_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.

![Table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s6axfwyumaj8bniqvnyo.png)

**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 -

![Resource Permissions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qogibtrfc64vmwbuht58.png)

![Resource Permissions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rdn99jora0vhjtl83cgq.png)

OK, Let's get back to Action group creation with OpenAPI specification file and Lambda function ready for us.

![AG2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/78apgq7fc8qkvw58q35h.png)

![AG3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y71n60ywwxizwuuyjd6d.png)
Also ensure, Lambda Execution role has access to DynamoDB table. I have given full access to DynamoDB to simplify (AmazonDynamoDBFullAccess).

![AG4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/amooflnfku1vfyhusu1q.png)

Click on Save and Prepare so that agent is ready with last changes.

![AG5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5a6sae2rfe6284m5gjf0.png)

Optionally, you can go back to agent page and create an Alias.
An alias points to a specific version of your Agent.

![Alias1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/61kh1tixsryxzvyb32t0.png)

![Alias2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qdesbf2ugdzfedxokjqt.png)

Let's check our prompts again -

![FinalCheck](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/785yqw1z7uct2f8opbc8.png)

**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.

![Lambda config](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yvqemk5s25b2o9vnqcs3.png)

**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_

![method request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/avu0g2a81mdgvq7h8gw9.png)

_Integration request_

![Integration request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x6jipn1ha7lmy7roaqwm.png)
{
"prompt":"$input.params('prompt')",
"session_id":"$input.params('session_id')"
}

Using postman to invoke API **/getloyaltypoints**.

![API Gateway](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7s2mlq3j00qs31q57zh0.png)

![API Gateway](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oxa1qgfbwoe0rlrl871i.png)

[Github Repository](https://github.com/awslearn-repo/RewardBot
)