import json, os
import boto3
bedrock = boto3.client("bedrock-runtime")
MODEL_ID = os.environ["MODEL_ID"] # a model enabled in your Region
TONES = {"funny", "formal", "emotional", "poetic"}
LANGS = {"English", "Hinglish"}
def handler(event, context):
body = json.loads(event.get("body") or "{}")
name = str(body.get("name", "")).strip()[:40]
relation = str(body.get("relation", "friend")).strip()[:30]
tone = body.get("tone", "emotional")
lang = body.get("language", "English")
if not name or tone not in TONES or lang not in LANGS:
return {"statusCode": 400,
"body": json.dumps({"error": "Need name, valid tone and language"})}
prompt = (f"Write a 3-4 sentence Diwali greeting for my {relation} named {name}. "
f"Tone: {tone}. Language: {lang} (Roman script only if Hinglish). "
"Mention light or togetherness. Output only the greeting.")
res = bedrock.converse(
modelId=MODEL_ID,
messages=[{"role": "user", "content": [{"text": prompt}]}],
inferenceConfig={"maxTokens": 300, "temperature": 0.8},
)
wish = res["output"]["message"]["content"][0]["text"].strip()
return {"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps({"wish": wish}, ensure_ascii=False)}
Loading article