From 147f085dae4abd181f4871eb76db293050a036cd Mon Sep 17 00:00:00 2001 From: Joel Balcaen Date: Fri, 3 May 2024 10:04:44 -0300 Subject: [PATCH] get rid of invoker 2 --- lambdas/bedrock_invoker/src/index.py | 60 ++++++++----- lambdas/bedrock_invoker_2/lambda.tf | 74 --------------- lambdas/bedrock_invoker_2/output.tf | 7 -- lambdas/bedrock_invoker_2/src/index.py | 89 ------------------- lambdas/bedrock_invoker_2/variables.tf | 15 ---- .../rfp_email_form_fill/state_machine.json | 4 +- terraform/modules.tf | 7 -- 7 files changed, 40 insertions(+), 216 deletions(-) delete mode 100644 lambdas/bedrock_invoker_2/lambda.tf delete mode 100644 lambdas/bedrock_invoker_2/output.tf delete mode 100644 lambdas/bedrock_invoker_2/src/index.py delete mode 100644 lambdas/bedrock_invoker_2/variables.tf diff --git a/lambdas/bedrock_invoker/src/index.py b/lambdas/bedrock_invoker/src/index.py index 10ef374..0fc177f 100644 --- a/lambdas/bedrock_invoker/src/index.py +++ b/lambdas/bedrock_invoker/src/index.py @@ -2,33 +2,56 @@ import json from botocore.exceptions import BotoCoreError, ClientError from botocore.config import Config +import os +import base64 s3 = boto3.client('s3') bedrock = boto3.client('bedrock-runtime', config=Config(read_timeout=1000)) + def lambda_handler(event, context): """ Invokes a bedrock model with the given parameters and s3 text object """ - s3_arn = event['s3_arn'] system_prompt = event['system_prompt'] prompt = event['prompt'] - s3_path = s3_arn.replace("arn:aws:s3:::", "") - bucket, key = s3_path.split('/', 1) + s3_uris = event['s3_uris'] - print(f"Fetching file bucket: {bucket}, key: {key}") - try: - s3_object = s3.get_object(Bucket=bucket, Key=key) - except ClientError as e: - return { - 'statusCode': 400, - 'body': str(e) - } - extracted_text = s3_object['Body'].read().decode('utf-8') - print(f"Extracted text that is {len(extracted_text)} characters long") - print(f"Preview of the first 100 chars: {extracted_text[:100]}") + user_prompt_content = [] + + for s3_uri in s3_uris: + bucket, key = s3_uri.replace("s3://", "").split('/', 1) + extension = os.path.splitext(key)[1].lower() + try: + print(f"Fetching file bucket: {bucket}, key: {key}") + s3_object = s3.get_object(Bucket=bucket, Key=key) + + if extension in ['.txt', '.csv', '.json']: + user_prompt_content.append({ + 'type': 'text', + 'text': s3_object['Body'].read().decode('utf-8') + }) + elif extension in ['.jpg', '.png', '.jpeg']: + image_data = s3_object['Body'].read() + encoded_image_data = base64.b64encode(image_data).decode() + media_type = f"image/{'png' if extension == '.png' else 'jpeg'}" + user_prompt_content.append({ + 'type': 'image', + 'source': {'type': 'base64', 'data': encoded_image_data, 'media_type': media_type} + }) + + except ClientError as e: + return { + 'statusCode': 400, + 'body': str(e) + } + + user_prompt_content.append({ + 'type': 'text', + 'text': f"{prompt}" + }) claude_body = { "anthropic_version": "bedrock-2023-05-31", @@ -37,12 +60,7 @@ def lambda_handler(event, context): "messages": [ { "role": "user", - "content": [ - { - 'type': "text", - "text": f"{prompt} {extracted_text}" - } - ] + "content": user_prompt_content } ] } @@ -52,7 +70,6 @@ def lambda_handler(event, context): print(f"Invoke claude with system prompt: ", system_prompt) print(f"Invoke claude with prompt: ", prompt) - try: response = bedrock.invoke_model( body=json.dumps(claude_body), @@ -60,7 +77,6 @@ def lambda_handler(event, context): accept='application/json', modelId=bedrock_model, ) - return { 'statusCode': 200, 'body': response['body'].read().decode('utf-8') diff --git a/lambdas/bedrock_invoker_2/lambda.tf b/lambdas/bedrock_invoker_2/lambda.tf deleted file mode 100644 index 172790c..0000000 --- a/lambdas/bedrock_invoker_2/lambda.tf +++ /dev/null @@ -1,74 +0,0 @@ -locals { - lambda_function_name = "levio-esta-bedrock-invoker-2" - timeout = 600 - runtime = "python3.11" - powertools_layer_arn = "arn:aws:lambda:${var.aws_region}:017000801446:layer:AWSLambdaPowertoolsPythonV2:67" -} - -data "aws_caller_identity" "current" {} - - -module "lambda_function_container_image" { - source = "terraform-aws-modules/lambda/aws" - function_name = local.lambda_function_name - handler = "index.lambda_handler" - publish = true - runtime = local.runtime - timeout = local.timeout - layers = [local.powertools_layer_arn] - source_path = "${path.module}/src" - s3_bucket = var.lambda_storage_bucket - memory_size = 256 - role_name = "${local.lambda_function_name}-role" - attach_policy_statements = true - - policy_statements = { - log_group = { - effect = "Allow" - actions = [ - "logs:CreateLogGroup" - ] - resources = [ - "arn:aws:logs:*:*:*" - ] - } - - bedrock = { - effect = "Allow" - actions = [ - "bedrock:InvokeModel" - ] - resources = [ - "arn:aws:bedrock:*:*:*" - ] - } - - s3 = { - effect = "Allow" - actions = [ - "s3:Get*", - "s3:List*", - "s3:Describe*", - "s3:PutObject", - "s3-object-lambda:Get*", - "s3-object-lambda:List*", - "s3-object-lambda:WriteGetObjectResponse" - ] - resources = var.allowed_s3_resources - } - - log_write = { - effect = "Allow" - - resources = [ - "arn:aws:logs:*:*:log-group:/aws/${local.lambda_function_name}/*:*" - ] - - actions = [ - "logs:CreateLogStream", - "logs:PutLogEvents", - ] - } - - } -} diff --git a/lambdas/bedrock_invoker_2/output.tf b/lambdas/bedrock_invoker_2/output.tf deleted file mode 100644 index cd2e3af..0000000 --- a/lambdas/bedrock_invoker_2/output.tf +++ /dev/null @@ -1,7 +0,0 @@ -output "lambda_function_arn" { - value = module.lambda_function_container_image.lambda_function_arn -} - -output "lambda_function_name" { - value = module.lambda_function_container_image.lambda_function_name -} diff --git a/lambdas/bedrock_invoker_2/src/index.py b/lambdas/bedrock_invoker_2/src/index.py deleted file mode 100644 index 0fc177f..0000000 --- a/lambdas/bedrock_invoker_2/src/index.py +++ /dev/null @@ -1,89 +0,0 @@ -import boto3 -import json -from botocore.exceptions import BotoCoreError, ClientError -from botocore.config import Config -import os -import base64 - -s3 = boto3.client('s3') -bedrock = boto3.client('bedrock-runtime', config=Config(read_timeout=1000)) - - -def lambda_handler(event, context): - """ - Invokes a bedrock model with the given parameters and s3 text object - """ - system_prompt = event['system_prompt'] - prompt = event['prompt'] - s3_uris = event['s3_uris'] - - - user_prompt_content = [] - - for s3_uri in s3_uris: - bucket, key = s3_uri.replace("s3://", "").split('/', 1) - extension = os.path.splitext(key)[1].lower() - - try: - print(f"Fetching file bucket: {bucket}, key: {key}") - s3_object = s3.get_object(Bucket=bucket, Key=key) - - if extension in ['.txt', '.csv', '.json']: - user_prompt_content.append({ - 'type': 'text', - 'text': s3_object['Body'].read().decode('utf-8') - }) - elif extension in ['.jpg', '.png', '.jpeg']: - image_data = s3_object['Body'].read() - encoded_image_data = base64.b64encode(image_data).decode() - media_type = f"image/{'png' if extension == '.png' else 'jpeg'}" - user_prompt_content.append({ - 'type': 'image', - 'source': {'type': 'base64', 'data': encoded_image_data, 'media_type': media_type} - }) - - except ClientError as e: - return { - 'statusCode': 400, - 'body': str(e) - } - - user_prompt_content.append({ - 'type': 'text', - 'text': f"{prompt}" - }) - - claude_body = { - "anthropic_version": "bedrock-2023-05-31", - "max_tokens": 4096, - "system": system_prompt, - "messages": [ - { - "role": "user", - "content": user_prompt_content - } - ] - } - - bedrock_model = 'anthropic.claude-3-sonnet-20240229-v1:0' - print(f"Invoke bedock with this model: ", bedrock_model) - print(f"Invoke claude with system prompt: ", system_prompt) - print(f"Invoke claude with prompt: ", prompt) - - try: - response = bedrock.invoke_model( - body=json.dumps(claude_body), - contentType='application/json', - accept='application/json', - modelId=bedrock_model, - ) - return { - 'statusCode': 200, - 'body': response['body'].read().decode('utf-8') - } - - except BotoCoreError as e: - return { - 'statusCode': 400, - 'body': str(e) - } diff --git a/lambdas/bedrock_invoker_2/variables.tf b/lambdas/bedrock_invoker_2/variables.tf deleted file mode 100644 index 45ce8a5..0000000 --- a/lambdas/bedrock_invoker_2/variables.tf +++ /dev/null @@ -1,15 +0,0 @@ -variable "lambda_storage_bucket" { - type = string - nullable = false -} - -variable "aws_region" { - type = string - nullable = false -} - -variable "allowed_s3_resources" { - type = list(string) - nullable = false - description = "values for the s3 resources that the lambda function can access" -} \ No newline at end of file diff --git a/state_machines/rfp_email_form_fill/state_machine.json b/state_machines/rfp_email_form_fill/state_machine.json index 80bbf78..6d6bfbb 100644 --- a/state_machines/rfp_email_form_fill/state_machine.json +++ b/state_machines/rfp_email_form_fill/state_machine.json @@ -96,7 +96,7 @@ "Payload": { "system_prompt.$": "$.system_prompt", "prompt.$": "$.prompt.prompt", - "s3_arn.$": "$.s3_arn" + "s3_uris.$": "$.s3_uris" } }, "Resource": "arn:aws:states:::lambda:invoke", @@ -128,7 +128,7 @@ "ItemSelector": { "system_prompt.$": "$.get_prompts_output.parsed_JSON.system_prompt", "prompt.$": "$$.Map.Item.Value", - "s3_arn.$": "$.parsed_attachments_for_llm_ouput.attachment_arns[0]" + "s3_uris.$": "$.parsed_attachments_for_llm_ouput.attachment_arns" }, "ResultPath": "$.prompt_responses", "Next": "Map claude response to document filler argument", diff --git a/terraform/modules.tf b/terraform/modules.tf index 1e671a1..d7089ce 100644 --- a/terraform/modules.tf +++ b/terraform/modules.tf @@ -275,13 +275,6 @@ module "bedrock_invoker" { allowed_s3_resources = [module.s3_bucket.s3_bucket_arn, "${module.s3_bucket.s3_bucket_arn}/*"] } -module "bedrock_invoker_2" { - source = "../lambdas/bedrock_invoker_2" - lambda_storage_bucket = aws_s3_bucket.lambda_storage.id - aws_region = var.aws_region - allowed_s3_resources = [module.s3_bucket.s3_bucket_arn, "${module.s3_bucket.s3_bucket_arn}/*"] -} - module "levio_esta_text_replacer" { source = "../lambdas/text_replacer" lambda_storage_bucket = aws_s3_bucket.lambda_storage.id