import json
import boto3
bedrock = boto3.client("bedrock-runtime", region_name="us-east-1")
MODEL_ID = "anthropic.claude-3-haiku-20240307-v1:0"
def lambda_handler(event, context):
try:
body = json.loads(event.get("body", "{}"))
text_to_summarize = body.get("text", "").strip()
if not text_to_summarize:
return {
"statusCode": 400,
"body": json.dumps({"error": "Missing 'text' field in request body."})
}
payload = {
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 300,
"messages": [
{
"role": "user",
"content": f"Summarize the following text into 3 concise bullet points:\n\n{text_to_summarize}"
}
]
}
response = bedrock.invoke_model(
modelId=MODEL_ID,
body=json.dumps(payload),
contentType="application/json",
accept="application/json"
)
result = json.loads(response["body"].read())
summary = result["content"][0]["text"]
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps({"summary": summary})
}
except Exception as e:
return {
"statusCode": 500,
"body": json.dumps({"error": str(e)})
}
Loading article