AWS Machine Learning Blog
Build a foundation model (FM) powered customer service bot with Amazon Bedrock agents
From enhancing the conversational experience to agent assistance, there are plenty of ways that generative artificial intelligence (AI) and foundation models (FMs) can help deliver faster, better support. With the increasing availability and diversity of FMs, it’s difficult to experiment and keep up-to-date with the latest model versions. Amazon Bedrock is a fully managed service that offers a choice of high-performing FMs from leading AI companies such as AI21 Labs, Anthropic, Cohere, Meta, Stability AI, and Amazon. With Amazon Bedrock’s comprehensive capabilities, you can easily experiment with a variety of top FMs, customize them privately with your data using techniques such as fine-tuning and Retrieval Augmented Generation (RAG).
Amazon Bedrock Agents
In July, AWS announced the preview of Amazon Bedrock Agents, a new capability for developers to create fully managed agents in a few clicks. Agents extend FMs to run complex business tasks—from booking travel and processing insurance claims to creating ad campaigns and managing inventory—all without writing any code. With fully managed agents, you don’t have to worry about provisioning or managing infrastructure.
In this post, we provide a step-by-step guide with building blocks to create a customer service bot. We use a text generation model (Anthropic Claude V2) and Amazon Bedrock Agents for this solution. We provide an AWS CloudFormation template to provision the resources needed for building this solution. Then we walk you through steps to create an agent for Amazon Bedrock.
ReAct Prompting
FMs determine how to solve user-requested tasks with a technique called ReAct. It’s a general paradigm that combines reasoning and acting with FMs. ReAct prompts FMs to generate verbal reasoning traces and actions for a task. This allows the system to perform dynamic reasoning to create, maintain, and adjust plans for acting while incorporating additional information into the reasoning. The structured prompts include a sequence of question-thought-action-observation examples.
- The question is the user-requested task or problem to solve.
- The thought is a reasoning step that helps demonstrate to the FM how to tackle the problem and identify an action to take.
- The action is an API that the model can invoke from an allowed set of APIs.
- The observation is the result of carrying out the action.
Components in Amazon Bedrock Agents
Behind the scenes, Amazon Bedrock Agents automate the prompt engineering and orchestration of user-requested tasks. They can securely augment the prompts with company-specific information to provide responses back to the user in natural language. The agent breaks the user-requested task into multiple steps and orchestrates subtasks with the help of FMs. Action groups are tasks that the agent can perform autonomously. Action groups are mapped to an AWS Lambda function and related API schema to perform API calls. The following diagram depicts the agent structure.
Solution overview
We use a shoe retailer use case to build the customer service bot. The bot helps customers purchase shoes by providing options in a humanlike conversation. Customers converse with the bot in natural language with multiple steps invoking external APIs to accomplish subtasks. The following diagram illustrates the sample process flow.
The following diagram depicts a high-level architecture of this solution.
- You can create an agent with Amazon Bedrock-supported FMs such as Anthropic Claude V2.
- Attach API schema, residing in an Amazon Simple Storage Service (Amazon S3) bucket, and a Lambda function containing the business logic to the agent. (Note: This is a one-time setup step.)
- The agent uses customer requests to create a prompt using the ReAct framework. It, then, uses the API schema to invoke corresponding code in the Lambda function.
- You can perform a variety of tasks, including sending email notifications, writing to databases, and triggering application APIs in the Lambda functions.
In this post, we use the Lambda function to retrieve customer details, list shoes matching customer-preferred activity, and finally, place orders. Our code is backed by an in-memory SQLite database. You can use similar constructs to write to a persistent data store.
Prerequisites
To implement the solution provided in this post, you should have an AWS account and access to Amazon Bedrock with agents enabled (currently in preview). Use AWS CloudFormation template to create the resource stack needed for the solution.
us-east-1 |
The CloudFormation template creates two IAM roles. Update these roles to apply least-privilege permissions as discussed in Security best practices. Click here to learn what IAM features are available to use with Amazon Bedrock Agents.
LambdaBasicExecutionRole
with Amazon S3 full access and CloudWatch access for logging.AmazonBedrockExecutionRoleForAgents
with Amazon S3 full access and Lambda full access.
Important: Amazon Bedrock Agents must have the role name prefixed by AmazonBedrockExecutionRoleForAgents_*
Bedrock Agents setup
In the next two sections, we will walk you through creating and testing an agent.
Create an agent for Amazon Bedrock
To create an agent, open the Amazon Bedrock console and choose Agents in the left navigation pane. Then select Create Agent.
This starts the agent creation workflow.
- Provide agent details: Give the agent a name and description (optional). Select the service role created by the CloudFormation stack and select Next.
Select the service role created by the CloudFormation stack and select Next.
- Select a foundation model: In the Select model screen, you select a model. Provide clear and precise instructions to the agent about what tasks to perform and how to interact with the users.
- Add action groups: An action is a task the agent can perform by making API calls. A set of actions comprise an action group. You provide an API schema that defines all the APIs in the action group. You must provide an API schema in the OpenAPI schema JSON format. For this agent the API schema is available in the S3 bucket for this blog, at
s3://agentb8-x-<your-aws-account-id>/customerservicebot.json
. The Lambda function contains the business logic needed to perform API calls. You must associate a Lambda function to each action group.
Give the action group a name and a description for the action. Select the Lambda function, provide an API schema file and select Next.
- In the final step, review the agent configuration and select Create Agent.
Test and deploy Amazon Bedrock Agents
- Test the agent: After the agent is created, a dialog box shows the agent overview along with a working draft. The Amazon Bedrock console provides a UI to test your agent.
- Deploy: After successful testing, you can deploy your agent. To deploy an agent in your application, you must create an alias. Amazon Bedrock then automatically creates a version for that alias.
The following actions occur with the preceding agent setup and the Lambda code provided with this post:
- The agent creates a prompt from the developer-provided instructions (such as “You are an agent that helps customers purchase shoes.”), API schemas needed to complete the tasks, and data source details. The automatic prompt creation saves weeks of experimenting with prompts for different FMs.
- The agent orchestrates the user-requested task, such as “I am looking for shoes,” by breaking it into smaller subtasks such as getting customer details, matching the customer-preferred activity with shoe activity, and placing shoe orders. The agent determines the right sequence of tasks and handles error scenarios along the way.
The following screenshot displays some example responses from the agent.
By selecting Show trace for each response, a dialog box shows the reasoning technique used by the agent and the final response generated by the FM.
Cleanup
To avoid incurring future charges, delete the resources. You can do this by deleting the stack from the CloudFormation console.
Feel free to download and test the code used in this post from the GitHub Amazon Bedrock Agents repository. You can also invoke the Amazon Bedrock Agents programmatically; an example Jupyter Notebook is provided in the repository.
Conclusion
Amazon Bedrock Agents can help you increase productivity, improve your customer service experience, or automate DevOps tasks. In this post, we showed you how to set up Amazon Bedrock Agents to create a customer service bot.
We encourage you to learn more by reviewing additional features of Amazon Bedrock. You can use the example code provided in this post to create your implementation. Try our workshop to gain hands-on experience with Amazon Bedrock.
About the Authors
Amit Arora is an AI and ML Specialist Architect at Amazon Web Services, helping enterprise customers use cloud-based machine learning services to rapidly scale their innovations. He is also an adjunct lecturer in the MS data science and analytics program at Georgetown University in Washington D.C.
Manju Prasad is a Senior Solutions Architect within Strategic Accounts at Amazon Web Services. She focuses on providing technical guidance in a variety of domains, including AI/ML to a marquee M&E customer. Prior to joining AWS, she has worked for companies in the Financial Services sector and also a startup.
Archana Inapudi is a Senior Solutions Architect at AWS supporting Strategic Customers. She has over a decade of experience helping customers design and build data analytics, and database solutions. She is passionate about using technology to provide value to customers and achieve business outcomes.