Exercise 1: Create a bot from an example
In this exercise, you create your first Amazon Lex V2 bot and test it in the Amazon Lex V2 console. For this exercise, you use the OrderFlowers example.
Example overview
You use the OrderFlowers example to create an Amazon Lex V2 bot. For more information about the structure of a bot, see How it works (p. 3).
• Intent – OrderFlowers
• Slot types – One custom slot type called FlowerTypes with enumeration values: roses, lilies, and tulips.
• Slots – The intent requires the following information (that is, slots) before the bot can fulfill the intent.
• PickupTime (AMAZON.TIME built-in type)
• FlowerType (FlowerTypes custom type)
• PickupDate (AMAZON.DATE built-in type)
• Utterance – The following sample utterances indicate the user's intent:
• "I would like to pick up flowers."
• "I would like to order some flowers."
• Prompts – After the bot identifies the intent, it uses the following prompts to fill the slots:
• Prompt for the FlowerType slot – "What type of flowers would you like to order?"
• Prompt for the PickupDate slot – "What day do you want the {FlowerType} to be picked up?"
• Prompt for the PickupTime slot – "At what time do you want the {FlowerType} to be picked up?"
• Confirmation statement – "Okay, your {FlowerType} will be ready for pickup by {PickupTime} on {PickupDate}. Does this sound okay?"
To create an Amazon Lex V2 bot (Console)
1. Sign in to the AWS Management Console and open the Amazon Lex V2 console at https://
console.aws.amazon.com/lexv2/home 2. Choose Create bot.
3. For the Creation method, choose Start with an example.
4. In the Example bots section, choose OrderFlowers from the list.
5. In the Bot configuration section give the bot a name and a description. The name must be unique in your account.
6. In the Permissions section, choose Create a new role with basic Amazon Lex permissions. This will create an AWS Identity and Access Management (IAM) role with the permissions that Amazon Lex V2 needs to run your bot.
Exercise 2: Review the conversation flow
8. In the Session timeout and Advanced settings sections, leave the defaults.
9. Choose Next. Amazon Lex V2 creates your bot.
After you create your bot, you must add one or more languages that the bot supports. A language contains the intents, slot types, and slots that the bot uses to converse with users.
To add a language to a bot
1. In the Language section, choose a supported language, and add a description.
2. Leave the Voice interaction and Intent classification confidence score threshold fields with their defaults.
3. Choose Add language to add the language to the bot.
4. After the language is added, choose Done to continue.
After you choose Done, the console opens the intent editor. You can use the intent editor to examine the intents used by the bot. When you are done examining the bot, you can test to bot.
To test the OrderFlowers bot
1. From the bottom menu, choose Build. Wait for the bot to build.
2. When the build is complete, choose Test to open the test window.
3. Test the bot. Start the conversation with one of the sample utterances, such as "I would like to pick up flowers."
Next steps
Now that you've created you first bot using a template, you can use the console to create your own bot.
For instruction on creating a custom bot, and for more information about creating bots, see Building bots (p. 17).
Exercise 2: Review the conversation flow
In this exercise you review the JSON structures that are sent between your client application and the Amazon Lex V2 bot that you created in Exercise 1: Create a bot from an example (p. 9).
The conversation uses the RecognizeText (p. 545) operation to generate the JSON structures. The RecognizeUtterance (p. 552) returns the same information as HTTP headers in the response.
The JSON structures are divided by each turn of the conversation. A turn is a request from the client application and a response from the bot.
Turn 1
During the first turn of the conversation, the client application initiates the conversation with your bot.
Both the URI and the body of the request provide information about the request.
POST /bots/botId/botAliases/botAliasId/botLocales/localeId/sessions/sessionId/text HTTP/1.1 Content-type: application/json
{ "text": "I would like to order flowers"
}
• The URI identifies the bot that the client application is communicating with. It also includes a session identifier generated by the client application that identifies a specific conversation between a user and the bot.
• The body of the request contains the text that the user typed to the client application. In this case, only the text is sent, however you application can send additional information, such as request attributes or session state. For more information, see the RecognizeText (p. 545) operation.
From text, Amazon Lex V2 detects the user's intent, to order flowers. Amazon Lex V2 chooses one of the intent's slots (FlowerType) and one of the prompts for the slot, and then sends the following response to the client application. The client displays the response to the user.
{ "interpretations": [
"sessionId": "bf445a49-7165-4fcd-9a9c-a782493fba5c", "sessionState": {
"originatingRequestId": "9e8add70-4106-4a10-93f5-2ce2cb959e5f"
} }
Exercise 2: Review the conversation flow
Turns 2 and 3
In turn 2 and 3, the user responds to prompts from the Amazon Lex V2 bot with values that fill the FlowerType and PickupDate slots.
The URI for the second and third turns is the same as the first.
{
"text": "1 dozen roses"
}
The response for turn 2 shows the FlowerType slot filled and provides a prompt to elicit the next slot value.
"sessionId": "bf445a49-7165-4fcd-9a9c-a782493fba5c", "sessionState": {
"value": {
"originatingRequestId": "9e8add70-4106-4a10-93f5-2ce2cb959e5f"
} }
Turn 4
In turn 4, the user provides the final slot value for the intent, the time that the flowers are picked up.
{ "text": "5 in the evening"
}
In the response, Amazon Lex V2 sends a confirmation prompt to the user to confirm that the order is correct. The dialogAction is set to ConfirmIntent and the confirmationState is None.
{ "interpretations": [
Exercise 2: Review the conversation flow 2021-01-04. Does this sound okay?",
"contentType": "PlainText"
} ],
"sessionId": "bf445a49-7165-4fcd-9a9c-a782493fba5c", "sessionState": {
"originatingRequestId": "9e8add70-4106-4a10-93f5-2ce2cb959e5f"
} }
Turn 5
In the final turn, the user responds with to the confirmation prompt.
{ "text": "yes"
}
In the response, Amazon Lex V2 sends indicates that the intent has been fulfilled by setting the confirmationState to Confirmed and the dialogAction to close. All of the slot values are available to the client application.
"sessionId": "bf445a49-7165-4fcd-9a9c-a782493fba5c", "sessionState": {
"dialogAction": {
Exercise 2: Review the conversation flow
"type": "Close"
},
"intent": {
"confirmationState": "Confirmed", "name": "OrderFlowers",
"slots": {
"FlowerType": { "value": {
"interpretedValue": "dozen roses", "originalValue": "dozen roses", "resolvedValues": []
} },
"PickupDate": { "value": {
"interpretedValue": "2021-01-04", "originalValue": "next monday", "resolvedValues": [
"2021-01-04"
] } },
"PickupTime": { "value": {
"interpretedValue": "17:00", "originalValue": "5 evening", "resolvedValues": [
"17:00"
] } } },
"state": "Fulfilled"
},
"originatingRequestId": "9e8add70-4106-4a10-93f5-2ce2cb959e5f"
} }
Building bots
You create an Amazon Lex V2 bot to interact with your users to elicit information to accomplish a task.
For example, you can create a bot that gathers the information needed to order a bouquet of flowers or book a hotel room.
To build a bot, you need the following information:
1. The language that the bot uses to interact with the customer. You can choose one or more languages, each language contains independent intents, slots, and slot types.
2. The intents, or goals, that the bot will help the user fulfill. A bot can contain one or more intents, such as ordering flowers, or booking a hotel and rental car. You need to decide which statements, or utterances, that the user makes to trigger the intent.
3. The information, or slots, that you need to gather from the user to fulfill an intent. For example, you might need to get the type of flowers from the user or the start date of a hotel reservation. You need to define one or more prompts that Amazon Lex V2 uses to elicit the slot value from the user.
4. The type of the slots that you need from the user. You may need to create a custom slot type, such as a list of flowers that a user can order, or you can use a built-in slot type, such as using the AMAZON.Date slot type for the start date of a reservation.
5. The interactions between user and intents. Amazon Lex V2 manages the interactions; you can create an AWS Lambda function to validate and fulfill the intent.
Topics
• Creating a bot (p. 17)
• Adding a language (p. 18)
• Adding intents (p. 18)
• Adding slot types (p. 19)
• Testing a bot using the console (p. 20)
• Creating versions (p. 23)
• Built-in intents and slot types (p. 24)
• Using a custom grammar slot type (p. 43)
• Creating custom slot types (p. 58)
• Creating a custom vocabulary to improve speech recognition (p. 59)
• Using multiple values in a slot (p. 62)
• Using an AWS Lambda function (p. 63)
Creating a bot
Start creating your bot by defining the name, description and some basic information.
To create a bot
1. Sign in to the AWS Management Console and open the Amazon Lex V2 console at https://
console.aws.amazon.com/lexv2/home 2. Choose Create bot.
3. In the Creation method section, choose Create.
4. In the Bot configuration section, give the bot a name and an optional description.
Adding a language
5. In the IAM permissions section, choose an AWS Identity and Access Management (IAM) role that provides Amazon Lex V2 permission to access other AWS services, such as Amazon CloudWatch.
You can have Amazon Lex V2 create the role, or you can choose an existing role with CloudWatch permissions.
6. In the Children's Online Privacy Protection Act (COPPA) section, choose the appropriate response.
7. In the Idle session timeout section, choose the duration that Amazon Lex V2 keeps a session with a user open. Amazon Lex V2 maintains session variables for the duration of the session so that your bot can resume a conversation with the same variables.
8. In the Advanced settings section add tags that help identify the bot, and can be used to control access and monitor resources.
9. Choose Next to create the bot and move to adding a language.
Adding a language
You add one or more languages and locales to your bot to enable it to communicate with users in their languages. You define the intents, slots, and slot types separately for each language so that the utterances, prompts, and slot values are specific to the language.
Your bot must contain at least one language.
To add a language to your bot
1. In the New language section, choose the language that you want to use. You can add a description to help identify the language in lists.
2. If your bot supports voice interaction, in the Voice interaction section, choose the Amazon Polly voice that Amazon Lex V2 uses to communicate with the user. If your bot doesn't support voice, choose None.
3. For the Intent classification confidence score threshold, set the value that Amazon Lex V2 uses to determine whether an intent is the correct intent. You can adjust this value after testing your bot.
4. Choose Add.
Adding intents
Intents are the goals that your users want to fulfill, such as ordering flowers or booking a hotel. Your bot needs to have at least one intent.
By default, all bots contain a single built-in intent, the fallback intent. This intent is used when Amazon Lex V2 does not recognize any other intent. For example, if a user says "I want to order flowers" to a hotel booking intent, the fallback intent is triggered.
To add an intent
1. Sign in to the AWS Management Console and open the Amazon Lex V2 console at https://
console.aws.amazon.com/lexv2/
2. From the list of bots, choose the bot that you want to add the intent to, then from Add languages choose View languages.
3. Choose the language to add the intent to, then choose Intents.
4. Choose Add intent, give your intent a name, and then choose Add.
5. In the intent editor, add the details of your intent.
• Conversation flow – Use the conversation flow diagram to see how a dialog with your bot might look. You can choose different sections of the conversation to jump to that section of the intent editor.
• Intent details – Give the intent a name and description to help identify the purpose of the intent.
You can also see the unique identifier that Amazon Lex V2 assigned to the intent.
• Contexts – Set the input and output contexts for the intent. A context is a state variable associated with an intent. An output context is set when an intent is fulfilled. An intent with an input context can only be recognized with the context is active. An intent with no input contexts can always be recognized.
• Sample utterances – You should provide 10 or more phrases that you expect your users to use to trigger an intent. Amazon Lex V2 generalizes from these phrases to recognize that the user wants to trigger the intent.
• Slots – Define the slots, or parameters, required to fulfill the intent. Each slot has a type that defines the values that can be entered in the slot. You can choose from your custom slot types, or you can choose a built-in slot type.
• Confirmation prompts and declination responses – These responses are used to end the conversation with a user and to confirm or decline fulfillment of the intent. The confirmation prompt asks the user to review slot values. For example, "I've booked a hotel room for Friday. Is this correct?" The declination response is sent to the user when they decline the confirmation.
• Fulfillment updates – Response sent to the user at the start of fulfillment and periodically while the fulfillment is in progress. For example, "I'm changing your password, this may take a few minutes" and "I'm still working on your request." Fulfillment updates are used only for streaming conversations. For more information, see Configuring fulfillment progress updates (p. 138).
• Post fulfillment status – Response sent to the user when an intent is fulfilled. You can set a success message, a failure message, and a timeout message. You can send post-fulfillment messages for both streaming and regular conversations. For example, if the fulfillment succeeds, you can send "I've changed your password." If the fulfillment doesn't succeed, you can send a response with more information, such as "I couldn't change your password, contact the help desk for assistance." If the fulfillment takes too long to process and exceeds the configured timeout period, you can send a message informing the user, such as "Our servers are very busy right not. Try your request again later." For more information, see Configuring fulfillment progress updates (p. 138).
• Closing responses – Response sent to the user after the intent is fulfilled and all other messages are played. For example, a thank you for booking a hotel room. Or it can prompt the user to start a different intent, such as "Thank you for booking a room, would you like to book a rental car?"
• Codehooks – Indicate whether you are using an AWS Lambda function to initialize the intent, validate user input, and to fulfill the intent. You specify the Lambda function in the alias that you use to run the bot.
6. Choose Save intent to save the intent.
Turning off responses
While using your bot, you may find it necessary to turn off start and update responses, post-fulfillment responses, confirmation prompts, and closing responses. Responses can be turned on and off for each intent.
Use the Active toggle to determine whether or not to use a response.
Adding slot types
Slot types define the values that users can supply for your intent variables. You define slot types for each language so that the values are specific to that language. For example, for a slot type that lists paint colors, you could include the value "red" in English, "rouge" in French, and "rojo" in Spanish.
Testing a bot
This topic describes how to create custom slot types that provide values for your intent's slots. You can also use built-in slot types for standard values. For example, you can use the built-in slot type AMAZON.Country for a list of countries in the world.
To create a slot type
1. Sign in to the AWS Management Console and open the Amazon Lex V2 console at https://
console.aws.amazon.com/lexv2/
2. From the list of bots, choose the bot that you want to add the language to, then choose Conversation structure and then All languages.
3. Choose the language to add the slot type to, then choose Slot types.
4. Choose Add slot type, give your slot type a name, and then choose Add.
5. In the slot type editor, add the details of your slot type.
• Slot value resolution – Determines how slot values are resolved. If you choose Expand values, Amazon Lex V2 uses the values as representative values for training. If you use Restrict to slot values the allowed values for the slot are restricted to the ones that you provide.
• Slot type values – The values for the slot. If you chose Restrict to slot values, you can add synonyms for the value. For example, for the value "football" you can add the synonym "soccer." If the user enters "soccer" in a conversation with your bot, the actual value of the slot is "football."
• Use slot values as custom vocabulary – Enable this option to help improve recognition of slot values and synonyms in audio conversations. Don't enable this option when the slot values are common terms, such as "yes," "no," "one," "two, "three," etc.
6. Choose Save slot type.
Testing a bot using the console
The Amazon Lex V2 console contains a test window that you can use to test the interaction with your bot. You use the test window to have a test conversation with your bot and to see the responses that your application receives from the bot.
There are two types of testing that you can perform with your bot. The first, express testing, enables you to test your bot with the exact phrases that you used for creating the bot. For example, if you added the utterance "I want to pick up flowers" to your intent, you can test the bot using that exact phrase.
The second type, complete testing, enables you to test your bot using phrases related to the utterances that you configured. For example, you can use the phrase "Can I order flowers" to start a conversation with your bot.
You test a bot using a specific alias and language. If you are testing the development version of the bot, you use the TestBotAlias alias for testing.
To open the test window
1. Sign in to the AWS Management Console and open the Amazon Lex V2 console at https://
console.aws.amazon.com/lexv2/
2. Choose the bot to test from the list of bots.
3. From the left menu, choose Aliases.
4. From the list of aliases, choose the alias to test.
5. From Languages, choose the radio button of the language to test, and then choose Test.
After you choose Test, the test window opens in the console. You can use the test window to interact with your bot, as shown in the following graphic.
In addition to the conversation, you can also choose Inspect in the test window to see the responses
In addition to the conversation, you can also choose Inspect in the test window to see the responses