Many e-commerce applications want to provide users with human-like chatbots that guide them in choosing the best product as a gift for their loved ones or friends. To improve customer experience, the chatbot should interact in a natural conversational manner and understand the user’s preferences and requirements, such as the recipient’s gender, gifting occasion, desired product category, etc. Based on the user discussion, the chatbot should be able to query the e-commerce product catalog, filter the results, and recommend the most appropriate product.
Amazon Bedrock is a fully managed service that offers a choice of high-performance foundational models (FMs) from leading artificial intelligence (AI) companies, including AI21 Labs, Anthropic, Cohere, Meta, Mistral AI, Stability AI, and Amazon, through a single API and also provides a wide range of capabilities for building generative AI applications with security, privacy, and responsible AI.
Amazon Bedrock Agents is a feature that enables generative AI applications to execute multi-step tasks across enterprise systems and data sources. In this post, we demonstrate how to build an e-commerce product recommendation chatbot using Amazon Bedrock Agents and FMs available in Amazon Bedrock.
Solution overview
Traditional rule-based chatbots often struggle to handle the nuance and complexity of open-ended conversations, frustrating for users. Additionally, manually coding every possible conversation flow and product filtering logic is time-consuming and error-prone, especially as your product catalog expands.
Addressing this challenge requires a solution that leverages the latest advancements in generative AI to create natural conversational experiences. The solution should seamlessly integrate with existing product catalog APIs and dynamically adjust the conversation flow based on user responses, reducing the need for extensive coding.
Amazon Bedrock Agents enables you to build intelligent chatbots that can converse naturally with users, understand their preferences, and efficiently retrieve and recommend the most relevant products from your catalog. Amazon Bedrock Agents simplifies the process of building and deploying generative AI models, enabling businesses to create engaging, personalized conversational experiences without requiring extensive machine learning (ML) expertise.
In our use case, we use Amazon Bedrock Agents to build a recommender chatbot that prompts users to describe where they would like to buy a gift and the relevant occasion. The agent uses an API implemented as an AWS Lambda function to query product information stored in an Amazon DynamoDB table. The agent tailors the API input based on the discussion with the user (e.g. gender, occasion, category) to filter products. After asking clarifying questions to get the user’s gift preferences, the agent responds with the most relevant products available in the DynamoDB table based on the user’s preferences.
The following diagram shows the solution architecture:
As shown in the diagram above, an e-commerce application first uses an agent to facilitate a conversation with the user and generate product recommendations. The agent retrieves product information using an API supported by Lambda. Finally, the Lambda function looks up the product data from DynamoDB.
Prerequisites
You must have an AWS account with a user or role with, at a minimum, the following AWS Identity and Access Management (IAM) policies and permissions:
- AWS managed policies:
AmazonBedrockFullAccess
AWSMarketplaceManageSubscriptions
AWSLambda_ReadOnlyAccess
AmazonDynamoDBReadOnlyAccess
- IAM actions:
iam:CreateRole
iam:CreatePolicy
iam:AttachRolePolicy
Deploying solution resources using AWS CloudFormation
Before we create the agent, we need to set up a product database and API. We use an AWS CloudFormation template to create a DynamoDB table to store product information and a Lambda function that acts as an API to get product details.
At the time of writing, you can deploy the solution in any of the following AWS Regions: US East (N. Virginia), US West (Oregon), Asia Pacific (Mumbai, Sydney), Europe (Frankfurt, Paris), Canada (Central), and South America (Sao Paulo). For the latest information, see Amazon Bedrock Agent Supported Regions and Models.
To deploy the template, Launch the stack:
This template creates DynamoDB tables named: Products
It has the following attributes: product_name
(Partition key) category
, gender
and occasion
We also define global secondary indexes (GSIs) for each of these attributes to enable efficient queries.
In addition, the template configures a Lambda function. GetProductDetailsFunction
This acts as an API to get product details. This Lambda function accepts query parameters such as: category
, gender
and occasion
Builds a filter expression based on the specified parameters and scans the DynamoDB table to retrieve matching products. If no parameters are specified, retrieves all products in the table and returns the first 100 products.
The template also creates another Lambda function. PopulateProductsTableFunction
Generate sample data to save, Products
The CloudFormation template includes: PopulateProductsTableFunction
It acts once as part of the template deployment and adds 100 sample product entries to the Products DynamoDB table with different combinations of product name, description, category, gender, and occasion.
Optionally, you can update the sample product entries or replace them with your own product data. To do so, open the DynamoDB console, Explore itemsSelect Products
Table. Select scan Select run View and edit the current item, or Create Item Add a new item. If the attributes of your data are different from the sample product entry, you’ll need to adjust the Lambda function code. GetProductDetailsFunction
the OpenAPI schema, and the agent procedures used in the next section.
Create an agent
Now that the infrastructure is in place, we can create an agent. The first step is to request access to the model.
- On the Amazon Bedrock console, Model Access In the navigation pane.
- choose Enable a specific model.
- Select the model you need to access (for this post, Claude 3 Sonnet).
Wait until the model access status changes to Allow Access.
Now we can create the agent. We will use a CloudFormation template to create the agent and an action group that will invoke the Lambda function.
- To deploy the template, Launch the stack:
You can now see the details of the agent created by the stack.
- On the Amazon Bedrock console, agent under Builder Tools In the navigation pane.
- Select an Agent
product-recommendation-agent
Select Edit with Agent Builder. - of Instructions for agents A section contains a set of steps that guide your agents on how to communicate with users and use the APIs. You can adapt the steps based on different use cases or business scenarios and the APIs available to you.
The agent’s main objective is to converse with the user and gather information about the recipient’s gender, gift occasion, and preferred category. Based on this information, the agent queries a Lambda function to retrieve and recommend appropriate products.
The next step is to verify the action group that will allow the agent to invoke the Lambda function.
- So Action Groups In the section
Get-Product-Recommendations
Action group.
You can see GetProductDetailsFunction
A Lambda function is selected Calling an Action Group section.
So Action Groups Schema In the section, you can see the OpenAPI schema that allows the agent to understand the API description, inputs, outputs, and actions that can be used while conversing with the user.
Now, Test Agent A pane for talking to a chatbot.
Test your chatbot
The following screenshot shows an example conversation where the chatbot recommends products after calling the API.
In the sample conversation, the chatbot asks the right questions to determine the gift recipient’s gender, occasion, and desired category. After collecting enough information, it queries the API and presents a list of recommended products that match the user’s preferences.
To see the rationale behind each answer, Show TraceThe following screenshot shows how an agent decided to use different API filters based on the discussion.
As you can see, rationale
Record how agents make decisions for each interaction. This tracing data helps you understand the reasoning behind recommendations. Logging this information can help you refine agent recommendations in the future.
cleaning
To clean up resources, follow these steps:
- Delete the stack in the AWS CloudFormation console.
AgentStack
. - Then delete the stack
Productstableandapi
.
Conclusion
In this post, we demonstrated how to use Amazon Bedrock Agents to create a conversational chatbot that can help users find the perfect gift. The chatbot intelligently gathers user preferences, queries backend APIs to get relevant product details, and presents recommendations to the user. This approach demonstrates the power of Amazon Bedrock Agents for building engaging, context-aware conversational experiences.
We recommend that you follow best practices when working with the Amazon Bedrock agent. For example, you can create and configure your agents using AWS CloudFormation to minimize human error and to recreate agents in different environments and regions. Also, automating agent testing using a set of golden questions and their expected answers allows you to test the quality of the agent’s instructions and compare the output of different models on Amazon Bedrock as they relate to your use case.
Visit Amazon Bedrock Agent to learn more about features and details.
About the Author
Mahmoud Salaheldin is a Senior Solutions Architect at AWS. He works with clients across the Middle East, North Africa and Turkey to help large enterprises, digital-centric companies and independent software vendors develop new products that can improve customer experience and drive business efficiency. He is a Generative AI Ambassador and a member of the Container Community. He lives in Dubai, UAE and loves riding his motorbike and traveling.