lex and amazon lambda building chatbot with...

19
BUILDING CHATBOT WITH AMAZON LEX AND AMAZON LAMBDA

Upload: others

Post on 31-Dec-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: LEX AND AMAZON LAMBDA BUILDING CHATBOT WITH AMAZONmrhudson.pbworks.com/w/file/fetch/130317948/Building chatbot wit… · • Step 1 Login to AWS Management Console • Step 2 Create

BUILDING CHATBOT WITH AMAZON LEX AND AMAZON LAMBDA

Page 2: LEX AND AMAZON LAMBDA BUILDING CHATBOT WITH AMAZONmrhudson.pbworks.com/w/file/fetch/130317948/Building chatbot wit… · • Step 1 Login to AWS Management Console • Step 2 Create

INTRODUCTION

What is a chatbot?

A computer program designed to simulate conversation with human users, especially over the Internet.

https://xkcd.com/948/

https://www.youtube.com/watch?v=fEbzk4vTHsQ

Page 3: LEX AND AMAZON LAMBDA BUILDING CHATBOT WITH AMAZONmrhudson.pbworks.com/w/file/fetch/130317948/Building chatbot wit… · • Step 1 Login to AWS Management Console • Step 2 Create

WHAT IS A BOT

Page 4: LEX AND AMAZON LAMBDA BUILDING CHATBOT WITH AMAZONmrhudson.pbworks.com/w/file/fetch/130317948/Building chatbot wit… · • Step 1 Login to AWS Management Console • Step 2 Create

INTRODUCTION

Users interact with a chatbot via various channels:• Text• Voice• Facebook• Kik

Amazon Lex is the same conversational engine that powers Amazon Alexa. It used for building conversational interfaces for applications using voice and text.

Amazon Lambda is used to run code with zero servers. Also called serverless computing.

Page 5: LEX AND AMAZON LAMBDA BUILDING CHATBOT WITH AMAZONmrhudson.pbworks.com/w/file/fetch/130317948/Building chatbot wit… · • Step 1 Login to AWS Management Console • Step 2 Create

OVERVIEW

• Step 1 Login to AWS Management Console• Step 2 Create a custom bot• Step 3 Create Intents• Step 4 Create Lambda Function (Python)• Step 5 Test Lambda Function• Step 6 Use Lambda Function in the Bot• Step 7 Custom Slot Type• Step 8 Update Lambda Function• Step 9 Test Updated Lambda Function• Step 10 Use Updated Lambda Function in the Bot

Page 6: LEX AND AMAZON LAMBDA BUILDING CHATBOT WITH AMAZONmrhudson.pbworks.com/w/file/fetch/130317948/Building chatbot wit… · • Step 1 Login to AWS Management Console • Step 2 Create

STEP 1SIGN IN TO AWS

• URL: console.aws.amazon.com/console/home

• Account ID: 253640467280

• Type in your username: aws_login_01 to aws_login_40

• Type in your Password

Page 7: LEX AND AMAZON LAMBDA BUILDING CHATBOT WITH AMAZONmrhudson.pbworks.com/w/file/fetch/130317948/Building chatbot wit… · • Step 1 Login to AWS Management Console • Step 2 Create

STEP 2CREATE CHATBOT WITH AMAZON LEX

• On top right, change region to US East (N.Virginia)

• Click Service → Amazon Lex

• Click Custom Bot• Bot Name: name of your bot (e.g. <yourname>LunchBot)

• Output Voice: select any option

• Session timeout: 5 min

• IAM role: leave default

• COPPA: No

• Click Create

Page 8: LEX AND AMAZON LAMBDA BUILDING CHATBOT WITH AMAZONmrhudson.pbworks.com/w/file/fetch/130317948/Building chatbot wit… · • Step 1 Login to AWS Management Console • Step 2 Create

STEP 3 CREATING INTENTS

An intent represents an action that the user wants to perform. You create a bot to support one or more related intents. For example, you might create a bot that orders pizza and drinks.

Intents contain Sample utterances specifying how a user might convey the intent. For example, a user might say "Can I order a pizza please" or "I want to order a pizza".

• Click Create Intent

• Provide a name for your intent (e.g. LunchMenuIntent)

Page 9: LEX AND AMAZON LAMBDA BUILDING CHATBOT WITH AMAZONmrhudson.pbworks.com/w/file/fetch/130317948/Building chatbot wit… · • Step 1 Login to AWS Management Console • Step 2 Create

STEP 3CREATING INTENTS

• Create an Sample utterance.• Utterances are phrases that invoke your intent• e.g. I want to know what’s for lunch• e.g. What’s for lunch

• Slots store data that the use must provide• Create a new slot:

• Name: Day or slotDay• Slot type: AMAZON.DATE• Prompt: For what day?

• You can reference a slot in utterance using {}• e.g. What's for lunch on {Day}

• Click Build• Try it out!

Page 10: LEX AND AMAZON LAMBDA BUILDING CHATBOT WITH AMAZONmrhudson.pbworks.com/w/file/fetch/130317948/Building chatbot wit… · • Step 1 Login to AWS Management Console • Step 2 Create

INTRODUCTION TO PYTHON

Python Variables:x = 5y = "John“

Python Lists:thislist = ["apple", "banana", "cherry"]thislist[1] = "blackcurrant“# Change the second item

Python Dictionary:thisdict = { "apple": "green", "banana": "yellow", "cherry": "red"}thisdict["apple"] = "red“ # Change apple color to red

Python Conditions:

a = 33b = 200if b > a: print("b is greater than a")

Python Functions:def my_function(): print("Hello from a function")

my_function() # calling a function

Page 11: LEX AND AMAZON LAMBDA BUILDING CHATBOT WITH AMAZONmrhudson.pbworks.com/w/file/fetch/130317948/Building chatbot wit… · • Step 1 Login to AWS Management Console • Step 2 Create

STEP 4CREATE LAMBDA FUNCTION

• Click on Services → Lambda

• Click on Create Function

• Select Author from scratch

• Name: <yourname>LunchFunction

• Runtime: Python 3.6

• Role: Choose an existing role

• Existing role: SHAD_lamda_basic_execution

• Click on Create function

Page 12: LEX AND AMAZON LAMBDA BUILDING CHATBOT WITH AMAZONmrhudson.pbworks.com/w/file/fetch/130317948/Building chatbot wit… · • Step 1 Login to AWS Management Console • Step 2 Create

STEP 4CREATE LAMBDA FUNCTION

import datetimefrom dateutil.parser import parse

def lambda_handler(event, context): # get the current intent intent_name = event['currentIntent']['name'] # Dispatch to your bot's intent handlers if intent_name == 'LunchMenuItem': return get_menu_handler(event) # Event Handler for get menu intent.def get_menu_handler(event): # get date date = event['currentIntent']['slots']['Day'] # get the day of week from the date day_of_week = parse(date).weekday() # get the menu item for the meal and day of week menu_item = get_menu_item(day_of_week) return create_response(event, menu_item)

# Returns the menu item for a specific meal and day of weekdef get_menu_item(day_of_week): # menu is represented as a dictinary menu = ['salad', 'pizza', 'pasta', 'soup', 'hamburger', 'sushi', 'pizza'] return menu[day_of_week]

# Generates a valid response for AWS Lexdef create_response(event, menu_item): return { 'sessionAttributes': event['sessionAttributes'], 'dialogAction':{ 'type':'Close', 'fulfillmentState': 'Fulfilled', 'message':{ 'contentType':'PlainText', 'content':menu_item } } }

GitHub Repository:https://bit.ly/2yEG9yB

Page 13: LEX AND AMAZON LAMBDA BUILDING CHATBOT WITH AMAZONmrhudson.pbworks.com/w/file/fetch/130317948/Building chatbot wit… · • Step 1 Login to AWS Management Console • Step 2 Create

STEP 5TESTING LAMBDA FUNCTION

{ "currentIntent": { "slots": { "slotDay": "2030-11-08" }, "name": "LunchMenuItem", "confirmationStatus": "None" }, "bot": { "alias": "$LATEST", "version": "$LATEST", "name": "MakeAppointment" }, "userId": "John", "invocationSource": "DialogCodeHook", "outputDialogMode": "Text", "messageVersion": "1.0", "sessionAttributes": {}}

• Click on Select a test event → Configure Test Events• For Event Name, provide a name for this test case

• Event Template → Amazon Lex Make Appointment• Amazon Lex event that is passed to a Lambda function in a specific format:

The Lambda Function is case sensitive, make lambda function recognizes your values.

Page 14: LEX AND AMAZON LAMBDA BUILDING CHATBOT WITH AMAZONmrhudson.pbworks.com/w/file/fetch/130317948/Building chatbot wit… · • Step 1 Login to AWS Management Console • Step 2 Create

STEP 6LEX + LAMBDA FUNCTION

• Click on Services → Amazon Lex

• Click on your bot (e.g. <yourname>LunchBot)

• Under Fulfillment, select AWS Lambda Function

• Select your lambda function (e.g. <yourname>LunchFunction)

• Click Build

• Try it out!

Page 15: LEX AND AMAZON LAMBDA BUILDING CHATBOT WITH AMAZONmrhudson.pbworks.com/w/file/fetch/130317948/Building chatbot wit… · • Step 1 Login to AWS Management Console • Step 2 Create

STEP 7CUSTOM SLOT TYPE

• Click on Slot Types

• Slot Type Name: MealType

• Slot Resolution: Restrict to Slot values and Synonyms

• Values:• Lunch

• Breakfast

• Dinner

• Click on Add Slot To Intent

• Add new slot to the intent as we did previously with Day slot.

Page 16: LEX AND AMAZON LAMBDA BUILDING CHATBOT WITH AMAZONmrhudson.pbworks.com/w/file/fetch/130317948/Building chatbot wit… · • Step 1 Login to AWS Management Console • Step 2 Create

STEP 8UPDATE LAMBDA FUNCTION

import datetimefrom dateutil.parser import parse

def lambda_handler(event, context): # get the current intent intent_name = event['currentIntent']['name']

# Dispatch to your bot's intent handlers if intent_name == 'GetMenuIntent': return get_menu_item_handler(event)

# Event Handler for get menu intent.def get_menu_item_handler(event): # get the meal and date meal = event['currentIntent']['slots']['slotMeal'] date = event['currentIntent']['slots']['slotDay'] # get the day of week from the date day_of_week = parse(date).weekday() # get the menu item for the meal and day of week menu_item = get_menu_item(meal, day_of_week) return create_response(event, menu_item)

# Returns the menu item for a specific meal and day of weekdef get_menu_item(meal, day_of_week): # menu is represented as a dictinary menu = { ‘Breakfast':['cereal', 'fruit bowl', 'eggs', 'pb&j', 'ham sandwich', 'cereal', 'cereal'], ‘Lunch':['salad', 'pizza', 'pasta', 'soup', 'hamburger', 'sushi', 'pizza'], ‘Dinner':['steak', 'salmon', 'spaghetti', 'fish and chips', 'chicken', 'turkey', 'ceaser salad'] } return menu[meal][day_of_week]

# Generates a valid response for AWS Lexdef create_response(event, menu_item): return { 'sessionAttributes': event['sessionAttributes'], 'dialogAction':{ 'type':'Close', 'fulfillmentState': 'Fulfilled', 'message':{ 'contentType':'PlainText', 'content':menu_item } } }GitHub Repository:

https://bit.ly/2PsS5gx

Page 17: LEX AND AMAZON LAMBDA BUILDING CHATBOT WITH AMAZONmrhudson.pbworks.com/w/file/fetch/130317948/Building chatbot wit… · • Step 1 Login to AWS Management Console • Step 2 Create

STEP 9 TEST UPDATED LAMBDA FUNCTION

• You will need to update your saved Lex Test Event to test your updated Lambda Function to ensure everything works properly

{ "currentIntent": { "slots": { "slotDay": "2030-11-08“, "slotMeal": “Breakfast“, }, "name": "LunchMenuItem", "confirmationStatus": "None" }, "bot": { "alias": "$LATEST", "version": "$LATEST", "name": "MakeAppointment" }, "userId": "John", "invocationSource": "DialogCodeHook", "outputDialogMode": "Text", "messageVersion": "1.0", "sessionAttributes": {}}

The Lambda Function is case sensitive, make lambda function recognizes your values.

Page 18: LEX AND AMAZON LAMBDA BUILDING CHATBOT WITH AMAZONmrhudson.pbworks.com/w/file/fetch/130317948/Building chatbot wit… · • Step 1 Login to AWS Management Console • Step 2 Create

STEP 10LEX + UPDATED LAMDBA FUNCTION

• Click on Services → Amazon Lex

• Click on your bot (e.g. <yourname>LunchBot)• Under Fulfillment, select AWS Lambda Function• Select your lambda function (e.g. <yourname>LunchFunction)• Click Build• Try it out!

Page 19: LEX AND AMAZON LAMBDA BUILDING CHATBOT WITH AMAZONmrhudson.pbworks.com/w/file/fetch/130317948/Building chatbot wit… · • Step 1 Login to AWS Management Console • Step 2 Create

QUESTIONS