In this section, we will create a new Lambda function called NLPHandler
to process and enhance Lex responses using Amazon Bedrock.
NLPHandler
with Python 3.x runtime.LambdaChatbotExecutionRole
import boto3
import json
import os
bedrock = boto3.client(service_name='bedrock-runtime', region_name='ap-southeast-2')
def enhance_with_bedrock(text):
prompt = f"Rewrite the following response in a natural, fun, and Gen Z vibe — playful, trendy, and a little humorous, but still keeping the original meaning:\n\n{text}"
body = {
"inputText": prompt,
"textGenerationConfig": {
"temperature": 0.7,
"maxTokenCount": 512
}
}
try:
res = bedrock.invoke_model(
modelId="amazon.titan-text-lite-v1",
body=json.dumps(body)
)
payload = json.loads(res['body'].read())
output_text = payload.get('results', [{}])[0].get('outputText', '')
return output_text
except Exception as e:
return "[Error] Could not enhance text"
def lambda_handler(event, context):
lex_output = event.get("lex_output", "")
if not lex_output:
return {"statusCode": 400, "body": "Missing Lex output"}
enhanced_text = enhance_with_bedrock(lex_output)
lambda_client = boto3.client("lambda")
lambda_client.invoke(
FunctionName="WrapOutput", # Lambda function to send to Slack
InvocationType="Event", # async
Payload=json.dumps({
"message": enhanced_text
})
)
return {
"statusCode": 200,
"body": json.dumps({"enhanced_text": enhanced_text})
}
InputHandler
InputHandler
to call NLPHandler
: print("Sending text to Lex...")
lex_msg = send_to_lex(english_text, session_id="test-session-123")
print(f"Lex replied: {lex_msg}")
lambda_client = boto3.client('lambda')
nlp_payload = {
"lex_output": lex_msg,
"original_language": original_language,
"original_text": text
}
lambda_client.invoke(
FunctionName='NLPHandler',
InvocationType='Event', # Async call
Payload=json.dumps(nlp_payload)
)
return {
'statusCode': 200,
'body': json.dumps({
'text': lex_msg,
'response_type': 'in_channel'
}),
'headers': {'Content-Type': 'application/json'}
}
LambdaChatbotExecutionRole
role, select Add permissions > Create inline policy{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "lambda:InvokeFunction",
"Resource": "*"
}
]
}
InvokeLambda
and create the policy.