-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from gowthamshankar99/main
Add support for SONNET 3.5
- Loading branch information
Showing
7 changed files
with
65 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 32 additions & 35 deletions
67
sample_deployments/cdk_lambda/infra/lambda_document_proccessing.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,55 @@ | ||
from aws_cdk import ( | ||
Duration, | ||
RemovalPolicy, | ||
Stack, | ||
aws_lambda, | ||
aws_iam, | ||
aws_s3 | ||
) | ||
from pathlib import Path | ||
|
||
from aws_cdk import Stack, Duration, RemovalPolicy, aws_s3, aws_iam, aws_lambda | ||
from constructs import Construct | ||
from pathlib import Path | ||
|
||
class SampleDeploymentsStack(Stack): | ||
|
||
class SampleDeploymentsStack(Stack): | ||
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: | ||
super().__init__(scope, construct_id, **kwargs) | ||
|
||
bucket = aws_s3.Bucket(self, "MyLambdaBucket", | ||
removal_policy=RemovalPolicy.DESTROY, | ||
auto_delete_objects=True | ||
bucket = aws_s3.Bucket( | ||
self, "MyLambdaBucket", removal_policy=RemovalPolicy.DESTROY, auto_delete_objects=True | ||
) | ||
|
||
|
||
lambda_function = self.__create_lambda_function("document-processing-lambda", bucket) | ||
lambda_function = self.__create_lambda_function("document-processing-lambda", bucket) | ||
srole_bedrock = aws_iam.Role( | ||
scope = self, | ||
id = f'srole-bedrock', | ||
assumed_by = aws_iam.ServicePrincipal('bedrock.amazonaws.com') | ||
scope=self, | ||
id="srole-bedrock", | ||
assumed_by=aws_iam.ServicePrincipal("bedrock.amazonaws.com"), | ||
) | ||
|
||
srole_bedrock.grant_pass_role(lambda_function) | ||
|
||
lambda_function.role.add_managed_policy( | ||
aws_iam.ManagedPolicy.from_aws_managed_policy_name('AmazonBedrockFullAccess') | ||
aws_iam.ManagedPolicy.from_aws_managed_policy_name("AmazonBedrockFullAccess") | ||
) | ||
|
||
bucket.grant_read_write(lambda_function.role) | ||
|
||
def __create_lambda_function(self, function_name: str, bucket: aws_s3.Bucket): | ||
|
||
lambda_role = aws_iam.Role(self, 'LambdaExecutionRole', | ||
assumed_by=aws_iam.ServicePrincipal('lambda.amazonaws.com'), | ||
managed_policies=[aws_iam.ManagedPolicy.from_aws_managed_policy_name('service-role/AWSLambdaBasicExecutionRole')] | ||
lambda_role = aws_iam.Role( | ||
self, | ||
"LambdaExecutionRole", | ||
assumed_by=aws_iam.ServicePrincipal("lambda.amazonaws.com"), | ||
managed_policies=[ | ||
aws_iam.ManagedPolicy.from_aws_managed_policy_name( | ||
"service-role/AWSLambdaBasicExecutionRole" | ||
) | ||
], | ||
) | ||
|
||
lambda_function = aws_lambda.DockerImageFunction( | ||
scope=self, | ||
id=function_name, | ||
function_name=function_name, | ||
code=aws_lambda.DockerImageCode.from_image_asset(directory=f"{Path('source/lambda').absolute()}"), | ||
timeout=Duration.minutes(15), | ||
memory_size=3000, | ||
role=lambda_role, | ||
environment={ | ||
'BUCKET_NAME': bucket.bucket_name | ||
} | ||
scope=self, | ||
id=function_name, | ||
function_name=function_name, | ||
code=aws_lambda.DockerImageCode.from_image_asset( | ||
directory=f"{Path('source/lambda').absolute()}" | ||
), | ||
timeout=Duration.minutes(15), | ||
memory_size=3000, | ||
role=lambda_role, | ||
environment={"BUCKET_NAME": bucket.bucket_name}, | ||
) | ||
|
||
return lambda_function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,33 @@ | ||
from os import getenv | ||
from rhubarb import DocAnalysis | ||
from os import getenv | ||
|
||
import boto3 | ||
|
||
class ProcessDocument(): | ||
from rhubarb import DocAnalysis | ||
|
||
|
||
class ProcessDocument: | ||
def generateJSON(self, document): | ||
try: | ||
session = boto3.Session() | ||
|
||
da = DocAnalysis(file_path=document, | ||
boto3_session=session, | ||
pages=[1]) | ||
prompt="I want to extract the employee name, employee SSN, employee address, \ | ||
da = DocAnalysis(file_path=document, boto3_session=session, pages=[1]) | ||
prompt = "I want to extract the employee name, employee SSN, employee address, \ | ||
date of birth and phone number from this document." | ||
resp = da.generate_schema(message=prompt) | ||
response = da.run(message=prompt, | ||
output_schema=resp['output'] | ||
) | ||
response = da.run(message=prompt, output_schema=resp["output"]) | ||
except ( | ||
# handle bedrock or S3 error | ||
) as e: | ||
|
||
): | ||
return None | ||
|
||
return response | ||
|
||
|
||
|
||
def lambda_handler(event, context): | ||
BUCKET_NAME = getenv('BUCKET_NAME') | ||
BUCKET_NAME = getenv("BUCKET_NAME") | ||
|
||
documentURL = f"s3://{BUCKET_NAME}/employee_enrollment.pdf" | ||
|
||
documentURL = f's3://{BUCKET_NAME}/employee_enrollment.pdf' | ||
|
||
result = ProcessDocument().generateJSON(documentURL) | ||
|
||
return result | ||
return result |
5 changes: 3 additions & 2 deletions
5
sample_deployments/cdk_lambda/tests/unit/test_sample_deployments_stack.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters