Many e-commerce applications want to offer their users a human-like chatbot that will guide them in choosing the best product as a gift for their loved ones or friends. To enhance the customer experience, the chatbot should interact in a natural and conversational manner to understand the user’s preferences and requirements, such as the gender of the recipient, the occasion for the gift, and the desired product category. Based on the conversation with the user, the chatbot should be able to query the e-commerce product catalogue, filter the results, and recommend the most suitable products.
amazon Bedrock is a fully managed service that offers a selection of high-performance base models (FMs) from leading artificial intelligence (ai) companies such as AI21 Labs, Anthropic, Cohere, Meta, Mistral ai, Stability ai, and amazon through a single API, along with a broad set of capabilities to build 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 on enterprise systems and data sources. In this post, we show you how to build an e-commerce product recommendation chatbot using amazon Bedrock Agents and FM available on amazon Bedrock.
Solution Overview
Traditional rules-based chatbots often struggle to manage the nuances and complexities of open-ended conversations, leading to frustrating user experiences. Additionally, manually coding all possible conversation flows and product filtering logic is time-consuming and error-prone, especially as your product catalog grows.
To address this challenge, you need a solution that uses the latest advances in generative ai to create a natural conversational experience. The solution should seamlessly integrate with your existing product catalog API and dynamically adapt the conversation flow based on user responses, reducing the need for extensive coding.
With amazon Bedrock Agents, you can build intelligent chatbots that can converse naturally with users, understand their preferences, and efficiently retrieve and recommend the most relevant products from the catalog. amazon Bedrock Agents simplifies the process of building and deploying generative ai models, enabling businesses to create engaging, personalized conversational experiences without the need for extensive machine learning (ML) expertise.
For our use case, we built a recommendation chatbot using amazon Bedrock Agents that asks users to describe who they want to purchase the gift for and the corresponding occasion. The agent queries product information stored in an amazon DynamoDB table, using an API implemented as an AWS Lambda function. The agent tailors the API inputs to filter products based on its conversation with the user, for example, gender, occasion, and category. After obtaining the user’s gift preferences through clarifying questions, the agent responds with the most relevant products that are available in the DynamoDB table based on the user’s preferences.
The following diagram illustrates the solution architecture.
As shown in the diagram above, the e-commerce application first uses the agent to conduct conversation with users and generate product recommendations. The agent uses a Lambda-backed API to fetch product information. Finally, the Lambda function fetches product data from DynamoDB.
Prerequisites
You must have an AWS account with a user or role that has at least 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
Deploy solution resources with AWS CloudFormation
Before you create the agent, you need to set up the product database and API. We used an AWS CloudFormation template to create a DynamoDB table to store product information and a Lambda function to serve as an API to retrieve product details.
At the time of writing this post, you can use any of the following AWS Regions to deploy the solution: US East (N. Virginia), US West (Oregon), Asia Pacific (Mumbai, Sydney), Europe (Frankfurt, Paris), Canada (Central), or South America (São Paulo). Visit Regions and Models Supported by amazon Bedrock Agents for updates.
To deploy the template, select Launch stack:
This template creates a DynamoDB table named Products
with the following attributes: product_name
(partition key), category
, gender
and occasion
It also defines a global secondary index (GSI) for each of these attributes to allow efficient querying.
Additionally, the template configures a Lambda function named GetProductDetailsFunction
which acts as an API to retrieve product details. This Lambda function accepts query parameters like category
, gender
and occasion
Constructs a filter expression based on the provided parameters and scans the DynamoDB table to retrieve matching products. If no parameters are provided, it retrieves all products from the table and returns the first 100 products.
The template also creates another Lambda function called PopulateProductsTableFunction
which generates sample data to store in the Products
table. The CloudFormation template includes a custom resource that will run the PopulateProductsTableFunction
run once as part of the template deployment, to add 100 sample product entries to the DynamoDB products table, with various combinations of product names, descriptions, categories, genders, and occasions.
Optionally, you can update the sample product entries or replace them with your own product data. To do this, open the DynamoDB console, select Explore articlesand select the Products
table. Choose Scan and choose Run to view and edit current items or choose Create article To add a new item, if your data has different attributes than the sample product entries, you need to adjust the Lambda function code. GetProductDetailsFunction
the OpenAPI schema and agent instructions used in the next section.
Create the agent
Now that you have the infrastructure up and running, you can create the agent. The first step is to request access to the model.
- In the amazon Bedrock console, select Access to the model in the navigation panel.
- Choose Enabling specific models.
- Select the model you need to access (for this post, we selected Claude Sonnet 3).
Wait for the model access status to change to Access granted.
You can now create your agent. We used a CloudFormation template to create the agent and the action group that will invoke the Lambda function.
- To deploy the template, select Launch stack:
You can now check the details of the agent that was created by the stack.
- In the amazon Bedrock console, select Agents low Construction tools in the navigation panel.
- Choose the agent
product-recommendation-agent
then choose Edit in Agent Builder. - He Instructions for the Agent The section includes a set of instructions that guide the agent on how to communicate with the user and use the API. You can adjust the instructions based on different use cases and business scenarios, as well as the available APIs.
The agent's main goal is to engage in conversation with the user to gather information about the recipient's gender, the occasion for which the gift is being given, and the desired category. Based on this information, the agent will query the Lambda function to retrieve and recommend suitable products.
The next step is to verify the action group that allows the agent to invoke the Lambda function.
- In it Action groups section, select the
Get-Product-Recommendations
action group.
You can see the GetProductDetailsFunction
The Lambda function is selected in the Invocation of action group section.
In it Group of actions scheme In this section, you can see the OpenAPI schema, which allows the agent to understand the API description, inputs, outputs, and actions that it can use during the conversation with the user.
Now you can use the Testing agent Panel to have conversations with the chatbot.
Testing the chatbot
The following screenshots show example conversations, with the chatbot recommending products after calling the API.
In the sample conversation, the chatbot asks relevant questions to determine the gender of the gift recipient, the occasion, and the desired category. Once it has gathered enough information, it queries the API and presents a list of recommended products that match the user's preferences.
You can see the justification for each answer by choosing Show traceThe following screenshots show how the agent decided to use different API filters based on the discussion.
You can see it in the rationale
field in which the agent made their decision for each interaction. This tracking data can help you understand the reasons behind a recommendation. Recording this information can be beneficial for future improvements to your agent's recommendations.
Clean
Complete the following steps to clean up your resources:
- In the AWS CloudFormation console, delete the stack
AgentStack
. - Then clear the stack
Productstableandapi
.
Conclusion
In this post, we showed how to use amazon Bedrock Agents to build a conversational chatbot that can help users find the perfect gift. The chatbot intelligently collects user preferences, queries a backend API to retrieve relevant product details, and presents its recommendations to the user. This approach demonstrates the power of amazon Bedrock Agents to create engaging and contextual conversational experiences.
We recommend that you follow best practices when using amazon Bedrock agents. For example, using AWS CloudFormation to create and configure the agent allows you to minimize human error and recreate the agent in different environments and regions. Additionally, automating agent testing using a set of key questions and their expected answers allows you to test the quality of instructions to the agent and compare the results of different models in amazon Bedrock relative to your use case.
Visit amazon Bedrock Agents for more information on features and details.
About the author
Mahmoud Salaheldin is a Senior Solutions Architect at AWS, working with customers in the Middle East, North Africa, and Turkey, where he helps enterprises, digital-focused businesses, and independent software vendors innovate new products that can improve their customer experience and increase business efficiency. He is a Generative ai Ambassador and a member of the Container Community. He lives in Dubai, United Arab Emirates, and enjoys motorcycling and traveling.