Bedrock LLM processing with Evaluation
This walkthrough guides you through deploying and using the Bedrock LLM Processor sample application, which demonstrates custom extraction using Amazon Bedrock models with evaluation capabilities.
Overview
The Bedrock LLM sample showcases how to:
- Process documents using custom extraction with Amazon Bedrock models
- Evaluate extraction results against baseline data
- Use different foundation models for classification and extraction
- Track document processing status through a GraphQL API
- View and manage documents through a web interface
Architecture
The sample deploys the following components:
- S3 buckets for input documents, processing results, and evaluation baselines
- AWS Step Functions workflow for orchestration
- AWS Lambda functions for processing tasks
- Amazon DynamoDB tables for configuration and tracking
- Amazon AppSync GraphQL API for querying processing status
- Amazon CloudFront distribution for the web interface
Prerequisites
Before you begin, ensure you have:
- AWS Account: With permissions to create the required resources
- AWS CLI: Configured with appropriate credentials
- Node.js: Version 18 or later (use NVM to install the version specified in
.nvmrc
) - AWS CDK: Version 2.x installed globally
- Docker: For building Lambda functions
- Amazon Bedrock Access: Ensure your account has access to Amazon Bedrock and the required models
Step 1: Clone the Repository
First, clone the GenAI IDP Accelerator repository and navigate to the sample directory:
git clone https://gitlab.aws.dev/genaiic-reusable-assets/engagement-artifacts/genaiic-idp-accelerator-cdk.git
cd genaiic-idp-accelerator-cdk/samples/sample-bedrock
Step 2: Install Dependencies
Install the required dependencies:
# Install the required Node.js version using NVM
nvm use
# Install dependencies
yarn install
# Create and activate a virtual environment
python -m venv .venv
source .venv/bin/activate
# Install dependencies
pip install -r requirements.txt
# Restore NuGet packages
dotnet restore
# Build the project
dotnet build
Step 3: Bootstrap Your AWS Environment
If you haven't already bootstrapped your AWS environment for CDK, run:
cdk bootstrap aws://ACCOUNT-NUMBER/REGION
Replace ACCOUNT-NUMBER
with your AWS account number and REGION
with your preferred AWS region.
Step 4: Review the Stack Configuration
The main stack is defined in src/bedrock-llm-stack.ts
. Let's examine the key components:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
Step 5: Deploy the Stack
Deploy the stack to your AWS account:
# Install dependencies
yarn install
# Build the project
yarn build
# Deploy the stack
cdk deploy
# Install dependencies
pip install -r requirements.txt
# Deploy the stack
cdk deploy
# Build the project
dotnet build
# Deploy the stack
cdk deploy
The deployment will take several minutes. Once complete, the command will output the necessary resource information.
Step 6: Configure Bedrock Model Access
Ensure you have access to the required Bedrock models:
- Go to the Amazon Bedrock console
- Navigate to "Model access" in the left sidebar
- Request access to the models used by the solution (e.g., Claude 3.5 Sonnet)
- Wait for access approval (usually immediate for most models)
Step 7: Upload Evaluation Baseline Documents
For evaluation to work properly, you need to upload baseline documents to the evaluation baseline bucket:
- Prepare your baseline documents with expected extraction results
- Upload them to the evaluation baseline bucket:
aws s3 cp baseline-document.json s3://YOUR-EVALUATION-BASELINE-BUCKET/
The baseline documents should follow this structure:
{
"documentId": "unique-document-id",
"expectedResults": {
"field1": "expected value 1",
"field2": "expected value 2",
"field3": {
"nestedField": "nested value"
}
}
}
Step 8: Test the Solution
Now you can test the solution by uploading a sample document:
- Upload a document to the input bucket:
aws s3 cp sample-document.pdf s3://YOUR-INPUT-BUCKET-NAME/
-
The document processing will start automatically. You can monitor the progress in the AWS Step Functions console.
-
Once processing is complete, you can retrieve the results from the output bucket:
aws s3 ls s3://YOUR-OUTPUT-BUCKET-NAME/
Step 9: Review Evaluation Results
After processing is complete, you can review the evaluation results:
- Navigate to the output bucket in the S3 console
- Find the processed document folder
- Open the evaluation results file
- Review the comparison between extracted data and baseline data
The evaluation results include: - Accuracy metrics for each extracted field - Overall extraction quality score - Suggestions for improving extraction quality
Step 10: Customize Extraction Schema
You can customize the extraction schema to target specific fields in your documents:
- Locate the DynamoDB configuration table created by the stack
- Update the schema configuration item with your custom schema
- The schema should define the fields to extract and their expected formats
Example schema:
{
"schema": {
"Invoice": {
"fields": [
{"name": "invoiceNumber", "description": "The invoice number"},
{"name": "date", "description": "The invoice date"},
{"name": "totalAmount", "description": "The total invoice amount"},
{"name": "vendor", "description": "The vendor name"}
]
},
"Receipt": {
"fields": [
{"name": "merchantName", "description": "The merchant name"},
{"name": "date", "description": "The receipt date"},
{"name": "totalAmount", "description": "The total amount"},
{"name": "items", "description": "List of purchased items"}
]
}
}
}
Step 11: Monitor Processing
You can monitor the document processing in several ways:
- AWS Step Functions Console: Monitor workflow executions
- CloudWatch Logs: View detailed processing logs
- DynamoDB Tables: Check the tracking table for processing status
- GraphQL API: Query processing status programmatically
Step 12: Clean Up
When you're done experimenting, you can clean up all resources to avoid incurring charges:
cdk destroy
Next Steps
Now that you have a working Bedrock LLM Processor solution with evaluation, consider:
- Refining your extraction schemas for better accuracy
- Creating more comprehensive baseline documents for evaluation
- Implementing custom post-processing logic for the extracted data
- Integrating the solution with your existing systems
- Exploring different foundation models for classification and extraction
For more information, refer to the Amazon Bedrock documentation and the GenAI IDP Accelerator documentation.