From f7eaf8814694a21a8b9487aa0bcb57042d3f57c5 Mon Sep 17 00:00:00 2001 From: Raja Vaidyanathan Date: Tue, 28 May 2024 12:24:04 +0100 Subject: [PATCH] Fix template.yml default values for AOSS resources, added requirements.txt to dependencies folder and enabled sam build to build layers, added AOSS Index fields mapping during creation --- s3-lambda-bedrock-aoss/README.md | 4 ++-- .../dependencies/requirements.txt | 3 +++ s3-lambda-bedrock-aoss/src/index_creation.py | 22 ++++++++++++++++++- s3-lambda-bedrock-aoss/src/requirements.txt | 3 +++ s3-lambda-bedrock-aoss/template.yaml | 8 ++++--- 5 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 s3-lambda-bedrock-aoss/dependencies/requirements.txt create mode 100644 s3-lambda-bedrock-aoss/src/requirements.txt diff --git a/s3-lambda-bedrock-aoss/README.md b/s3-lambda-bedrock-aoss/README.md index 9c8259b4b..3d54979d8 100644 --- a/s3-lambda-bedrock-aoss/README.md +++ b/s3-lambda-bedrock-aoss/README.md @@ -25,9 +25,9 @@ Important: this application uses various AWS services and there are costs associ ``` cd s3-lambda-bedrock-aoss ``` -3. From the command line, run `build-layer.sh` that would download required dependencies used to create a Lambda Layer +3. Build the layers and prepare the application for deployment ``` - ./build-layer.sh + sam build ``` 4. Use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: ``` diff --git a/s3-lambda-bedrock-aoss/dependencies/requirements.txt b/s3-lambda-bedrock-aoss/dependencies/requirements.txt new file mode 100644 index 000000000..f6689cf0c --- /dev/null +++ b/s3-lambda-bedrock-aoss/dependencies/requirements.txt @@ -0,0 +1,3 @@ +crhelper +opensearch-py +PyPDF2 \ No newline at end of file diff --git a/s3-lambda-bedrock-aoss/src/index_creation.py b/s3-lambda-bedrock-aoss/src/index_creation.py index 12d991f48..bf8c3cce0 100644 --- a/s3-lambda-bedrock-aoss/src/index_creation.py +++ b/s3-lambda-bedrock-aoss/src/index_creation.py @@ -39,7 +39,27 @@ def get_aoss_index_name(resource_properties): #Function to use the opensearch-py library to create an index within an opensearch collection def create_aoss_index(index_name, aos_client): - aos_client.indices.create(index=index_name) + + index_body = { + "settings": { + "index.knn": True + }, + "mappings": { + "properties": { + "page_vector": { + "type": "knn_vector", + "dimension": 1536 + }, + "page_text": { + "type": "text" + }, + "page_number": { + "type" : "integer" + } + } + } + } + aos_client.indices.create(index=index_name, body=index_body) print(f"Created index {index_name}") #Handles create event of the CloudFormation resource diff --git a/s3-lambda-bedrock-aoss/src/requirements.txt b/s3-lambda-bedrock-aoss/src/requirements.txt new file mode 100644 index 000000000..f6689cf0c --- /dev/null +++ b/s3-lambda-bedrock-aoss/src/requirements.txt @@ -0,0 +1,3 @@ +crhelper +opensearch-py +PyPDF2 \ No newline at end of file diff --git a/s3-lambda-bedrock-aoss/template.yaml b/s3-lambda-bedrock-aoss/template.yaml index 9e5de796e..7cab384a5 100644 --- a/s3-lambda-bedrock-aoss/template.yaml +++ b/s3-lambda-bedrock-aoss/template.yaml @@ -6,12 +6,12 @@ Parameters: Type: String Description: "The collection name to create for storing vector embeedings" ConstraintDescription: "Must be a valid name for Amazon OpenSearch Service Collection." - Default: "example_collection" + Default: "example-collection" AmazonOpenSearchIndexName: Type: String Description: "The index name to store vector embeedings" ConstraintDescription: "Must be a valid Amazon OpenSearch Service Vector Index." - Default: "example_index" + Default: "example-index" BedrockModelRegion: Type: String Description: Select the Bedrock service region from available options. Please make sure the model is access granted in the selected region Bedrock service. If your AOS service is in a region that is different than the bedrock service region, you may expect cross-region latency. @@ -161,6 +161,8 @@ Resources: ContentUri: dependencies/. CompatibleRuntimes: - python3.11 + Metadata: + BuildMethod: python3.11 SourceBucket: Type: AWS::S3::Bucket @@ -222,4 +224,4 @@ Outputs: Description: S3 Bucket for object storage DocumentIngestionLambdaFunctionName: Value: !Ref DocumentIngestionLambdaFunction - Description: Document Ingestion Lambda Function Name \ No newline at end of file + Description: Document Ingestion Lambda Function Name