diff --git a/s3-lambda-bedrock-js-sam/.gitignore b/s3-lambda-bedrock-js-sam/.gitignore new file mode 100644 index 000000000..969082951 --- /dev/null +++ b/s3-lambda-bedrock-js-sam/.gitignore @@ -0,0 +1,2 @@ +.aws-sam +samconfig.toml diff --git a/s3-lambda-bedrock-js-sam/README.md b/s3-lambda-bedrock-js-sam/README.md new file mode 100644 index 000000000..55e89e08d --- /dev/null +++ b/s3-lambda-bedrock-js-sam/README.md @@ -0,0 +1,72 @@ +# Amazon S3 to AWS Lambda to Amazon Bedrock using AWS SAM + +This pattern facilitates generating vector embeddings for text data uploaded to S3. + +This pattern facilitates LLM RAG use cases by generating vector embeddings of documents in S3. When files are uploaded to S3, Lambda triggers, sending file contents to the Bedrock embedding model, which generates semantic vector embeddings for NLP tasks like semantic search, document clustering, or information retrieval. + +Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. + +## Requirements + +* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured +* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) +* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed + +## Deployment Instructions + +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: + ``` + git clone https://github.com/aws-samples/serverless-patterns + ``` +1. Change directory to the pattern directory: + ``` + cd s3-lambda-bedrock-js-sam + ``` +1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: + ``` + sam deploy --guided + ``` +1. During the prompts: + * Enter a stack name + * Enter the desired AWS Region, just make sure you have access to the Titan Text Embeddings V1 model in the region + * Enter value of the DocumentBucketName parameter which will be the name of created S3 bucket + * Allow SAM CLI to create IAM roles with the required permissions. + + Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults. + +2. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing. + +## How it works + +When a new object is uploaded to an S3 bucket, S3 detects the putObject event and invokes a Lambda function asynchronously, passing details about the uploaded object. The Lambda function then retrieves the object's content and sends it to the Amazon Bedrock Titan Embedding service, which generates vector embeddings representing the content's meaning and context. + +## Testing + +To test the system, we should first tail the logs of the Lambda function. Then, we can upload the sample.txt to our designated S3 bucket. Once the document is uploaded, we can observe the logs to verify the Lambda function's execution and the embeddings of the uploaded document. + +1. Tail to the Lambda function log to get the logs +``` +aws logs tail "/aws/lambda/DocumentEmbedding" --follow --format json +``` +1. Upload the sample file to S3 using below command. Please note the file needs to uploaded to a directory named "content". Refer below for example +``` +aws s3 cp sample.txt s3:///content/ +``` + +### Sample Output: +After uploading the sample document to the S3 bucket and tailing the Lambda function logs, you should see the output in the form of vector embeddings - mathematical representations of the uploaded file's content, capturing its meaning and context. The vector embeddings will be displayed in a format similar to the one shown. +``` +{ "embedding": [ 0.29882812, -0.2421875, 0.24902344, 0.0063476562, 0.24414062, -0.07128906, 0.14355469, 0.00032424927, 0.21777344, -0.29882812, -0.11035156, 0.25, -0.15234375, 0.12597656, 0.33984375, -0.08886719, -0.20898438, -0.26757812, -0.061035156, 0.41015625, 0.14160156, 0.0076293945, -0.19921875, -0.045654297, -0.3984375, 0.09375, 0.10595703, -0.6015625, 0.17382812, -0.0056762695, 0.23339844, 0.47070312, 0.20996094, -0.40234375, 0.40820312, 0.26757812, 0.18066406, 0.44140625, 0.55859375, -0.4609375, -0.78125, -0.30273438, 0.42578125, 0.8203125, -0.26367188, -0.56640625, 0.15234375, 0.16699219, -0.0049743652, 0.45507812, 0.73828125, -0.27148438, 0.43164062, 0.28515625, -0.21875, 0.54296875, 0.296875, 0.111328125, -0.17382812, -0.043945312, -0.14160156, -0.43554688, -0.29882812, -0.77734375, 0.65234375, -0.07324219, 0.71484375, 0.28125, -0.3828125, 0.375, -0.37109375, -0.123535156, -0.3671875, -0.55078125, -0.084472656, -0.21484375, 0.17480469, -0.24804688, 0.20019531, 0.004333496, 0.09863281, 0.29296875, 0.044921875, 0.088378906, 0.125, 0.26367188, -0.083984375, 0.064941406, 0.00013065338, 0.36328125, -0.375, -0.30273438, 0.60546875, -0.080078125, -0.21679688, 0.12988281, -0.8125, -0.13183594, -0.2578125, -0.25390625, -0.03930664, 0.01586914, -0.08203125, 0.5546875, 0.20019531, 0.13964844, -0.0037841797, 0.0066223145, 0.0859375, 0.11328125, 0.0625, 0.12695312, 0.42773438, -0.234375, -0.96875, -0.13867188, 0.020385742, -0.30273438, -0.796875, 0.22363281, 0.28320312, -0.15820312, 0.29296875, 0.100097656, 0.54296875, 0.2734375, -0.24023438, -0.15820312, 0.51171875, 0.083984375, -0.25, 0.24902344, -0.31835938, 0.12988281, 0.046142578, -0.20019531, 0.47070312, 0.390625, 0.26171875, 0.25195312, -0.028686523, -0.95703125, 0.28710938, 0.15234375, -0.16015625, 0.03930664, 0.1328125, -0.53125, -0.16894531, -0.2578125, -0.484375, 0.30078125, 0.23925781, 0.390625, -0.08105469, 0.092285156, 0.35546875, -0.12597656, -0.0044555664, -0.05883789, -0.27539062, -0.31640625, -0.32421875, 0.27734375, 0.16503906, -0.12890625, -0.076660156, -0.14355469, -0.060058594, -0.14941406, 0.047851562, -0.22949219, -0.12451172, 0.40234375, -0.18164062, -0.032470703, 0.005065918, 0.09863281, -0.54296875, -0.40429688, -0.86328125, -0.115234375, -0.011962891, -0.22460938, -0.095214844, 0.0008544922, -0.38476562, -0.640625, 0.1640625, 0.018310547, -0.25585938, 0.0009536743, 0.32226562, -0.484375, 0.12890625, -0.27539062, -0.28710938, -0.21386719, -0.625, 0.18359375, 0.021484375, 0.021606445, 0.83984375, 0.19824219, -0.26171875, 0.13769531, -0.060546875, -0.16503906, 0.546875, -0.28125, -0.18847656, -0.23535156, -0.42382812, 0.03149414, 0.68359375, 0.23535156, 0.31445312, 0.32421875, -0.013427734, 0.064453125, -0.28515625, -0.08300781, -0.118652344, -0.020263672, -0.092285156, -0.05908203, -0.3828125, 0.234375, -0.25, -0.15917969, 0.041992188, -0.004119873, 0.41601562, 0.40234375, -0.016845703, 0.25976562, 0.022705078, 0.54296875, -0.18261719, 0.022827148, 0.036865234, -0.3125, -0.18066406, 0.140625, 0.26953125, -0.484375, 0.26367188, -0.28515625, -0.011352539, 0.17871094, 0.16113281, -0.09863281, 0.36132812, -0.079589844, -0.20214844, -0.06542969, -0.27539062, -0.14746094, -0.19433594, 0.328125, -0.33984375, 0.21777344, 0.099121094, -0.33007812, -0.140625, -0.58984375, 0.0079956055, 0.25390625, 0.35351562, 0.032714844, 0.00013160706, -0.28710938, 0.24609375, 0.037841797, -0.22070312, -0.021118164, 0.34960938, -0.24316406, -0.07861328, -0.23535156, 0.26953125, 0.025634766, -0.14257812, -0.2890625, 0.05419922, -0.8046875, 0.011108398, 0.13769531, 0.30664062, -0.4140625, 0.036132812, 0.14648438, 0.1484375, 0.44921875, 0.095214844, -0.17089844, -0.15332031, -0.13085938, -0.33203125, 0.35351562, -0.010620117, -0.13671875, 0.49023438, -0.29882812, -0.0073547363, -0.1484375, -0.125, 0.09716797, 0.087402344, -0.13183594, -0.13671875, -0.115234375, -0.63671875, 0.15234375, 0.14648438, 0.10595703, -0.07763672, -0.35351562, 0.1171875, 0.071777344, 0.26171875, -0.46289062, 0.028198242, 0.07861328, -0.12597656, 0.19433594, -0.49609375, -0.103027344, 0.20117188, -0.63671875, 0.17285156, -0.41796875, 0.11767578, 0.64453125, -0.2265625, -0.27734375, 0.14941406, 0.43164062, 0.515625, -0.1875, -0.37304688, -0.04296875, 0.3671875, 0.09033203, -0.20117188, -0.06689453, -0.15234375, -0.13867188, 0.53515625, 0.092285156, 0.5, -0.048095703, -0.46875, 0.04663086, -0.072753906, -0.3515625, -0.44335938, -0.81640625, 0.23535156, 0.296875, 0.018554688, -0.091796875, 0.15625, -0.56640625, 0.546875, 0.16601562, 0.017822266, -0.203125, 0.421875, 0.5859375, -0.095214844, 0.83984375, 0.088378906, 0.36328125, -0.13183594, -0.75390625, 0.06933594, -0.47851562, -0.421875, 0.006866455, 0.08496094, 0.15039062, 0.26367188, 0.17578125, 0.25585938, 0.20800781, 0.08984375, -0.09082031, -0.22363281, -0.19824219, 0.15820312, 0.38671875, -0.114746094, 0.08251953, -0.26953125, 0.28515625, 0.16894531, -0.45117188, 0.030029297, -0.2421875, 0.13378906, 0.11230469, 0.39257812, -0.00079345703, 0.095703125, -0.6171875, 0.0032958984, 0.21679688, -0.63671875, 0.32421875, -0.13769531, -0.22851562, -0.064453125, 0.29101562, 0.06542969, 0.17675781, 0.31640625, 0.044921875, -0.24609375, 0.1171875, 0.16015625, -0.49023438, 0.068847656, 0.07421875, 0.4375, 0.17578125, -0.46679688, -0.09814453, -0.125, -0.18847656, -0.10058594, -0.028930664, 0.021362305, -0.20800781, -0.18457031, -0.6640625, 0.064941406, 0.29101562, 0.10888672, 0.71875, 0.15429688, -0.0071411133, 0.027832031, -0.68359375, 0.006713867, -0.39257812, -0.13085938, -0.25195312, -0.4296875, -0.31054688, -0.003829956, 0.28710938, 0.36914062, 0.5, -0.42773438, -0.546875, -0.15820312, -0.11621094, 0.34179688, -0.038085938, -0.012145996, 0.24511719, 0.0030517578, -0.03564453, -0.3046875, -0.072265625, -0.48632812, -0.44140625, 0.36132812, -0.45117188, -0.328125, 0.88671875, 0.00091171265, -0.035888672, -0.1171875, -0.5, 0.23046875, 0.51171875, -0.25585938, 0.41210938, 0.111328125, -0.09765625, 0.12792969, 0.013671875, 0.671875, 0.5546875, 0.11767578, -0.11328125, 0.013122559, 0.15429688, 0.4765625, 0.22558594, -0.11816406, 0.5390625, 0.028686523, 0.21777344, -0.0002708435, -0.4375, 0.33984375, -0.14257812, 0.22753906, 0.21777344, 0.016723633, 0.026977539, 0.765625, 0.29492188, 0.21777344, -0.29492188, -0.27539062, 0.106933594, 0.671875, 0.008117676, -0.43945312, 0.2734375, -0.49023438, 0.040527344, -0.2890625, 0.14550781, -0.027832031, -0.08300781, 0.017333984, -0.29882812, -0.20214844, 0.73046875, -0.13769531, 0.08691406, 0.00059890747, 0.45507812, 0.23535156, -0.4296875, -0.28125, -0.21289062, -0.16015625, -0.04736328, 0.22460938, -0.26171875, -0.32226562, 0.034179688, -0.5625, -0.20605469, -0.13476562, -0.42773438, -0.46679688, 0.025512695, 0.11376953, -0.31835938, -0.025268555, -0.14746094, -0.13183594, 0.70703125, -0.017089844, -0.28515625, -0.061279297, -0.19824219, 0.001373291, 0.5625, 0.15820312, 0.89453125, -0.13964844, 0.89453125, -0.18652344, 0.028808594, -0.029418945, 0.10107422, -0.036621094, -0.12451172, -0.5703125, -0.27148438, -0.010009766, 0.13476562, -0.68359375, -0.578125, 0.04345703, 0.059326172, 0.5078125, 0.13476562, 0.01977539, -0.09082031, -0.06738281, 0.24902344, -0.04345703, -0.19140625, 0.17382812, 0.12695312, -0.72265625, 0.22949219, 0.24902344, -0.25390625, -0.024047852, 0.07421875, -0.73828125, 0.057617188, 0.018798828, -0.43945312, -0.17285156, 0.051513672, -0.103515625, 0.49023438, 0.2421875, 0.15527344, -0.16113281, 0.30078125, 0.07373047, -0.59375, -0.18066406, -0.37304688, -0.265625, 0.35351562, 0.09667969, -0.46679688, 0.12402344, -0.07910156, 0.051757812, -0.08935547, 0.013427734, -0.3671875, 0.47460938, -0.16210938, -0.20507812, 0.044433594, -0.10498047, -0.57421875, -0.12988281, -0.040283203, 0.390625, 0.09375, 0.21972656, -0.03540039, 0.546875, 0.34375, 0.40820312, 0.515625, 0.3203125, 0.15039062, 0.21191406, 1.1953125, 0.38867188, 0.015136719, 0.17089844, -0.68359375, -0.6015625, -0.09765625, -0.27539062, 0.51953125, -0.09277344, -0.60546875, -0.36523438, 0.0002937317, -0.12402344, 0.091308594, -0.16308594, -0.18066406, 0.18359375, -0.068359375, 0.118652344, 0.22753906, -0.17480469, -0.056152344, -0.63671875, 0.080566406, -0.38867188, 0.296875, 0.23046875, 0.032714844, 0.31835938, 0.27929688, 0.006164551, 0.32617188, -0.26171875, -0.013244629, 0.1328125, 0.12890625, -0.3515625, 0.095214844, -0.6875, -0.37109375, 0.15234375, -0.32617188, -0.578125, -0.0050354004, 1.0390625, 0.19335938, 0.42578125, 0.22265625, -0.49804688, -0.40429688, -0.10595703, -0.4609375, -0.265625, 0.28320312, -0.15039062, -0.37109375, 0.1015625, 0.15332031, -0.32226562, 0.39453125, 0.15039062, 0.3671875, 0.57421875, 0.103515625, 0.019165039, 0.1953125, 0.084472656, -0.17382812, -0.15527344, -0.11279297, -0.09277344, -0.15625, -0.013305664, 0.30273438, -0.27148438, 0.37304688, -0.23925781, -0.52734375, 0.11669922, 0.08935547, -0.515625, 0.072753906, -0.103027344, 0.50390625, -0.018798828, 0.3046875, -0.060058594, 0.43554688, 0.16308594, -0.19335938, -0.35546875, -0.40429688, 0.51171875, 0.18359375, -0.119140625, 0.38671875, -0.17578125, -0.20996094, -0.32226562, -0.0028533936, -0.34375, 0.29882812, 0.26953125, 0.25585938, 0.18359375, 0.09472656, 0.390625, 0.24902344, 0.25976562, 0.20605469, 0.14746094, 0.26171875, 0.05493164, -0.16113281, 0.034423828, -0.0047912598, -0.18066406, -0.48632812, -0.16210938, 0.072265625, -0.17382812, -0.47265625, -0.008911133, 0.43359375, -0.17578125, 0.40820312, -0.11376953, 0.021362305, 0.07763672, -0.25, -0.19921875, -0.21777344, 0.17480469, 0.25195312, 0.328125, -0.22851562, -0.16894531, -0.19335938, -0.234375, 0.16992188, 0.671875, -0.09716797, 0.09423828, -0.16796875, 0.060058594, 0.018310547, 0.125, -0.16699219, 0.28710938, 0.16699219, -0.26367188, -0.35742188, 0.53125, -0.076171875, -0.58984375, 0.044677734, -0.09375, 0.37695312, 0.484375, 0.09033203, -0.18359375, 0.46875, -0.079589844, -0.114746094, -0.20800781, -0.007171631, 0.31835938, 0.375, 0.24121094, -0.23046875, 0.048339844, -0.08203125, 0.51171875, 0.28710938, -0.24902344, -0.31835938, -0.53125, 0.13574219, -0.5078125, -0.33007812, 0.36914062, 0.0023498535, -0.37890625, -0.07128906, 0.0037231445, 0.6171875, -0.01977539, -0.36914062, 0.09814453, 0.0013885498, 0.041503906, 0.08886719, -0.095703125, 0.50390625, 0.28515625, -0.033935547, 0.22070312, 0.26367188, -0.15332031, -0.26367188, 0.18164062, -0.6640625, 0.2734375, 0.30859375, 0.26757812, 0.625, 0.33007812, -0.14355469, 0.19921875, 0.34375, 0.45898438, 0.21777344, -0.61328125, -0.19726562, -0.55078125, -0.16894531, 0.057128906, 0.09375, -0.22363281, -0.515625, -0.1875, -0.08642578, 0.31640625, 0.31640625, 0.31054688, 0.375, 0.12792969, -0.111816406, 0.09082031, 0.62890625, 0.92578125, 0.045654297, -0.028076172, 0.14550781, -0.072753906, 0.27734375, -0.080078125, 0.052001953, -0.24023438, -0.38085938, -0.07861328, 0.51171875, 0.24707031, 0.24804688, 0.30664062, -0.38867188, -0.15917969, 0.20019531, 0.4140625, 0.13574219, -0.06542969, -0.024169922, 0.08154297, 0.27734375, 0.69921875, 0.41601562, 0.10839844, 0.064941406, 0.03466797, -0.020263672, -0.041259766, -0.3984375, -0.6875, 0.30273438, 0.12792969, -0.21386719, -0.020019531, -0.34375, 0.546875, -0.12988281, 0.034179688, -0.0072021484, 0.004119873, -0.1953125, -0.26171875, -0.13378906, 0.40234375, -0.11767578, -0.087402344, 0.22753906, 0.40820312, 0.13183594, 0.080566406, -0.57421875, -0.057128906, 0.005706787, 0.04248047, -0.66796875, 0.22070312, -0.1484375, 0.21289062, 0.109375, 0.099609375, 0.036621094, -0.921875, 0.54296875, -0.6171875, -0.09082031, -0.013183594, -0.18847656, 0.05859375, 0.4453125, 0.66015625, 0.88671875, 0.016235352, 0.00013542175, -0.07861328, 0.03540039, 0.23242188, -0.17089844, 0.26953125, -0.07910156, -0.04638672, 0.27734375, -0.3828125, -0.20214844, 0.07373047, 0.0008354187, -0.26953125, 0.3828125, -0.022827148, 0.41015625, 0.140625, -0.43945312, -0.09765625, -0.17089844, 0.43945312, 0.546875, -0.45898438, -0.24609375, 0.47851562, -0.16308594, -0.42578125, -0.62109375, -0.34570312, -0.35546875, 0.53515625, 0.62109375, -0.071777344, -0.12451172, 0.53125, 0.8671875, -0.5390625, -0.19140625, 0.06640625, 0.072265625, -0.18554688, -0.20410156, 0.020874023, 0.48046875, -0.37304688, -0.2421875, 0.053710938, -0.5859375, -0.62890625, 0.11376953, -0.17871094, 0.48242188, -0.5546875, 0.16699219, 0.111328125, 0.359375, -0.063964844, -0.2421875, 0.033203125, -0.32226562, 0.045166016, -0.41601562, -0.0056152344, 0.23535156, -0.045898438, -0.22460938, 0.41796875, 0.27148438, -0.29296875, 0.20507812, -0.072753906, 0.234375, 0.07080078, -0.0037384033, -0.19238281, 0.33203125, -0.203125, -0.0126953125, 0.46289062, -0.055419922, -0.30859375, -0.12792969, -0.12158203, 0.12060547, 0.009399414, 0.011474609, -0.33398438, 0.59375, -0.03125, 0.061523438, -0.017333984, 0.5546875, 0.2734375, -0.3203125, -0.21191406, 0.24707031, -0.3125, 0.34570312, -0.24707031, -0.2890625, -0.2578125, 0.032958984, -0.21972656, 0.09375, 0.078125, -0.026977539, 0.0007095337, 0.12695312, -0.020874023, -0.091308594, -0.24121094, 0.14550781, 0.20605469, -0.33789062, -0.49414062, 0.08300781, 0.25976562, -0.13574219, -0.08251953, -0.1484375, 0.24804688, 0.09082031, 0.24316406, 0.24902344, -0.2890625, 0.08642578, -0.3671875, 0.08203125, -0.33398438, -0.051513672, 0.21972656, -0.17089844, -0.12988281, -0.21386719, -0.24707031, 0.38085938, -0.03564453, 0.6953125, -0.009216309, 0.23925781, -0.390625, -0.48828125, -0.072265625, 0.052246094, -0.40234375, -0.25390625, -0.014282227, 0.36132812, -0.006011963, 0.22167969, 0.84765625, 0.0008010864, 0.15625, 0.484375, 0.005004883, 0.19824219, -0.65234375, 0.0033569336, -0.015136719, 0.050048828, -0.22167969, -0.33203125, 0.24609375, 0.22265625, -0.65234375, -0.047851562, -0.040283203, 0.20703125, -0.04296875, -0.21289062, -0.10839844, 0.12988281, -0.38671875, 0.5546875, -0.41601562, -0.0703125, -0.859375, -0.0859375, 0.57421875, 0.34960938, -0.045410156, -0.2890625, -0.47851562, -0.44140625, 0.14160156, -0.03515625, -0.36914062, 0.40820312, -0.33984375, 0.11621094, 0.15234375, 0.796875, 0.115234375, -0.46679688, -0.05078125, -0.2265625, -0.6171875, -0.9375, 0.20605469, 0.15234375, -0.42773438, -0.515625, 0.75390625, 0.14550781, -0.04638672, 0.022094727, 0.053466797, 0.10058594, -0.1015625, -0.29296875, 0.46484375, -0.40820312, 0.6796875, 0.45117188, 0.49804688, -0.640625, 0.27148438, -0.095214844, -0.04736328, -0.033935547, 0.088378906, 0.16894531, 0.07763672, 0.21875, -0.17578125, -0.004272461, 0.13085938, -0.014221191, -0.34960938, -0.41601562, -0.08251953, -0.265625, 0.22070312, -0.06591797, -0.07080078, 0.26757812, -0.095703125, -0.515625, 0.43359375, 0.1015625, 0.42773438, -0.328125, 0.04736328, -0.140625, 0.049072266, 0.009643555, 0.15429688, 0.15625, -0.123046875, -0.1640625, 0.29101562, -0.119140625, -0.03955078, -0.24316406, 0.30664062, -0.41601562, 0.1875, -0.087890625, -0.3671875, 0.30273438, -0.0024719238, -0.012145996, 0.22949219, 0.13085938, 0.30664062, -0.47851562, 0.40625, -0.70703125, 0.24121094, 0.14550781, -0.01977539, -0.2890625, 0.2734375, -0.099121094, -0.11669922, 0.075683594, -0.42773438, 0.052246094, -0.60546875, -0.09765625, 0.27148438, -0.3671875, 0.18261719, 0.04248047, 0.046142578, 0.091796875, -0.02722168, 0.20117188, -0.26953125, 0.19335938, 0.20507812, 0.40625, 0.19921875, 0.15722656, -0.26171875, 0.091308594, -0.1875, -0.26757812, -0.5078125, -0.104003906, 0.60546875, 0.56640625, 0.28320312, 0.16699219, -0.34570312, -0.045654297, -0.23046875, 0.038330078, 0.18457031, -0.061279297, 0.16796875, 0.17382812, -0.58984375, 0.16601562, -0.028930664, -0.15332031, -0.018920898, -0.057128906, 0.890625, 0.07128906, 0.14355469, 0.56640625, 0.25585938, -0.68359375, 0.24707031, -0.30273438, 0.3203125, -0.14746094, -0.578125, -0.016601562, 0.21972656, -0.25976562, 0.2578125, 0.38867188, -0.25, -0.33398438, -0.10058594, 0.016723633, 0.41015625, -0.24804688, -0.5546875, 0.7265625, -0.3046875, 0.100097656, -0.6796875, 0.16503906, -0.104003906, -0.71875, -0.4453125, -0.29882812, -0.21679688, -0.006286621, -0.4375, 0.026367188, 0.061767578, -0.010986328, 0.1796875, -0.010620117, 0.37109375, 0.46875, -0.765625, 0.16503906, -0.020141602, 0.044677734, 0.66015625, -0.765625, 0.45507812, -0.17480469, 0.2421875, -0.140625, 0.24511719, 0.33398438, -0.091308594, 0.119140625, 0.13671875, -0.09277344, 0.171875, -0.109375, 0.115234375, -0.13183594, 0.35742188, -0.10449219, 0.051513672, -0.46875, 0.140625, 0.13867188, -0.32226562, 0.47851562, -0.10253906, 0.22949219, -0.19824219, 0.11816406, 0.049560547, 0.77734375, -0.62109375, -0.08544922, 0.11035156, -0.0016174316, -0.049316406, 0.06542969, 0.609375, -0.15332031, 0.13476562, -0.203125, -0.51171875, 0.92578125, -0.2265625, -0.14550781, 0.028686523, -0.36523438, 0.01977539, 0.25390625, -0.22363281, 0.38671875, -0.16210938, -0.17089844, 0.110839844, -0.036621094, 0.104003906, 0.15429688, 0.34765625, 0.08203125, -0.1015625, 0.12451172, 0.044433594, 0.06542969, 0.008483887, 0.080078125, -0.3828125, 0.13867188, -0.62109375, 0.03564453, -0.59375, 0.6796875, 0.0107421875, -0.41601562, -0.095703125, -0.09082031, -0.29296875, 0.075683594, -0.118652344, 0.2578125, 0.33789062, 0.3203125, 0.29101562, -0.765625, -0.13476562, -0.09375, -0.07470703, 0.19628906, 0.011108398, -0.18359375, -0.064453125, 0.3359375, -0.22070312, -0.07910156, 0.071777344, 0.046875, 0.23339844, 0.3125, -0.19824219, -0.29296875, -0.37695312, -0.54296875, -0.24707031, 0.375, -0.7265625, -0.16113281, -0.70703125, -0.18164062, 0.18652344, 0.08691406, -0.234375, -0.40039062, 0.203125, 0.23632812, 0.51171875, -0.11816406, -0.12988281, 0.28710938, -0.12597656, 0.54296875, -0.19628906, -0.23925781, 0.11425781, -0.05859375, 0.49414062, -0.30664062, 0.14160156, -0.48828125, 0.14550781, 0.58203125, 0.0000029802322, 0.007385254, 0.6796875, -0.12109375, -0.12109375, 0.265625, -0.0390625, 0.609375, -0.34570312, 0.5859375, 0.011169434, -0.19433594, 0.453125, 0.33398438, -0.546875, 0.13183594, -0.13769531, -0.1484375, -0.2734375, 0.06542969, 0.625, -0.0016326904, -0.41210938, 0.057617188, 0.16992188, -0.10595703, -0.0126953125, 0.23144531, 0.6171875, -0.21289062, -0.08251953, -0.06225586, -0.15429688, -0.48046875, 0.0546875, -0.10644531, 0.15332031, -0.546875, -0.5, 0.17089844, 0.14160156, 0.20214844, 0.7890625, 0.26757812, 0.41796875, 0.34765625, -0.375, -0.19335938, -0.328125, 0.041748047, 0.12988281, 0.006713867, -0.59375, -0.30859375, -0.46679688, 0.26367188, 0.035888672, -0.026123047, 0.30273438, -0.22851562, 0.2578125, 0.22460938, 0.5, -0.19921875, -0.14355469, -0.22558594, 0.12988281, 0.064941406, -0.03857422, 0.36132812, -0.20019531, 0.66796875, -0.39257812, 0.09033203, -0.06982422, -0.5234375, 0.028320312, -0.027954102, 0.20703125, -0.63671875, -0.34960938, -0.47851562, -0.15234375, -0.35546875, -0.015991211, -0.0063476562, -0.20214844, -0.08105469, -0.26953125, -0.34765625, -0.23242188, 0.17871094, -0.122558594, 0.072753906, 0.53125, -0.02319336, -0.41015625, -0.20605469, -0.484375, 0.16894531, -0.13085938, -0.20410156 ], "inputTextTokenCount": 82 } +``` + +## Cleanup + +1. Delete the stack +``` +sam delete +``` +---- +Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. + +SPDX-License-Identifier: MIT-0 diff --git a/s3-lambda-bedrock-js-sam/example-pattern.json b/s3-lambda-bedrock-js-sam/example-pattern.json new file mode 100644 index 000000000..05e412133 --- /dev/null +++ b/s3-lambda-bedrock-js-sam/example-pattern.json @@ -0,0 +1,69 @@ +{ + "title": "Amazon S3 to AWS Lambda to Amazon Bedrock using AWS SAM", + "description": "This pattern facilitates generating vector embeddings for text data uploaded to S3.", + "language": "Node.js", + "level": "200", + "framework": "SAM", + "introBox": { + "headline": "How it works", + "text": [ + "When a new object is uploaded to an S3 bucket, S3 detects the putObject event and invokes a Lambda function asynchronously, passing details about the uploaded object. The Lambda function then retrieves the object's content and sends it to the Amazon Bedrock Titan Embedding service, which generates vector embeddings representing the content's meaning and context." + ] + }, + "gitHub": { + "template": { + "repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/s3-lambda-bedrock-js-sam", + "templateURL": "serverless-patterns/s3-lambda-bedrock-js-sam", + "projectFolder": "s3-lambda-bedrock-js-sam", + "templateFile": "template.yaml" + } + }, + "resources": { + "bullets": [ + { + "text": "Amazon S3", + "link": "https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html" + }, + { + "text": "AWS Lambda", + "link": "https://docs.aws.amazon.com/lambda/latest/dg/welcome.html" + }, + { + "text": "Amazon Bedrock", + "link": "https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html" + } + ] + }, + "deploy": { + "text": [ + "sam build", + "sam deploy --guided" + ] + }, + "testing": { + "text": [ + "See the GitHub repo for detailed testing instructions." + ] + }, + "cleanup": { + "text": [ + "sam delete" + ] + }, + "authors": [ + { + "name": "Achintya Veer Singh", + "image": "https://avatars.githubusercontent.com/u/55053737?v=4", + "bio": "Solutions Architect @ AWS", + "linkedin": "www.linkedin.com/in/achintya-veer-singh-493403193", + "twitter": "achintya_veer" + }, + { + "name": "Rohit Raj", + "image": "https://avatars.githubusercontent.com/u/69171887?v=4", + "bio": "Solutions Architect @ AWS", + "linkedin": "https://www.linkedin.com/in/rohitraj-11", + "twitter": "RohitRaj_11" + } + ] + } \ No newline at end of file diff --git a/s3-lambda-bedrock-js-sam/s3-lambda-bedrock-js-sam.json b/s3-lambda-bedrock-js-sam/s3-lambda-bedrock-js-sam.json new file mode 100644 index 000000000..725810d1c --- /dev/null +++ b/s3-lambda-bedrock-js-sam/s3-lambda-bedrock-js-sam.json @@ -0,0 +1,97 @@ +{ + "title": "Amazon S3 to AWS Lambda to Amazon Bedrock using AWS SAM", + "description": "This pattern facilitates generating vector embeddings for text data uploaded to S3.", + "language": "Node.js", + "level": "200", + "framework": "SAM", + "introBox": { + "headline": "How it works", + "text": [ + "When a new object is uploaded to an S3 bucket, S3 detects the putObject event and invokes a Lambda function asynchronously, passing details about the uploaded object. The Lambda function then retrieves the object's content and sends it to the Amazon Bedrock Titan Embedding service, which generates vector embeddings representing the content's meaning and context." + ] + }, + "gitHub": { + "template": { + "repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/s3-lambda-bedrock-js-sam", + "templateURL": "serverless-patterns/s3-lambda-bedrock-js-sam", + "projectFolder": "s3-lambda-bedrock-js-sam", + "templateFile": "template.yaml" + } + }, + "resources": { + "bullets": [ + { + "text": "Amazon S3", + "link": "https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html" + }, + { + "text": "AWS Lambda", + "link": "https://docs.aws.amazon.com/lambda/latest/dg/welcome.html" + }, + { + "text": "Amazon Bedrock", + "link": "https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html" + } + ] + }, + "deploy": { + "text": [ + "sam build", + "sam deploy --guided" + ] + }, + "testing": { + "text": [ + "See the GitHub repo for detailed testing instructions." + ] + }, + "cleanup": { + "text": [ + "sam delete" + ] + }, + "authors": [ + { + "name": "Achintya Veer Singh", + "image": "https://avatars.githubusercontent.com/u/55053737?v=4", + "bio": "Solutions Architect @ AWS", + "linkedin": "achintya-veer-singh-493403193", + "twitter": "achintya_veer" + }, + { + "name": "Rohit Raj", + "image": "https://avatars.githubusercontent.com/u/69171887?v=4", + "bio": "Solutions Architect @ AWS", + "linkedin": "rohitraj-11", + "twitter": "RohitRaj_11" + } + ], + "patternArch": { + "icon1": { + "x": 20, + "y": 50, + "service": "s3", + "label": "Amazon S3" + }, + "icon2": { + "x": 50, + "y": 50, + "service": "lambda", + "label": "AWS Lambda" + }, + "icon3": { + "x": 80, + "y": 50, + "service": "bedrock", + "label": "Amazon Bedrock" + }, + "line1": { + "from": "icon1", + "to": "icon2" + }, + "line2": { + "from": "icon2", + "to": "icon3" + } + } +} diff --git a/s3-lambda-bedrock-js-sam/sample.txt b/s3-lambda-bedrock-js-sam/sample.txt new file mode 100644 index 000000000..1a0f62bbf --- /dev/null +++ b/s3-lambda-bedrock-js-sam/sample.txt @@ -0,0 +1,3 @@ +Peace is a state of tranquility, calm, and harmony, free from violence, conflict, or hostility. +It is a fundamental human aspiration, as it allows for the flourishing of individuals, communities, and nations. +Achieving lasting peace requires a concerted effort to address the root causes of violence, foster understanding and respect among diverse groups, and promote justice, equality, and sustainable development. \ No newline at end of file diff --git a/s3-lambda-bedrock-js-sam/src/index.mjs b/s3-lambda-bedrock-js-sam/src/index.mjs new file mode 100644 index 000000000..cebae006b --- /dev/null +++ b/s3-lambda-bedrock-js-sam/src/index.mjs @@ -0,0 +1,53 @@ +import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3"; +import { + BedrockRuntimeClient, + InvokeModelCommand, +} from "@aws-sdk/client-bedrock-runtime"; + +const awsS3Client = new S3Client(); +const awsBedrockClient = new BedrockRuntimeClient(); + +const fetchFileFromS3 = async (bucketName, fileKey) => { + const command = new GetObjectCommand({ + Bucket: bucketName, + Key: fileKey, + }); + const response = await awsS3Client.send(command); + const fileContent = response.Body + ? await response.Body.transformToString() + : ""; + return fileContent; +}; + +export const handler = async (event) => { + if (!event || !event.Records) { + return { + statusCode: 200, + body: JSON.stringify({ message: "No S3 event detected" }), + }; + } + + const eventRecord = event.Records[0]; + const bucketName = eventRecord.s3.bucket.name; + const fileName = decodeURIComponent(eventRecord.s3.object.key); + const fileContent = await fetchFileFromS3(bucketName, fileName); + + const bedrockModelInput = { + modelId: "amazon.titan-embed-text-v1", + contentType: "application/json", + accept: "*/*", + body: JSON.stringify({ inputText: JSON.stringify(fileContent) }), + }; + console.log("Input provided to Bedrock", bedrockModelInput); + + const bedrockCommand = new InvokeModelCommand(bedrockModelInput); + const bedrockResponse = await awsBedrockClient.send(bedrockCommand); + + const responseData = Buffer.from(bedrockResponse.body).toString(); + console.log("Response data: ", responseData); + + return { + statusCode: 200, + body: JSON.stringify(responseData), + }; +}; diff --git a/s3-lambda-bedrock-js-sam/template.yaml b/s3-lambda-bedrock-js-sam/template.yaml new file mode 100644 index 000000000..13918d821 --- /dev/null +++ b/s3-lambda-bedrock-js-sam/template.yaml @@ -0,0 +1,79 @@ +AWSTemplateFormatVersion: "2010-09-09" +Transform: AWS::Serverless-2016-10-31 + +Description: This pattern facilitates intelligent document processing by generating vector embeddings for text data uploaded to S3. + +Parameters: + DocumentBucketName: + Type: String + +Resources: + DocumentBucket: + Type: AWS::S3::Bucket + Properties: + BucketName: !Ref DocumentBucketName + + DocumentEmbeddingFunction: + Type: AWS::Serverless::Function + Properties: + CodeUri: src/ + Handler: index.handler + FunctionName: DocumentEmbedding + Description: Lambda function to generate embeddings for uploaded documents + Runtime: nodejs18.x + Timeout: 10 + MemorySize: 128 + Role: !GetAtt DocumentEmbeddingFunctionExecutionRole.Arn + Events: + FileUpload: + Type: S3 + Properties: + Bucket: !Ref DocumentBucket + Events: s3:ObjectCreated:* + Filter: + S3Key: + Rules: + - Name: prefix + Value: "content/" + + DocumentEmbeddingFunctionExecutionRole: + Type: AWS::IAM::Role + Properties: + AssumeRolePolicyDocument: + Version: "2012-10-17" + Statement: + - Effect: Allow + Principal: + Service: + - lambda.amazonaws.com + Action: + - "sts:AssumeRole" + ManagedPolicyArns: + - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole + Policies: + - PolicyName: CustomPolicy + PolicyDocument: + Version: "2012-10-17" + Statement: + - Effect: Allow + Action: + - s3:GetObject + - s3:ListBucket + Resource: + - !Sub "arn:aws:s3:::${DocumentBucketName}/*" + - !Sub "arn:aws:s3:::${DocumentBucketName}" + - Effect: Allow + Action: + - bedrock:InvokeModel + Resource: "*" + +Outputs: + DocumentBucket: + Description: S3 bucket for storing documents + Value: !Ref DocumentBucket + DocumentEmbeddingFunction: + Value: !Ref DocumentEmbeddingFunction + Description: Lambda Function name for generating document embeddings + DocumentEmbeddingFunctionARN: + Value: !GetAtt DocumentEmbeddingFunction.Arn + Description: ARN of Lambda Function for generating document embeddings