Skip to content

Commit

Permalink
udate logging
Browse files Browse the repository at this point in the history
  • Loading branch information
joelbalcaen committed Apr 25, 2024
1 parent 79a123f commit db83705
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions lambdas/bedrock_invoker/src/index.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import boto3
import json
from botocore.exceptions import BotoCoreError, ClientError
from aws_lambda_powertools import Logger

logger = Logger()
s3 = boto3.client('s3')
bedrock = boto3.client('bedrock-runtime')

@logger.inject_lambda_context
def lambda_handler(event, context):
"""
Invokes a bedrock model with the given parameters and s3 text object
"""
s3_arn = event['s3_arn']
prompt = event['prompt']

logger.info(f"Invoking claude with prompt: ",prompt)

# Parse the S3 ARN to get the bucket and key
s3_path = s3_arn.replace("arn:aws:s3:::", "")
bucket, key = s3_path.split('/', 1)

# Download the file from S3
logger.info(f"Fetching file bucket: {bucket}, key: {key}")
try:
s3_object = s3.get_object(Bucket=bucket, Key=key)
except ClientError as e:
Expand All @@ -27,7 +33,8 @@ def lambda_handler(event, context):

# Extract text from the S3 object
extracted_text = s3_object['Body'].read().decode('utf-8')

logger.info(f"Extracted text what is {len(extracted_text)} characters long")

claude_body = {
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 4096,
Expand All @@ -44,14 +51,26 @@ def lambda_handler(event, context):
}
]
}
# Create a copy of claude_body
redacted_claude_body = claude_body.copy()

# Replace all the text values in messages with "redacted"
for message in redacted_claude_body["messages"]:
for content in message["content"]:
if 'text' in content:
content['text'] = "redacted"

logger.info(f"Invoke bedrock with this body: ", redacted_claude_body)
bedrock_model = 'anthropic.claude-3-sonnet-20240229-v1:0'
logger.info(f"Invoke bedock with this model: ", bedrock_model)

# Invoke the Bedrock model with the extracted text and the provided parameters
try:
response = bedrock.invoke_model(
body=json.dumps(claude_body),
contentType='application/json',
accept='application/json',
modelId='anthropic.claude-3-sonnet-20240229-v1:0',
modelId=bedrock_model,
)
except BotoCoreError as e:
return {
Expand Down

0 comments on commit db83705

Please sign in to comment.