Skip to content

Commit dadff4c

Browse files
author
Daniel Lorch
committed
feat: add documentation for dynamic few-shot examples
1 parent 3fad958 commit dadff4c

File tree

1 file changed

+262
-4
lines changed

1 file changed

+262
-4
lines changed

docs/few-shot-examples.md

Lines changed: 262 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -849,16 +849,274 @@ extraction:
849849
{DOCUMENT_TEXT}
850850
```
851851

852+
## Dynamic Few-Shot Prompting
853+
854+
Dynamic few-shot prompting takes the few-shot examples feature to the next level by automatically selecting the most relevant examples based on document similarity using vector search. Instead of using static examples defined in your configuration, the system dynamically retrieves similar documents from a vector database at processing time.
855+
856+
### Overview
857+
858+
Dynamic few-shot prompting uses Amazon S3 Vectors and Amazon Nova multimodal embeddings to:
859+
- **Automatically find similar examples** based on visual and content similarity
860+
- **Retrieve relevant examples** from a large dataset without manual configuration
861+
- **Adapt to document variations** by selecting the most appropriate examples for each document
862+
- **Scale to large example datasets** without impacting configuration complexity
863+
864+
### How It Works
865+
866+
```mermaid
867+
flowchart TD
868+
A[Document Processing] --> B[Generate Document Embedding]
869+
B --> C[Query S3 Vectors Index]
870+
C --> D[Retrieve Top-K Similar Examples]
871+
D --> E[Load Example Images from S3]
872+
E --> F[Format Examples for Prompt]
873+
F --> G[Use in Extraction]
874+
875+
subgraph "S3 Vectors"
876+
H[Vector Index]
877+
I[Example Metadata]
878+
J[Similarity Search]
879+
end
880+
881+
C --> H
882+
H --> J
883+
J --> I
884+
I --> D
885+
```
886+
887+
### Key Benefits
888+
889+
- **🎯 Automatic Relevance**: Examples are selected based on actual similarity to the document being processed
890+
- **📈 Scalability**: Support for large example datasets (hundreds or thousands of examples)
891+
- **🔄 Dynamic Adaptation**: Different examples are selected for different document variations
892+
- **🚀 No Configuration Overhead**: Add examples to the dataset without modifying configuration files
893+
- **💡 Better Accuracy**: More relevant examples lead to better extraction results
894+
- **🔍 Multimodal Similarity**: Uses both visual and content features for similarity matching
895+
896+
### Architecture Components
897+
898+
The dynamic few-shot system consists of:
899+
900+
1. **S3 Vectors Index**: Stores document embeddings and metadata
901+
2. **Example Dataset**: Collection of example documents with ground truth annotations
902+
3. **Dynamic Few-Shot Lambda**: Retrieves similar examples based on document embeddings
903+
4. **IDP Integration**: Seamlessly integrates with Pattern-2 extraction process
904+
905+
### Setting Up Dynamic Few-Shot
906+
907+
#### Step 1: Deploy the Dynamic Few-Shot Stack
908+
909+
Deploy the Lambda function and S3 Vectors infrastructure:
910+
911+
```bash
912+
cd notebooks/examples/dynamic-few-shot-lambda
913+
sam deploy --guided
914+
```
915+
916+
This creates:
917+
- Lambda function for dynamic example retrieval
918+
- S3 Vectors bucket and index
919+
- S3 bucket for storing example images
920+
- Required IAM roles and permissions
921+
922+
#### Step 2: Populate the Example Dataset
923+
924+
Use the provided notebook to import example documents into S3 Vectors:
925+
926+
```bash
927+
# Open the dataset import notebook
928+
jupyter notebook notebooks/misc/fewshot_dataset_import.ipynb
929+
```
930+
931+
The notebook demonstrates:
932+
- Loading example documents (e.g., FATURA2 invoice dataset)
933+
- Generating embeddings using Amazon Nova
934+
- Uploading vectors and metadata to S3 Vectors
935+
- Verifying the import with similarity search
936+
937+
**Example Dataset Structure**:
938+
939+
Each example in S3 Vectors includes:
940+
- **Vector**: Multimodal embedding of the document image
941+
- **Metadata**:
942+
- `classLabel`: Document class (e.g., "invoice")
943+
- `classPrompt`: Description for classification
944+
- `attributesPrompt`: Expected attribute extraction results in JSON format
945+
- `imagePath`: S3 path to example document images
946+
947+
```python
948+
# Example metadata structure
949+
metadata = {
950+
"classLabel": "invoice",
951+
"classPrompt": "This is an example of the class 'invoice'",
952+
"attributesPrompt": """expected attributes are:
953+
"invoice_number": "INV-2024-001",
954+
"invoice_date": "January 15, 2024",
955+
"total_amount": "$1,250.00"
956+
""",
957+
"imagePath": "s3://examples-bucket/invoices/example-001/"
958+
}
959+
```
960+
961+
#### Step 3: Configure IDP to Use Dynamic Few-Shot
962+
963+
Add the Lambda ARN to your Pattern-2 extraction configuration:
964+
965+
```yaml
966+
extraction:
967+
dynamic_few_shot_lambda_arn: "arn:aws:lambda:region:account:function:GENAIIDP-dynamic-few-shot"
968+
```
969+
970+
### Lambda Interface
971+
972+
The dynamic few-shot Lambda receives document information and returns similar examples:
973+
974+
**Input**:
975+
```json
976+
{
977+
"class_label": "invoice",
978+
"document_text": "Text content from the document...",
979+
"image_content": ["base64_encoded_image_1", "base64_encoded_image_2"]
980+
}
981+
```
982+
983+
**Output**:
984+
```json
985+
[
986+
{
987+
"attributes_prompt": "expected attributes are: \"invoice_number\": \"INV-001\"...",
988+
"class_prompt": "This is an example of the class 'invoice'",
989+
"distance": 0.122,
990+
"image_content": ["base64_encoded_example_image_1", "base64_encoded_example_image_2"]
991+
}
992+
]
993+
```
994+
995+
### Configuration Parameters
996+
997+
Configure dynamic few-shot behavior through environment variables in the Lambda:
998+
999+
- **`S3VECTOR_BUCKET`**: S3 Vectors bucket name
1000+
- **`S3VECTOR_INDEX`**: Vector index name
1001+
- **`S3VECTOR_DIMENSIONS`**: Embedding dimensions (3072 for Nova Multimodal)
1002+
- **`MODEL_ID`**: Bedrock model for embeddings (e.g., `amazon.nova-2-multimodal-embeddings-v1:0`)
1003+
- **`TOP_K`**: Number of similar examples to retrieve (default: 3)
1004+
- **`THRESHOLD`**: Maximum distance threshold for filtering results (default: 0.2)
1005+
1006+
### Best Practices
1007+
1008+
#### Example Dataset Quality
1009+
1010+
1. **Diverse Coverage**: Include examples covering different document variations
1011+
2. **High-Quality Annotations**: Ensure ground truth attributes are accurate
1012+
3. **Representative Samples**: Choose examples that represent typical documents
1013+
4. **Regular Updates**: Keep the dataset current with new document types
1014+
1015+
#### Performance Optimization
1016+
1017+
1. **Appropriate TOP_K**: Use 2-5 examples for optimal balance between accuracy and cost
1018+
2. **Distance Threshold**: Tune threshold to filter out dissimilar examples
1019+
3. **Batch Processing**: Process similar documents together for better cache utilization
1020+
4. **Monitor Costs**: Track embedding generation and Lambda invocation costs
1021+
1022+
### Monitoring and Troubleshooting
1023+
1024+
#### CloudWatch Metrics
1025+
1026+
Monitor these key metrics:
1027+
- **Lambda Duration**: Time to retrieve and process examples
1028+
- **S3 Vectors Query Time**: Vector similarity search performance
1029+
- **Example Count**: Number of examples returned per request
1030+
- **Error Rate**: Failed example retrievals
1031+
1032+
#### Common Issues
1033+
1034+
**No Examples Retrieved**:
1035+
- Check that the vector index contains examples for the document class
1036+
- Verify distance threshold isn't too strict
1037+
- Ensure embeddings are generated correctly
1038+
1039+
**Poor Example Quality**:
1040+
- Review similarity scores (distances) in Lambda logs
1041+
- Adjust TOP_K to retrieve more/fewer examples
1042+
- Improve example dataset quality and diversity
1043+
1044+
**High Costs**:
1045+
- Reduce TOP_K to retrieve fewer examples
1046+
- Optimize embedding generation frequency
1047+
- Use appropriate Lambda memory allocation
1048+
1049+
### Comparison: Static vs Dynamic Few-Shot
1050+
1051+
| Aspect | Static Few-Shot | Dynamic Few-Shot |
1052+
|--------|----------------|------------------|
1053+
| **Configuration** | Examples in YAML config | Lambda ARN in config |
1054+
| **Example Selection** | Fixed examples per class | Similarity-based selection |
1055+
| **Scalability** | Limited by config size | Scales to large datasets |
1056+
| **Relevance** | Same examples for all docs | Most relevant examples per doc |
1057+
| **Setup Complexity** | Simple YAML configuration | Requires Lambda + S3 Vectors |
1058+
| **Cost** | Token cost for examples | Token + Lambda + embedding costs |
1059+
| **Use Case** | Small, stable example sets | Large, diverse example datasets |
1060+
| **Maintenance** | Manual config updates | Add examples to dataset |
1061+
1062+
### When to Use Dynamic Few-Shot
1063+
1064+
Consider dynamic few-shot prompting when:
1065+
- You have a large collection of example documents (100+)
1066+
- Document variations are significant within each class
1067+
- You want to minimize configuration maintenance
1068+
- Accuracy improvements justify the additional infrastructure
1069+
- You need to continuously add new examples without config changes
1070+
1071+
### Example Workflow
1072+
1073+
1. **Initial Setup**: Deploy Lambda and S3 Vectors infrastructure
1074+
2. **Dataset Import**: Import example documents using the provided notebook
1075+
3. **Configuration**: Add Lambda ARN to IDP extraction config
1076+
4. **Processing**: IDP automatically retrieves relevant examples for each document
1077+
5. **Monitoring**: Track accuracy improvements and costs
1078+
6. **Iteration**: Add more examples to improve coverage
1079+
1080+
### Cost Considerations
1081+
1082+
Dynamic few-shot adds these costs:
1083+
- **Embedding Generation**: Nova multimodal embedding API calls
1084+
- **Lambda Invocations**: Per-document Lambda execution
1085+
- **S3 Vectors**: Storage and query costs
1086+
- **S3 Storage**: Example image storage
1087+
1088+
**Cost Optimization**:
1089+
- Use appropriate TOP_K values (2-3 examples typically sufficient)
1090+
- Implement distance thresholds to avoid irrelevant examples
1091+
- Monitor and optimize Lambda memory allocation
1092+
- Use S3 lifecycle policies for example image storage
1093+
1094+
### Testing Dynamic Few-Shot
1095+
1096+
Test your dynamic few-shot setup using the provided notebook:
1097+
1098+
```bash
1099+
# Open the test notebook
1100+
jupyter notebook notebooks/examples/step3_extraction_with_dynamic_few_shot.ipynb
1101+
```
1102+
1103+
The notebook demonstrates:
1104+
- Document processing with dynamic few-shot enabled
1105+
- Comparison with static few-shot and no examples
1106+
- Accuracy and cost analysis
1107+
- Example retrieval visualization
1108+
8521109
## Future Capabilities
8531110

8541111
The few-shot examples feature continues to evolve with planned enhancements:
8551112

856-
- **Dynamic Example Selection**: Automatically choose the most relevant examples based on document similarity
1113+
- **Hybrid Example Selection**: Combine static and dynamic examples for optimal results
8571114
- **Quality Assessment**: Automated evaluation of example quality and recommendations for improvement
8581115
- **Confidence Integration**: Use few-shot examples to improve confidence scoring
8591116
- **Additional Formats**: Support for more example formats and metadata
8601117
- **Enhanced Caching**: More sophisticated caching strategies for different use cases
861-
- **Hybrid Examples**: Better integration of text and image content within examples
862-
- **Enhanced Multi-Image Support**: Advanced algorithms for optimal image selection from directories
1118+
- **Multi-Modal Fusion**: Better integration of text and image content within examples
1119+
- **Adaptive Thresholds**: Automatically adjust similarity thresholds based on document characteristics
1120+
- **Example Ranking**: Advanced algorithms for optimal example selection and ordering
8631121

864-
Few-shot examples with prompt caching represent a significant step forward in making Pattern-2 more accurate, cost-effective, and easier to configure for diverse document processing scenarios. The flexibility to use images, text, or both, combined with support for multiple images per example, provides comprehensive options for different security, privacy, and performance requirements.
1122+
Few-shot examples with prompt caching and dynamic selection represent a significant step forward in making Pattern-2 more accurate, cost-effective, and easier to configure for diverse document processing scenarios. The flexibility to use static or dynamic examples, images or text, combined with support for multiple images per example, provides comprehensive options for different security, privacy, and performance requirements.

0 commit comments

Comments
 (0)