Amazon Bedrock RAG Sample

repository·main·Indexed 42 days ago

https://github.com/aws-samples/amazon-bedrock-rag

A fully managed Retrieval-Augmented Generation (RAG) solution using Amazon Bedrock Knowledge Bases. The project includes a React frontend, a backend API, and automated data ingestion from S3 and web sources, deployable via AWS CDK. It supports both Amazon OpenSearch Serverless and Managed Knowledge Bases for embedding, storage, and retrieval.

Tokens
6.9K
Snippets
24
Records
33
Agent score
53%

What's inside Amazon Bedrock RAG

  1. Use Managed Knowledge Bases instead of OpenSearch Serverless

    main

    Amazon Bedrock now supports Managed Knowledge Bases, which handle embedding, storage, and retrieval automatically without requiring an external vector store like OpenSearch Serverless. This reduces costs and supports agentic retrieval.

    Configuration:

    • To use managed retrieval in this project, set the environment variable KNOWLEDGE_BASE_TYPE=MANAGED in the Lambda function.
    • Managed KBs require boto3 >= 1.43.2.
    • Retrieval uses managedSearchConfiguration instead of vectorSearchConfiguration.

    Reranking Options for Managed Search:

    • MANAGED (default): Automatic reranking.
    • NONE: Disables reranking.
    • CUSTOM: Uses a specific Bedrock reranking model (e.g., Cohere Rerank v3.5).
  2. API Shapes for Managed Knowledge Bases

    main

    When configuring Managed Knowledge Bases, the following property shapes must be used instead of the standard vector configurations:

    • KB Creation: Set type: MANAGED and use managedKnowledgeBaseConfiguration.embeddingModelType: MANAGED.
    • Data Source: Use type: MANAGED_KNOWLEDGE_BASE_CONNECTOR for managed ingestion.
    • Retrieval: Use managedSearchConfiguration instead of vectorSearchConfiguration.
    • Agentic Retrieval: Use AgenticRetrieveStream with foundationModelType: MANAGED and rerankingModelType: MANAGED.
  3. Manage the backend CDK project

    main

    The backend is a CDK TypeScript project used to define and deploy the infrastructure for the RAG solution. You can use standard npm scripts for development and the AWS CDK CLI for deployment and synthesis.

    # Compile TypeScript to JavaScript
    npm run build
    
    # Watch for changes and compile automatically
    npm run watch
    
    # Run Jest unit tests
    npm run test
    
    # Deploy the stack to your default AWS account/region
    npx cdk deploy
    
    # Compare the deployed stack with your current local state
    npx cdk diff
    
    # Emit the synthesized CloudFormation template
    npx cdk synth
  4. Configure Managed Knowledge Bases via CDK context

    main

    By default, the CDK deployment uses VECTOR type Knowledge Bases. To deploy using the new Managed Knowledge Base support, you must pass the knowledgeBaseType=MANAGED context variable during the CDK deployment process. This opt-in mechanism ensures backward compatibility for existing VECTOR stacks.

    cdk deploy -c knowledgeBaseType=MANAGED
  5. Run the RAG frontend application

    main

    To launch the user interface for the Q&A chatbot:

    1. Navigate to the frontend folder.
    2. Install dependencies: npm install.
    3. Start the application: npm run start.
    4. In the browser interface:
      • Step 1: Enter the APIGatewayUrl obtained during backend deployment.
      • Step 2: Select a model (default is Anthropic Claude Instant).
      • Step 3: Enter a question (e.g., "Which cities have offices?") to receive an answer with citations.
    cd frontend
    npm install
    npm run start
  6. Deploy the RAG solution backend

    main

    To deploy the backend infrastructure using AWS CDK, follow these steps:

    1. Clone the repository to your local machine.
    2. Navigate to the backend folder and install dependencies using npm install.
    3. Deploy the stack using cdk deploy. You must provide a CIDR-formatted client IP address via the allowedip context variable to restrict API Gateway access.

    Important Outputs: After deployment, note the following values from the terminal output:

    • BackendStack.APIGatewayUrl: The endpoint for your API.
    • BackendStack.DocsBucketName: The name of the S3 bucket used for document storage.

    Note on Costs: This solution uses Amazon OpenSearch Serverless, which incurs a minimum charge of 4 OCUs. Ensure you run the cleanup command when finished.

    cd backend
    npm install
    cdk deploy --context allowedip="xxx.xxx.xxx.xxx/32"
  7. Synchronize the Web Crawler data source

    main

    The solution includes a web data source named WebCrawlerDataSource (initialized with https://www.aboutamazon.com/news/amazon-offices). Unlike S3 uploads, web content ingestion is scheduled and must be triggered manually to make it immediately available for querying.

    To sync web content:

    1. Open the Amazon Bedrock console.
    2. Navigate to your Knowledge Base.
    3. Select the WebCrawlerDataSource.
    4. Initiate a Sync operation.
  8. Upload documents to the S3 data source

    main

    The solution automatically synchronizes documents uploaded to the designated S3 bucket with the Amazon Bedrock knowledge base. The deployment watches for new content in the bucket and triggers an ingestion workflow automatically.

    To test this:

    1. Identify your DocsBucketName from the backend deployment outputs.
    2. Upload a PDF (e.g., an Amazon annual report) to that bucket using the AWS S3 Console.