Skip to content

Commit

Permalink
logging
Browse files Browse the repository at this point in the history
  • Loading branch information
joelbalcaen committed Apr 26, 2024
1 parent 135313b commit 06bf54c
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions lambdas/bedrock_invoker/src/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
s3 = boto3.client('s3')
bedrock = boto3.client('bedrock-runtime')


def lambda_handler(event, context):
"""
Invokes a bedrock model with the given parameters and s3 text object
Expand All @@ -14,11 +15,9 @@ def lambda_handler(event, context):

print(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
print(f"Fetching file bucket: {bucket}, key: {key}")
try:
s3_object = s3.get_object(Bucket=bucket, Key=key)
Expand All @@ -28,10 +27,9 @@ def lambda_handler(event, context):
'body': str(e)
}

# Extract text from the S3 object
extracted_text = s3_object['Body'].read().decode('utf-8')
print(f"Extracted text that is {len(extracted_text)} characters long")

claude_body = {
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 4096,
Expand All @@ -48,10 +46,10 @@ def lambda_handler(event, context):
}
]
}
# Create a copy of claude_body

# Replace all the text values in messages with "redacted" since we don't want to log sensitive data
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:
Expand All @@ -61,23 +59,25 @@ def lambda_handler(event, context):
bedrock_model = 'anthropic.claude-3-sonnet-20240229-v1:0'
print(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=bedrock_model,
)

print(response)
print(response['Body'])
response_body = response['Body'].read().decode('utf-8')
print(response_body)
return {
'statusCode': 200,
'body': response_body
}

except BotoCoreError as e:
return {
'statusCode': 400,
'body': str(e)
}

response_body = response['Body'].read().decode('utf-8')

return {
'statusCode': 200,
'body': response_body
}

0 comments on commit 06bf54c

Please sign in to comment.