├── Bedrock-rag-knowledgebase-agents-automation ├── AmazonBedrock_kb_agents.yml └── OpenSearch_serverless.yml ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md └── img ├── Architecture.png ├── Img1.png ├── Img2.png ├── Img3.png ├── Img4.png ├── Img5.png ├── Img6.png ├── Img7.png └── img_vectorfaiss.png /Bedrock-rag-knowledgebase-agents-automation/AmazonBedrock_kb_agents.yml: -------------------------------------------------------------------------------- 1 | AWSTemplateFormatVersion: 2010-09-09 2 | Description: 'Serverless RAG Q&A application using Knowledge Base , Agents, Opensearch Serverless' 3 | 4 | Parameters: 5 | KnowledgeBaseName: 6 | Default: rag-sagemaker-kb 7 | Type: String 8 | Description: The name of the knowledge base. 9 | KnowledgeBaseDescription: 10 | Default: Answer based only on information contained in knowledge base. 11 | Type: String 12 | Description: The description of the knowledge base. 13 | AmazonBedrockExecutionRoleForKnowledgeBasearn: 14 | Type: String 15 | Description: Execution Role for Knowledge Base Arn. 16 | 17 | AgentName: 18 | Default: rag-sagemaker-agent 19 | Type: String 20 | Description: The name of the agent. 21 | AOSSIndexName: 22 | Default: rag-sagemaker-readthedocs-io 23 | Type: String 24 | Description: Name of the vector index in the Amazon OpenSearch Service Serverless (AOSS) collection. You can get the name from the output section of the previous stack 25 | DataSource: 26 | 27 | Type: String 28 | Description: S3 bucket name from the previous stack. 29 | 30 | S3bucketarn: 31 | Type: String 32 | Description: S3 bucket arn from the previous stack. 33 | 34 | CollectionArn: 35 | Type: String 36 | Description: Collection Arn from the previous stack. 37 | 38 | Resources: 39 | 40 | 41 | AmazonBedrockExecutionRoleForAgentsQA: 42 | Type: AWS::IAM::Role 43 | Properties: 44 | RoleName: AmazonBedrockExecutionRoleForAgents_SageMakerQA 45 | AssumeRolePolicyDocument: 46 | Statement: 47 | - Effect: Allow 48 | Principal: 49 | Service: bedrock.amazonaws.com 50 | Action: sts:AssumeRole 51 | ManagedPolicyArns: 52 | - arn:aws:iam::aws:policy/AmazonBedrockFullAccess 53 | 54 | 55 | 56 | KnowledgeBaseWithAoss: 57 | Type: AWS::Bedrock::KnowledgeBase 58 | Properties: 59 | Name: !Ref KnowledgeBaseName 60 | Description: !Ref KnowledgeBaseDescription 61 | RoleArn: !Ref AmazonBedrockExecutionRoleForKnowledgeBasearn 62 | KnowledgeBaseConfiguration: 63 | Type: "VECTOR" 64 | VectorKnowledgeBaseConfiguration: 65 | EmbeddingModelArn: !Sub "arn:${AWS::Partition}:bedrock:${AWS::Region}::foundation-model/amazon.titan-embed-text-v1" 66 | StorageConfiguration: 67 | Type: "OPENSEARCH_SERVERLESS" 68 | OpensearchServerlessConfiguration: 69 | CollectionArn: !Ref CollectionArn 70 | VectorIndexName: !Ref AOSSIndexName 71 | FieldMapping: 72 | VectorField: "vector" 73 | TextField: "text" 74 | MetadataField: "metadata" 75 | SampleDataSource: 76 | Type: AWS::Bedrock::DataSource 77 | Properties: 78 | KnowledgeBaseId: !Ref KnowledgeBaseWithAoss 79 | Name: !Ref DataSource 80 | DataSourceConfiguration: 81 | Type: "S3" 82 | S3Configuration: 83 | BucketArn: !Ref S3bucketarn 84 | 85 | 86 | AgentResource: 87 | Type: AWS::Bedrock::Agent 88 | Properties: 89 | AgentName: !Ref AgentName 90 | AgentResourceRoleArn: !GetAtt AmazonBedrockExecutionRoleForAgentsQA.Arn 91 | AutoPrepare: true 92 | FoundationModel: "anthropic.claude-v2" 93 | Instruction: "You are a Q&A bot to answer questions on Amazon SageMaker" 94 | Description: "Description is here" 95 | IdleSessionTTLInSeconds: 900 96 | KnowledgeBases: 97 | - KnowledgeBaseId: !Ref KnowledgeBaseWithAoss 98 | Description: !Ref KnowledgeBaseDescription 99 | KnowledgeBaseState: ENABLED 100 | -------------------------------------------------------------------------------- /Bedrock-rag-knowledgebase-agents-automation/OpenSearch_serverless.yml: -------------------------------------------------------------------------------- 1 | AWSTemplateFormatVersion: 2010-09-09 2 | Description: 'Serverless RAG Q&A application using Knowledge Base , Agents, Opensearch Serverless' 3 | 4 | Parameters: 5 | IAMUserArn: 6 | Description: The Arn of the IAM user (or assumed role) running this CloudFormation template. 7 | Type: String 8 | AOSSCollectionName: 9 | Default: rag-sagemaker-kb 10 | Type: String 11 | Description: Name of the Amazon OpenSearch Service Serverless (AOSS) collection. 12 | MinLength: 1 13 | MaxLength: 21 14 | AllowedPattern: ^[a-z0-9](-*[a-z0-9])* 15 | ConstraintDescription: Must be lowercase or numbers with a length of 1-63 characters. 16 | AOSSIndexName: 17 | Default: rag-sagemaker-readthedocs-io 18 | Type: String 19 | Description: Name of the vector index in the Amazon OpenSearch Service Serverless (AOSS) collection. 20 | 21 | Resources: 22 | 23 | S3Bucket: 24 | Type: AWS::S3::Bucket 25 | Description: Creating Amazon S3 bucket to hold source data for knowledge base 26 | Properties: 27 | BucketName: !Join 28 | - '-' 29 | - - !Ref AOSSCollectionName 30 | - !Sub ${AWS::AccountId} 31 | BucketEncryption: 32 | ServerSideEncryptionConfiguration: 33 | - ServerSideEncryptionByDefault: 34 | SSEAlgorithm: AES256 35 | 36 | cleanupBucketOnDelete: 37 | Type: Custom::cleanupbucket 38 | Properties: 39 | ServiceToken: !GetAtt 'DeleteS3Bucket.Arn' 40 | BucketName: !Ref S3Bucket 41 | DependsOn: S3Bucket 42 | 43 | AmazonBedrockExecutionRoleForKnowledgeBase: 44 | Type: AWS::IAM::Role 45 | Properties: 46 | RoleName: !Join 47 | - '-' 48 | - - AmazonBedrockExecutionRoleForKnowledgeBase 49 | - !Ref AOSSCollectionName 50 | AssumeRolePolicyDocument: 51 | Statement: 52 | - Effect: Allow 53 | Principal: 54 | Service: bedrock.amazonaws.com 55 | Action: sts:AssumeRole 56 | Condition: 57 | StringEquals: 58 | "aws:SourceAccount": !Sub "${AWS::AccountId}" 59 | ArnLike: 60 | "AWS:SourceArn": !Sub "arn:aws:bedrock:${AWS::Region}:${AWS::AccountId}:knowledge-base/*" 61 | Path: / 62 | Policies: 63 | - PolicyName: S3ReadOnlyAccess 64 | PolicyDocument: 65 | Version: '2012-10-17' 66 | Statement: 67 | - Effect: Allow 68 | Action: 69 | - s3:Get* 70 | - s3:List* 71 | - s3:Describe* 72 | - s3-object-lambda:Get* 73 | - s3-object-lambda:List* 74 | Resource: 75 | - arn:aws:s3:::aws-blogs-artifacts-public/* 76 | - !Sub arn:aws:s3:::${S3Bucket}/* 77 | - PolicyName: AOSSAPIAccessAll 78 | PolicyDocument: 79 | Version: '2012-10-17' 80 | Statement: 81 | - Effect: Allow 82 | Action: 83 | - aoss:APIAccessAll 84 | Resource: !Sub arn:aws:aoss:${AWS::Region}:${AWS::AccountId}:collection/* 85 | - PolicyName: BedrockListAndInvokeModel 86 | PolicyDocument: 87 | Version: '2012-10-17' 88 | Statement: 89 | - Effect: Allow 90 | Action: 91 | - bedrock:ListCustomModels 92 | Resource: '*' 93 | - Effect: Allow 94 | Action: 95 | - bedrock:InvokeModel 96 | Resource: !Sub arn:aws:bedrock:${AWS::Region}::foundation-model/* 97 | 98 | DeleteS3Bucket: 99 | Type: AWS::Lambda::Function 100 | Properties: 101 | Handler: index.lambda_handler 102 | Description: "Delete all objects in S3 bucket" 103 | Timeout: 30 104 | Role: !GetAtt 'LambdaBasicExecutionRole.Arn' 105 | Runtime: python3.9 106 | Environment: 107 | Variables: 108 | BUCKET_NAME: !Ref S3Bucket 109 | Code: 110 | ZipFile: | 111 | import json, boto3, logging 112 | import cfnresponse 113 | logger = logging.getLogger() 114 | logger.setLevel(logging.INFO) 115 | 116 | def lambda_handler(event, context): 117 | logger.info("event: {}".format(event)) 118 | try: 119 | bucket = event['ResourceProperties']['BucketName'] 120 | logger.info("bucket: {}, event['RequestType']: {}".format(bucket,event['RequestType'])) 121 | if event['RequestType'] == 'Delete': 122 | s3 = boto3.resource('s3') 123 | bucket = s3.Bucket(bucket) 124 | for obj in bucket.objects.filter(): 125 | logger.info("delete obj: {}".format(obj)) 126 | s3.Object(bucket.name, obj.key).delete() 127 | 128 | sendResponseCfn(event, context, cfnresponse.SUCCESS) 129 | except Exception as e: 130 | logger.info("Exception: {}".format(e)) 131 | sendResponseCfn(event, context, cfnresponse.FAILED) 132 | 133 | def sendResponseCfn(event, context, responseStatus): 134 | responseData = {} 135 | responseData['Data'] = {} 136 | cfnresponse.send(event, context, responseStatus, responseData, "CustomResourcePhysicalID") 137 | 138 | CustomSGResource: 139 | Type: AWS::CloudFormation::CustomResource 140 | Properties: 141 | ServiceToken: !GetAtt 'CustomFunctionCopyContentsToS3Bucket.Arn' 142 | 143 | 144 | LambdaBasicExecutionRole: 145 | Type: AWS::IAM::Role 146 | Properties: 147 | AssumeRolePolicyDocument: 148 | Statement: 149 | - Effect: Allow 150 | Principal: 151 | Service: lambda.amazonaws.com 152 | Action: sts:AssumeRole 153 | Path: / 154 | Policies: 155 | - PolicyName: S3Access 156 | PolicyDocument: 157 | Version: '2012-10-17' 158 | Statement: 159 | - Effect: Allow 160 | Action: 161 | - logs:CreateLogGroup 162 | - logs:CreateLogStream 163 | - logs:PutLogEvents 164 | Resource: arn:aws:logs:*:*:* 165 | - Effect: Allow 166 | Action: 167 | - s3:GetObject 168 | - s3:PutObject 169 | - s3:DeleteObject 170 | Resource: 171 | - arn:aws:s3:::aws-blogs-artifacts-public/* 172 | - !Sub arn:aws:s3:::${S3Bucket}/* 173 | 174 | 175 | CustomFunctionCopyContentsToS3Bucket: 176 | Type: AWS::Lambda::Function 177 | Properties: 178 | Handler: index.lambda_handler 179 | Description: "Copies files from the Blog bucket to bucket in this account" 180 | Timeout: 30 181 | Role: !GetAtt 'LambdaBasicExecutionRole.Arn' 182 | Runtime: python3.9 183 | Environment: 184 | Variables: 185 | AOSS_COLLECTION_NAME: !Ref AOSSCollectionName 186 | Code: 187 | ZipFile: | 188 | import os 189 | import json 190 | import boto3 191 | import logging 192 | import cfnresponse 193 | 194 | logger = logging.getLogger() 195 | logger.setLevel(logging.INFO) 196 | DATA_BUCKET = "aws-blogs-artifacts-public" 197 | SRC_PREFIX = "artifacts/ML-15729" 198 | MANIFEST = os.path.join(SRC_PREFIX, "manifest.txt") 199 | # s3://aws-blogs-artifacts-public/artifacts/ML-15729/docs/manifest.txt 200 | def lambda_handler(event, context): 201 | logger.info('got event {}'.format(event)) 202 | if event['RequestType'] == 'Delete': 203 | logger.info(f"copy files function called at the time of stack deletion, skipping") 204 | response = dict(files_copied=0, error=None) 205 | cfnresponse.send(event, context, cfnresponse.SUCCESS, response) 206 | return 207 | try: 208 | s3 = boto3.client('s3') 209 | obj = s3.get_object(Bucket=DATA_BUCKET, Key=MANIFEST) 210 | manifest_data = obj['Body'].iter_lines() 211 | ctr = 0 212 | for f in manifest_data: 213 | fname = f.decode() 214 | key = os.path.join(SRC_PREFIX, fname) 215 | logger.info(f"going to read {key} from bucket={DATA_BUCKET}") 216 | copy_source = { 'Bucket': DATA_BUCKET, 'Key': key } 217 | account_id = boto3.client('sts').get_caller_identity().get('Account') 218 | bucket = boto3.resource('s3').Bucket(f"{os.environ.get('AOSS_COLLECTION_NAME')}-{account_id}") 219 | dst_key = fname 220 | logger.info(f"going to copy {copy_source} -> s3://{bucket}/{dst_key}") 221 | bucket.copy(copy_source, dst_key) 222 | ctr += 1 223 | response = dict(files_copied=ctr, error=None) 224 | cfnresponse.send(event, context, cfnresponse.SUCCESS, response) 225 | except Exception as e: 226 | logger.error(e) 227 | response = dict(files_copied=0, error=str(e)) 228 | cfnresponse.send(event, context, cfnresponse.FAILED, response) 229 | 230 | return 231 | DataAccessPolicy: 232 | Type: 'AWS::OpenSearchServerless::AccessPolicy' 233 | Properties: 234 | Name: !Join 235 | - '-' 236 | - - !Ref AOSSCollectionName 237 | - access-policy 238 | Type: data 239 | Description: Access policy for AOSS collection 240 | Policy: !Sub >- 241 | [{"Description":"Access for cfn user","Rules":[{"ResourceType":"index","Resource":["index/*/*"],"Permission":["aoss:*"]}, 242 | {"ResourceType":"collection","Resource":["collection/quickstart"],"Permission":["aoss:*"]}], 243 | "Principal":["${IAMUserArn}", "${AmazonBedrockExecutionRoleForKnowledgeBase.Arn}"]}] 244 | NetworkPolicy: 245 | Type: 'AWS::OpenSearchServerless::SecurityPolicy' 246 | Properties: 247 | Name: !Join 248 | - '-' 249 | - - !Ref AOSSCollectionName 250 | - network-policy 251 | Type: network 252 | Description: Network policy for AOSS collection 253 | Policy: !Sub >- 254 | [{"Rules":[{"ResourceType":"collection","Resource":["collection/${AOSSCollectionName}"]}, {"ResourceType":"dashboard","Resource":["collection/${AOSSCollectionName}"]}],"AllowFromPublic":true}] 255 | EncryptionPolicy: 256 | Type: 'AWS::OpenSearchServerless::SecurityPolicy' 257 | Properties: 258 | Name: !Join 259 | - '-' 260 | - - !Ref AOSSCollectionName 261 | - security-policy 262 | Type: encryption 263 | Description: Encryption policy for AOSS collection 264 | Policy: !Sub >- 265 | {"Rules":[{"ResourceType":"collection","Resource":["collection/${AOSSCollectionName}"]}],"AWSOwnedKey":true} 266 | Collection: 267 | Type: 'AWS::OpenSearchServerless::Collection' 268 | Properties: 269 | Name: !Ref AOSSCollectionName 270 | Type: VECTORSEARCH 271 | Description: Collection to holds vector search data 272 | DependsOn: EncryptionPolicy 273 | 274 | Outputs: 275 | S3Bucket: 276 | Value: !GetAtt S3Bucket.Arn 277 | S3BucketName: 278 | Value: !Ref S3Bucket 279 | DashboardURL: 280 | Value: !GetAtt Collection.DashboardEndpoint 281 | 282 | AmazonBedrockExecutionRoleForKnowledgeBase: 283 | Value: !GetAtt AmazonBedrockExecutionRoleForKnowledgeBase.Arn 284 | 285 | CollectionARN: 286 | Value: !GetAtt Collection.Arn 287 | FilesCopied: 288 | Description: Files copied 289 | Value: !GetAtt 'CustomSGResource.files_copied' 290 | FileCopyError: 291 | Description: Files copy error 292 | Value: !GetAtt 'CustomSGResource.error' 293 | AOSSVectorIndexName: 294 | Description: vector index 295 | Value: !Ref AOSSIndexName 296 | Region: 297 | Description: Deployed Region 298 | Value: !Ref AWS::Region 299 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | ## Code of Conduct 2 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 3 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 4 | opensource-codeofconduct@amazon.com with any additional questions or comments. 5 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, correction, or additional 4 | documentation, we greatly value feedback and contributions from our community. 5 | 6 | Please read through this document before submitting any issues or pull requests to ensure we have all the necessary 7 | information to effectively respond to your bug report or contribution. 8 | 9 | 10 | ## Reporting Bugs/Feature Requests 11 | 12 | We welcome you to use the GitHub issue tracker to report bugs or suggest features. 13 | 14 | When filing an issue, please check existing open, or recently closed, issues to make sure somebody else hasn't already 15 | reported the issue. Please try to include as much information as you can. Details like these are incredibly useful: 16 | 17 | * A reproducible test case or series of steps 18 | * The version of our code being used 19 | * Any modifications you've made relevant to the bug 20 | * Anything unusual about your environment or deployment 21 | 22 | 23 | ## Contributing via Pull Requests 24 | Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that: 25 | 26 | 1. You are working against the latest source on the *main* branch. 27 | 2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already. 28 | 3. You open an issue to discuss any significant work - we would hate for your time to be wasted. 29 | 30 | To send us a pull request, please: 31 | 32 | 1. Fork the repository. 33 | 2. Modify the source; please focus on the specific change you are contributing. If you also reformat all the code, it will be hard for us to focus on your change. 34 | 3. Ensure local tests pass. 35 | 4. Commit to your fork using clear commit messages. 36 | 5. Send us a pull request, answering any default questions in the pull request interface. 37 | 6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation. 38 | 39 | GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and 40 | [creating a pull request](https://help.github.com/articles/creating-a-pull-request/). 41 | 42 | 43 | ## Finding contributions to work on 44 | Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any 'help wanted' issues is a great place to start. 45 | 46 | 47 | ## Code of Conduct 48 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 49 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 50 | opensource-codeofconduct@amazon.com with any additional questions or comments. 51 | 52 | 53 | ## Security issue notifications 54 | If you discover a potential security issue in this project we ask that you notify AWS/Amazon Security via our [vulnerability reporting page](http://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public github issue. 55 | 56 | 57 | ## Licensing 58 | 59 | See the [LICENSE](LICENSE) file for our project's licensing. We will ask you to confirm the licensing of your contribution. 60 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT No Attribution 2 | 3 | Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so. 10 | 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 12 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 13 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 14 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 15 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 16 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 17 | 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bedrock Knowledgebase Agent Workload Iac 2 | 3 | # Title 4 | 5 | Quickly deploy a serverless RAG application using Amazon Bedrock KnowledgeBase , Amazon Bedrock agents and OpenSearch Serverless. 6 | 7 | # Introduction 8 | 9 | In today's fast-paced digital landscape , the role of Generative AI chatbots has become increasingly vital. 10 | Retrieval-Augmented Generation (RAG) is the process of optimizing the output of a large language model, so it references an authoritative knowledge base outside of its training data sources before generating a response. Large Language Models (LLMs) are trained on vast volumes of data and use billions of parameters to generate original output for tasks like answering questions, translating languages, and completing sentences. RAG extends the already powerful capabilities of LLMs to specific domains or an organization's internal knowledge base, all without the need to retrain the model. It is a cost-effective approach to improving LLM output so it remains relevant, accurate, and useful in various contexts. 11 | 12 | Chatbot development typically begins using a foundation model. Foundation models (FMs) are API-accessible LLMs trained on a broad spectrum of generalized and unlabeled data. The computational and financial costs of retraining FMs for organization or domain-specific information are high. [RAG](https://aws.amazon.com/what-is/retrieval-augmented-generation/) is a more cost-effective approach to introducing new data to the LLM. It makes generative artificial intelligence (generative AI) technology more broadly accessible and usable. 13 | 14 | In this post , we will deploy an enterprise grade Q&A, Serverless RAG application using Amazon Bedrock agents, Amazon Bedrock KnowledgeBase and OpenSearch Serverless and Amazon Titan Text Embeddings V1. 15 | To equip FMs with up-to-date and proprietary information, organizations use Retrieval Augmented Generation (RAG), a technique that fetches data from company data sources and enriches the prompt to provide more relevant and accurate responses. Knowledge Bases for Amazon Bedrock is a fully managed capability that helps you implement the entire RAG workflow from ingestion to retrieval and prompt augmentation without having to build custom integrations to data sources and manage data flows. 16 | [Knowledge Bases](https://aws.amazon.com/bedrock/knowledge-bases/) gives you a fully managed RAG experience and the easiest way to get started with RAG in Amazon Bedrock. Knowledge Bases now manages the initial vector store setup, handles the embedding and querying, and provides source attribution and short-term memory needed for production RAG applications. If needed, you can also customize the RAG workflows to meet specific use case requirements or integrate RAG with other generative artificial intelligence (AI) tools and applications. 17 | [Agents for Amazon Bedrock](https://aws.amazon.com/bedrock/agents/) creates a prompt from the developer-provided instructions (E.g., “You are an insurance agent designed to process open claims”), API details needed to complete the tasks, and company data source details from knowledge bases. The automatic prompt creation saves weeks of experimenting with prompts for different FMs. 18 | 19 | In this proposed solution , the Amazon Bedrock KnowledgeBase agent ingests the text corpus, which represents an enterprise knowledge base and is stored as HTML files in Amazon S3, into an index in an Amazon OpenSearch Serverless collection in the form of text embeddings. 20 | 21 | This repository sample consists of 2 Cloudformation templates to provision all the resources required to deploy the application end-end. 22 | The implementation consists an Amazon S3 as the datasource which stores your data that needs to be ingested into a knowledge base i.e. a vector database such as Amazon OpenSearch Service Serverless (AOSS). This will make it possible to lookup when a question is received. 23 | 24 | # Solution Overview 25 | 26 | The information is stored as HTML files within an S3 bucket, serving as the primary data source for the Amazon Bedrock Knowledge Base. Subsequently, the Bedrock knowledge base agent reads and break down these files into smaller segments, encoding them into vectors using Amazon Titan Text Embeddings V1. The encoded segments are stored as index within an OpenSearch Serverless Collection. The RAG functionality is applied within the console, enabling questions to be posed to the Claude Model based on the documents retrieved from OpenSearch Serverless using RAG approach. 27 | 28 | 29 | The following figure represents the high-level architecture of the proposed solution : 30 | 31 | ![Architecture](img/Architecture.png) 32 | 33 | 34 | As illustrated in the architecture diagram, we use the following AWS 35 | services: 36 | 37 | - [Bedrock](https://aws.amazon.com/bedrock/) for access to the FMs for 38 | embedding and text generation as well as for the knowledge base agent. 39 | - [OpenSearch Service Serverless with vector 40 | search](https://aws.amazon.com/opensearch-service/serverless-vector-engine/) 41 | for storing the embeddings of the enterprise knowledge corpus and 42 | doing similarity search with user questions. 43 | - [S3](https://aws.amazon.com/pm/serv-s3/) for storing the raw knowledge 44 | corpus data (HTML files). 45 | - [AWS Identity and Access Management](https://aws.amazon.com/iam/) 46 | roles and policies for access management. 47 | - [AWS CloudFormation](https://aws.amazon.com/cloudformation/) for 48 | creating the entire solution stack through infrastructure as code. 49 | 50 | 51 | # Deployment Guide 52 | 53 | In the following sections, we discuss the key steps to deploy the solution, including pre-deployment and post-deployment. 54 | 55 | # Pre-Deployment 56 | An AWS account to deploy the resources. Please use the link to sign-up if you do not have an account [AWS 57 | account](https://signin.aws.amazon.com/signin?redirect_uri=https%3A%2F%2Fportal.aws.amazon.com%2Fbilling%2Fsignup%2Fresume&client_id=signup) 58 | 59 | **Note** Navigate to Amazon Bedrock Console and ensure that you have access to the models you are going to use in this solution 60 | 61 | Clone the repository using the command 62 | git clone 63 | 64 | # Deployment Steps 65 | The solution deployment automation script uses two parameterized CloudFormation template, OpenSearch_serverless.yml and AmazonBedrock_kb_agents.yml, to automate provisioning of following solution resources: 66 | 67 | 1. OpenSearch Service Serverless collection 68 | 2. Amazon S3 Bucket (DataSource) 69 | 3. Amazon Bedrock KnowledgeBase 70 | 4. Amazon Bedrock Agent 71 | 5. IAM Roles 72 | 73 | 74 | # Cloudformation to deploy OpenSearch_serverless.yml stack 75 | AWS CloudFormation prepopulates stack parameters with the default values provided in the template except for ARN of the IAM role with which you are 76 | currently logged into your AWS account which you’d have to provide. To provide alternative input values, you can specify parameters as environment variables that are referenced in the `ParameterKey=,ParameterValue=` pairs in the following shell script’s `aws cloudformation create-stack --stack-name --template-body file://OpenSearch_serverless.yml --parameters ParameterKey=,ParameterValue= ParameterKey=,ParameterValue=` .... 77 | 78 | 79 | 80 | **Currently the stack can only be deployed in us-east-1 and us-west-2** 81 | 82 | Once the Cloudformation stack creation is successful navigate to the Output section of the stack and grab the following output values AmazonBedrockExecutionRoleForKnowledgeBasearn , AOSSIndexName, CollectionArn,DataSource(S3 bucket name), S3bucketarn. We will use these values as parameters for our next stack AmazonBedrock_kb_agents.yml to deploy Amazon Bedrock Knowledgebase and agents. 83 | 84 | # Create Vector index in OpenSearch Serverless 85 | The previous CloudFormation stack creates OpenSearch Service Serverless collection,but the next step will require us to create a vector index in OpenSearch Service Serverless collection. Follow the steps outlined below : 86 | 87 | 1. Navigate to OpenSearch Service console and click on `Collections`. 88 | The `rag-sagemaker-kb` collection created by the CloudFormation stack 89 | will be listed there. 90 | 91 |
92 | Figure 3: SageMaker Knowledge Base Collection 94 | 96 |
97 | 98 | 2. Click on the `rag-sagemaker-kb` link to create a vector index for 99 | storing the embeddings from the documents in S3. 100 | 101 |
102 | Figure 4: SageMaker Knowledge Base Vector Index 105 | 107 |
108 | 109 | 3. Grab the vector index name from the output values of the previous stack, the default value is`rag-sagemaker-readthedocs-io`. Input the vector 110 | field name as `vector` dimensions as `1536`, choose engine types as `FAISS` and distance metric as 111 | `Euclidean`. **It is required that you set these parameters exactly 112 | as mentioned here because the Bedrock Knowledge Base Agent is going 113 | to use these same values**. 114 | 115 |
116 | Figure 5: SageMaker Knowledge Base Vector Index Parameters 119 | 121 |
122 | 123 | 4. Once created the vector index is listed as part of the collection. 124 | 125 |
126 | Figure 6: SageMaker Knowledge Base Vector Index Created 129 | 131 |
132 | 133 | # CloudFormation to deploy AmazonBedrock_kb_agents.yml 134 | 135 | Deploy the next stack using the following commands to provision the resources in your AWS account. 136 | 137 | `aws cloudformation create-stack --stack-name --template-body file://AmazonBedrock_kb_agents.yml --parameters ParameterKey=,ParameterValue= ParameterKey=,ParameterValue=` .... 138 | 139 | **Note** , grab the values of parameters from the output of the previous stack.Use these keys, **AmazonBedrockExecutionRoleForKnowledgeBasearn , AOSSIndexName, CollectionArn,DataSource(S3 bucket name), S3bucketarn** and the corresponding output values from previous stack to pass it as parameters when you are trying to create the 2nd stack 140 | 141 | # Test the RAG App in Amazon Bedrock Agents Console. 142 | 143 | 1. Navigate to Amazon Bedrock console and click on `Agents` 144 | The `rag-sagemaker-agent` knowledgebase agent created by the CloudFormation stack 145 | will be listed there. 146 | 147 |
148 | Figure 3: SageMaker Knowledge Base Collection 150 | 152 |
153 | 154 | 2. Click on the `rag-sagemaker-agent` to open it and ask the following questions to the agent using console. 155 |
156 | Figure 21: Agent console 158 | 159 |
160 | 3. Lets ask the agent a question such as `Tell me something about Amazon SageMaker Conditional Step`. Note that besides providing with the correct answer , the agent shares the correct documentation details that is stored in the S3 bucket which has been used to deduce the answer. 161 | 162 |
163 | Figure 22: Q&A with Bedrock Agent 165 | 167 |
168 | 169 | 4. Also notice that the each response from an Amazon Bedrock agent is accompanied by a **trace** that details the steps being orchestrated by the agent. The **trace** helps you follow the agent's reasoning process that leads it to the response it gives at that point in the conversation. 170 | 171 | Use the **trace** to track the agent's path from the user input to the response it returns. The trace provides information about the inputs to the action groups that the agent invokes and the knowledge bases that it queries to respond to the user. 172 | 173 | **Note** if the Agent is taking time to generate recommendations , navigate to Amazon Bedrock Knowledge bases and resync the data source and try again. 174 | 175 | # Deploy the Agent 176 | In this workshop we have deployed a working draft version(DRAFT). To deploy the agent into your application , follow the steps here : 177 | https://docs.aws.amazon.com/bedrock/latest/userguide/agents-deploy.html 178 | 179 | ## Clean up 180 | 181 | To avoid incurring future charges, delete the resources. You can do this 182 | by first deleting all the files from the S3 bucket created by the 183 | CloudFormation template and then deleting the CloudFormation stack. 184 | 185 | ## Conclusion 186 | 187 | In this codesample, we have demonstrated how to create an enterprise ready RAG solution 188 | using a combination of AWS services and CloudFormation. 189 | 190 | 191 | 192 | 193 | 194 | 195 | -------------------------------------------------------------------------------- /img/Architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws-samples/amazon-bedrock-rag-knowledgebases-agents-cloudformation/2ec14534401b9b6bcf5436e591834f4cd52394fc/img/Architecture.png -------------------------------------------------------------------------------- /img/Img1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws-samples/amazon-bedrock-rag-knowledgebases-agents-cloudformation/2ec14534401b9b6bcf5436e591834f4cd52394fc/img/Img1.png -------------------------------------------------------------------------------- /img/Img2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws-samples/amazon-bedrock-rag-knowledgebases-agents-cloudformation/2ec14534401b9b6bcf5436e591834f4cd52394fc/img/Img2.png -------------------------------------------------------------------------------- /img/Img3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws-samples/amazon-bedrock-rag-knowledgebases-agents-cloudformation/2ec14534401b9b6bcf5436e591834f4cd52394fc/img/Img3.png -------------------------------------------------------------------------------- /img/Img4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws-samples/amazon-bedrock-rag-knowledgebases-agents-cloudformation/2ec14534401b9b6bcf5436e591834f4cd52394fc/img/Img4.png -------------------------------------------------------------------------------- /img/Img5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws-samples/amazon-bedrock-rag-knowledgebases-agents-cloudformation/2ec14534401b9b6bcf5436e591834f4cd52394fc/img/Img5.png -------------------------------------------------------------------------------- /img/Img6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws-samples/amazon-bedrock-rag-knowledgebases-agents-cloudformation/2ec14534401b9b6bcf5436e591834f4cd52394fc/img/Img6.png -------------------------------------------------------------------------------- /img/Img7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws-samples/amazon-bedrock-rag-knowledgebases-agents-cloudformation/2ec14534401b9b6bcf5436e591834f4cd52394fc/img/Img7.png -------------------------------------------------------------------------------- /img/img_vectorfaiss.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws-samples/amazon-bedrock-rag-knowledgebases-agents-cloudformation/2ec14534401b9b6bcf5436e591834f4cd52394fc/img/img_vectorfaiss.png --------------------------------------------------------------------------------