Retrieval Augmentation Generation (RAG) is a state-of-the-art approach to building question answering systems that combines the strengths of retrieval and generative language models. RAG models retrieve relevant information from large text corpora and synthesize answers based on the retrieved information using a generative language model.
Developing and deploying an end-to-end RAG solution is complex and involves multiple components: knowledge bases, search systems, generative language models, etc. Building and deploying these components can be complex and error-prone, especially when dealing with large data and models.
In this post, we explain how to use Amazon Bedrock knowledge base and the AWS Cloud Development Kit (AWS CDK) to seamlessly automate the deployment of an end-to-end RAG solution, enabling organizations to quickly set up a powerful question answering system.
Solution overview
This solution provides an automated end-to-end deployment of the RAG workflow using an Amazon Bedrock knowledge base. Using AWS CDK, the solution sets up the necessary resources, such as AWS Identity and Access Management (IAM) roles, Amazon OpenSearch Serverless collections and indexes, and a knowledge base with associated data sources.
The RAG workflow allows you to use document data stored in an Amazon Simple Storage Service (Amazon S3) bucket and integrate it with the powerful natural language processing (NLP) capabilities of the foundational models (FMs) provided by Amazon Bedrock. The solution simplifies the setup process by allowing you to programmatically modify the infrastructure, deploy your models, and start querying your data using your FM of choice.
Prerequisites
To implement the solution provided in this post, you will need:
- An active AWS account and knowledge of FM, Amazon Bedrock, and Amazon OpenSearch Service.
- Model access is enabled for the required models you plan to experiment with.
- The AWS CDK is already set up, see the AWS CDK Workshop for installation instructions.
- An S3 bucket set up with documents in supported formats (.txt, .md, .html, .doc/docx, .csv, .xls/.xlsx, .pdf).
- Amazon Titan Embeddings V2 model enabled on Amazon Bedrock. Model Access On the Amazon Bedrock console page, if the Amazon Titan Embeddings V2 model is enabled, the access status is displayed as follows: Allow AccessAs shown in the following screenshot.
Configure the solution
After you complete the prerequisite steps, you’re ready to set up the solution.
- Clone the GitHub repository that contains the solution files.
- Navigate to your solutions directory.
- Create and activate the virtual environment.
Activating a virtual environment varies by operating system, for activation in other environments, see the AWS CDK Workshop.
- Once your virtual environment is activated, you can install the required dependencies.
Now you can prepare a .zip file of the code and synthesize an AWS CloudFormation template for this code.
- In the terminal, export the AWS credentials for the role or user:
ACCOUNT_ID
The role must have all the permissions required for CDK deployment.
export AWS_REGION=”<リージョン>” # Same regionACCOUNT_REGION
On top of that
export AWS_ACCESS_KEY_ID=”<アクセスキー>” # Set the access keys for the role/user
export AWS_SECRET_ACCESS_KEY=”<秘密鍵>” # Set the role/user secret key - Create a dependency.
- If you are deploying the AWS CDK for the first time, run the following command:
- To synthesize the CloudFormation template, run the following command:
- This deployment contains multiple stacks that must be deployed in a specific order. Deploy the stacks in the following order:
- Once the deployment is complete, you can access the AWS CloudFormation console as shown below to view the deployed stack and also view the knowledge base details (name, ID, etc.) under the Resources tab.
Test the solution
Now that you have deployed the solution using the AWS CDK, you can test it by following these steps:
- On the Amazon Bedrock console, Knowledge Base On the navigation page.
- Select the knowledge base you created.
- choose Synchronization Start the data ingestion job.
- Once the data ingestion job is complete, select the desired FM to use for acquisition and generation. (You will need to grant model access to this FM in Amazon Bedrock before you can use it.)
- Start querying your data using natural language queries.
That’s it, you can now work with documents using RAG workflows powered by Amazon Bedrock.
cleaning
To avoid incurring future charges on your AWS account, follow these steps:
- Delete all files in the provisioned S3 bucket.
- To delete the CloudFormation stack that was provisioned using the AWS CDK, run the following command in your terminal:
Conclusion
In this post, I showed you how to use your knowledge base of Amazon Bedrock and the AWS CDK to quickly deploy an end-to-end RAG solution.
The solution streamlines the process of setting up the required infrastructure, including IAM roles, OpenSearch Serverless collections and indexes, and a knowledge base with relevant data sources. The automated deployment process enabled by AWS CDK minimizes the complexity and potential errors associated with manually configuring and deploying the various components required for a RAG solution. By leveraging the power of FM provided by Amazon Bedrock, you can seamlessly integrate your document data with advanced NLP capabilities to efficiently retrieve relevant information and generate high-quality answers to natural language queries.
The solution not only simplifies the deployment process, but also enables a scalable and efficient use of RAG capabilities in question answering systems. The ability to programmatically modify the infrastructure allows the solution to be quickly tailored to meet the specific needs of an organization, making it a valuable tool for a wide range of applications that require the capture and generation of accurate, contextual information.
About the Author
Sandeep Singh He is a Senior Generative AI Data Scientist at Amazon Web Services, helping enterprises innovate with Generative AI. He specializes in Generative AI, Machine Learning and System Design. He has successfully delivered cutting edge AI/ML powered solutions to solve complex business problems across industries, optimizing efficiency and scalability.
Manoj Krishna Mohan Manoj is a Machine Learning Engineer at Amazon. He specializes in building AI/ML solutions using Amazon SageMaker. He is passionate about developing customer-ready solutions. Manoj holds a Masters in Computer Science with a focus on Data Science from the University of North Carolina at Charlotte.
Mani Kanuja She is a technical lead for the Generative AI Specialist, author of Applied Machine Learning and High-Performance Computing on AWS, and a member of the Women in Manufacturing Education Foundation Board. She leads machine learning projects in a variety of areas including computer vision, natural language processing, and generative AI. She has spoken at internal and external conferences including AWS re:Invent, Women in Manufacturing West, YouTube webinars, and GHC 23. In her free time, she enjoys long runs along the beach.