aws-samples / serverless-rag-demo

Amazon Bedrock Foundation models with Amazon Opensearch Serverless as a Vector DB
https://aws.amazon.com/blogs/big-data/build-scalable-and-serverless-rag-workflows-with-a-vector-engine-for-amazon-opensearch-serverless-and-amazon-bedrock-claude-models/
MIT No Attribution
147 stars 47 forks source link
anthropic claude claude-3-5-sonnet claude-3-opus-20240229 haiku opensearch opensearchserverless rag sonnet

Scalable RAG solutions/Agentic Workflows with Amazon Bedrock and Amazon Opensearch serverless service

Overview

Widespread AI adoption is being driven by generative AI models that can generate human-like content. However, these foundation models are trained on general data making it less effective for domain specific tasks. There lies the importance of Retrieval Augmented Generation (RAG). RAG allows augmenting prompts with relevant external data for better domain-specific outputs. With RAG, documents and queries are converted to embeddings, compared to find relevant context, and that context is appended to the original prompt before being passed to the LLM. Knowledge libraries can be updated asynchronously to provide the most relevant external data for augmenting prompts.

Amazon Opensearch Serverless(AOSS) offers vector engine to store embeddings for faster similarity searches. The vector engine provides a simple, scalable, and high-performing similarity search capability in Amazon OpenSearch Serverless that makes it easy for you to build generative artificial intelligence (AI) applications without having to manage the underlying vector database infrastructure.

[!NOTE] This repository offers a production ready easily deployable Generative AI solution with the below features:

  1. Document chat
  2. Multi-Agent collaboration
  3. Sentiment Analysis
  4. PII Redaction
  5. OCR

[!IMPORTANT] The Older UI is maintained in the v0.0.1(Old-UI) branch.

Demos

Doc Chat/Doc Management (Multi-lingual) ![output](https://github.com/user-attachments/assets/7c1bd957-3053-4311-9422-86a5542ff2c3)
Multi-Agent Demo ![ma](https://github.com/user-attachments/assets/1d38d65a-cb4f-4533-84be-1ae784838917)
PII Redaction
OCR ![ocr](https://github.com/user-attachments/assets/dd635617-6b24-43b8-8dd4-8b8f40758083)
Sentiment Analysis
Latest project updates * 29-Oct-2024 Support for Claude-3.5 Sonnet V2/Opus for RAG/OCR/PII Identification/Sentiment Analysis * 1-Sept-204 Document Aware chunking strategy, to compare across several documents

Prerequisites

Prerequisites * [An AWS account](https://aws.amazon.com/console/) * [You should have access to Anthropic Claude-3 Haiku/Sonnet models on Amazon Bedrock](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html) * [For RAG, you should have access to Cohere English Embed model on Amazon Bedrock](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html) * [Amazon Bedrock supported regions](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html#bedrock-regions) * [Amazon Opensearch serverless(AOSS) supported regions](https://aws.amazon.com/about-aws/whats-new/2023/01/amazon-opensearch-serverless-available/) #### Familiarity with below Services * [AWS IAM](https://docs.aws.amazon.com/iam/index.html). * [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) * [Amazon API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html) * [Amazon opensearch serverless](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/serverless-overview.html)

Architecture

architecture

Deploying the Solution to your AWS account with AWS Cloudshell

Create an Admin User to deploy this stack #### Section1 - Create an IAM user with Administrator permissions (OPTIONAL: If you're already an Admin role, you may skip this step) 1. Search for the service IAM on the AWS Console and go the IAM Dashboard and click on “Roles“ tab under ”Access Management” and Click on “Create Role” Screenshot 2024-04-05 at 5 52 42 PM 2. Select AWS Account and click “Next“ role-iam 3. Under permissions select Administrator access Screenshot 2024-04-05 at 5 31 30 PM 4. Give the role a name and create the role Screenshot 2024-04-05 at 5 33 45 PM 5. You can now assume this role and proceed to deploy the stack. Click on Switch-Role assune-role 6. Switch role Screenshot 2024-04-05 at 5 42 06 PM 7. Proceed to cloudshell step
Deploy the RAG based Solution (Total deployment time 40 minutes) #### Section 2 - Deploy this RAG based Solution (The below commands should be executed in the region of deployment) 1. Switch to Admin role. Search for Cloudshell service on the AWS Console and follow the steps below to clone the github repository Screenshot 2024-04-05 at 5 48 41 PM 2. Git Clone the serverless-rag-demo repository from aws-samples ``` git clone https://github.com/aws-samples/serverless-rag-demo.git ``` 3. Go to the directory where we have the downloaded files. ``` cd serverless-rag-demo ``` 4. Fire the bash script that creates the RAG based solution. Pass the environment and region for deployment. environment can be dev,qa,sandbox. Look at Prerequisites to deploy to the correct region. ``` sh creator.sh ``` 5. Press **Enter** to proceed with deployment of the stack or **ctrl+c** to exit ![Cloudshell](media/CloudShell-deployment.png) 6. The UI is hosted on AppRunner the link to AppRunner could be found in CloudShell once the script execution is complete, or you could also go to the AppRunner service on the AWS Console and obtain the https url. The UI is authenticated through Amazon Cognito hence the very first time you would have to sign-up and then sign-in to login to the application ![AppRunnerUI](media/AppRunner_UI.png) 7. On Amazon Bedrock page enable access to the below models model_access
(ADVANCED) Using an existing Bedrock Knowledge base > [!IMPORTANT] > You could query your existing Knowledge base created on Amazon Bedrock provided it uses Amazon Opensearch Serverless service. #### Steps 1. Get the Collection ARN and the embedding model used by your Knowledge base on Bedrock ![Collection-ARN](media/Collection-ARN-and-ModelId.png) 2. Head to Amazon Opensearch Serverless and search by ARN to fetch Opensearch Endpoint ![OSS-Endpoint](media/Retrieve-Opensearch-Endpoint.png) 3. Modify the configurations of your `bedrock_rag_query_*` lambda function. Set the below a. IS_BEDROCK_KB = yes b. OPENSEARCH_VECTOR_ENDPOINT = <> c. EMBED_MODEL_ID = <>. Find the base model Id from here (https://docs.aws.amazon.com/bedrock/latest/userguide/model-ids.html) d. VECTOR_INDEX_NAME = <> e. BEDROCK_KB_EMBEDDING_KEY = <> ![Lambda-Config](media/Bedrock-KB-Integration.png) ![Bedrock-page](media/Bedrock-Page.png) 4. Get the ARN of the Lambda role ![Lambda-Config](media/Lambda-ARN.png) 5. Head to Amazon Opensearch on the AWS Console and click on Data Access Policies. Search for the Data Access Policy attached to your Bedrock KB and click on the `Edit` button ![Access-AOSS](media/Modify-DataAccessPolicy.png) 6. In the principal section add the ARN of your Lambda role and hit save ![Lambda-Access-AOSS](media/Add-lambda-arn-to-dataaccess.png) 7. Now try Document Chat on the UI, it should query from your Amazon Bedrock Knowledge base. > [!IMPORTANT] > We do not support indexing to an existing Knowledge base. That can be done through the Amazon Bedrock Console.