/Building-AWS-Comprehend-Lex-ChatBot-

Building AWS Comprehend Lex ChatBot

Primary LanguagePythonMIT LicenseMIT

Building-AWS-Comprehend-Lex-ChatBot

Scenario

Chatbot can be used in many fields and more humanly helping people to automate the triggering of target events. However, the chatbot with emotional analysis can give different answers to users' input and can record information about interactions with users to further optimize the service. This experiment will teach you how to build your own serverless ChatBot and use lambda functions to connect Amazon Comprehend and Amazon Translation services.

Prerequisites

  • Sign-in an AWS account, and make sure you have select N.Virginia region.
  • Download source file from this Github.

Lab tutorial

This lab has two part, one for lambda creating and one for Lex Chatbot building.


Create a lambda function

  1. Open the Lambda in the console.
  2. Choose Create Function.
  3. Choose Author from scratch.
  4. For Runtime, choose Python 3.6.

1.png

  1. For Role, choose Create a custom role and set following information:
  • For Role Description, enter Lambda execution role permissions.
  • For IAM Role, choose Create a new IAM Role.
  • For Role Name, enter myLexLambdaRole.

2.png

  • For Policy, Edit it and use the following policy:

     {
       "Version": "2012-10-17",
       "Statement": [{
           "Effect": "Allow",
           "Action": [
             "logs:CreateLogGroup",
             "logs:CreateLogStream",
             "logs:PutLogEvents"
           ],
           "Resource": "arn:aws:logs:*:*:*"
         },
         {
           "Action": [
             "comprehend:DetectDominantLanguage",
             "comprehend:DetectSentiment"
           ],
           "Effect": "Allow",
           "Resource": "*"
         },
         {
           "Effect": "Allow",
           "Action": [
             "cloudwatch:GetMetricStatistics",
             "cloudwatch:DescribeAlarms",
             "cloudwatch:DescribeAlarmsForMetric",
             "kms:DescribeKey",
             "kms:ListAliases",
             "lambda:GetPolicy",
             "lambda:ListFunctions",
             "lex:*",
             "polly:DescribeVoices",
             "polly:SynthesizeSpeech"
           ],
           "Resource": [
             "*"
           ]
         },
         {
             "Action": [
               "translate:TranslateText",
               "comprehend:DetectDominantLanguage",
               "cloudwatch:GetMetricStatistics"
             ],
             "Effect": "Allow",
             "Resource": "*"
           }
    
       ]
     }
    
  1. Click Allow.
  2. Go back to Create function page and choose the role you just created and click Create function.
  3. Paste the lambda script in this Github.

Set up Lex Chatbot

  1. In AWS console, select Lex service.
  2. Click create.
  3. Click Custom bot and set following content:
  • Type Bot name as lambda_bot.
  • Choose the Output voice None.
  • Set the Session timeout.
  • In COPPA, check Yes.

3.png

  1. Click Create.
  2. Click Create intent and type the intent name as lambda_bot_intent.

4.png

  1. Add some sample utterances. This allows lex to capture your answers based on these keywords. (ex. good, bad)

5.png

  1. In Fulfillment, click AWS Lambda function and choose the lambda function you've created.
  2. It will show the notification to add permission to lambda function, click OK.
  3. Click Build.
  4. After building your Chatbot, click Test to type some sentence and make sure it works. (ex. This service is too bad to use!)

6.png


Conclusion

  • Now you learn how to build your serverless Lex chatbot with sentiment analysis function.
  • This architecture can be combined with more APIs for AWS services, please give it a try.