• 沒有找到結果。

Create an Amazon Lex Bot (Console)

在文檔中 Amazon Lex (頁 79-90)

AWS Lambda Function: Blueprint Summary

Step 1: Create an Amazon Lex Bot (Console)

For this exercise, create a bot for ordering flowers, called OrderFlowersBot.

To create an Amazon Lex bot (console)

1. Sign in to the AWS Management Console and open the Amazon Lex console at https://

console.aws.amazon.com/lex/.

2. If this is your first bot, choose Get Started; otherwise, on the Bots page, choose Create.

3. On the Create your Lex bot page, provide the following information, and then choose Create.

• Choose the OrderFlowers blueprint.

• Leave the default bot name (OrderFlowers).

• For COPPA, choose No.

• For User utterance storage, choose the appropriate response.

4. Choose Create. The console makes the necessary requests to Amazon Lex to save the configuration.

The console then displays the bot editor window.

5. Wait for confirmation that your bot was built.

6. Test the bot.

NoteYou can test the bot by typing text into the test window, or, for compatible browsers, by choosing the microphone button in the test window and speaking.

Use the following example text to engage in conversation with the bot to order flowers:

From this input, the bot infers the OrderFlowers intent and prompts for slot data. When you provide all of the required slot data, the bot fulfills the intent (OrderFlowers) by returning all of the information to the client application (in this case, the console). The console shows the information in the test window.

Specifically:

• In the statement "What day do you want the roses to be picked up?,"the term "roses" appears because the prompt for the pickupDate slot is configured using substitutions, {FlowerType}.

Verify this in the console.

• The "Okay, your roses will be ready..." statement is the confirmation prompt that you configured.

• The last statement ("FlowerType:roses...") is just the slot data that is returned to the client, in this case, in the test window. In the next exercise, you use a Lambda function to fulfill the intent, in which case you get a message indicating that the order is fulfilled.

Next Step

Step 2 (Optional): Review the Details of Information Flow (Console) (p. 75)

Step 2 (Optional): Review the Details of Information Flow (Console)

This section explains the flow of information between a client and Amazon Lex for each user input in our example conversation.

The example uses the console test window for the conversation with the bot.

To open the Amazon Lex test window

1. Sign in to the AWS Management Console and open the Amazon Lex console at https://

console.aws.amazon.com/lex/.

2. Choose the bot to test.

3. From the right side of the console, choose Test chatbot.

To see the flow of information for spoken or typed content, choose the appropriate topic.

Topics

• Step 2a (Optional): Review the Details of the Spoken Information Flow (Console) (p. 75)

• Step 2b (Optional): Review the Details of the Typed Information Flow (Console) (p. 80)

Step 2a (Optional): Review the Details of the Spoken Information Flow (Console)

This section explains the flow of information between the client and Amazon Lex when the client uses speech to send requests. For more information, see PostContent (p. 434).

1. The user says: I would like to order some flowers.

a. The client (console) sends the following PostContent (p. 434) request to Amazon Lex:

POST /bot/OrderFlowers/alias/$LATEST/user/4o9wwdhx6nlheferh6a73fujd3118f5w/content HTTP/1.1

x-amz-lex-session-attributes: "e30="

Content-Type: "audio/x-l16; sample-rate=16000; channel-count=1"

Accept: "audio/mpeg"

Request body input stream

Both the request URI and the body provide information to Amazon Lex:

• Request URI – Provides the bot name (OrderFlowers), bot alias ($LATEST), and the user name (a random string that identifies the user). content indicates that this is a PostContent API request (not a PostText request).

• Request headers

• x-amz-lex-session-attributes – The base64-encoded value represents "{}". When the client makes the first request, there are no session attributes.

• Content-Type – Reflects the audio format.

• Request body – The user input audio stream ("I would like to order some flowers.").

NoteIf the user chooses to send text ("I would like to order some flowers") to the PostContent API instead of speaking, the request body is the user input. The Content-Type header is set accordingly:

POST /bot/OrderFlowers/alias/$LATEST/user/4o9wwdhx6nlheferh6a73fujd3118f5w/

content HTTP/1.1

x-amz-lex-session-attributes: "e30="

Content-Type: "text/plain; charset=utf-8"

Accept: accept Request body input stream

b. From the input stream, Amazon Lex detects the intent (OrderFlowers). It then chooses one of the intent's slots (in this case, the FlowerType) and one of its value elicitation prompts, and then sends a response with the following headers:

x-amz-lex-dialog-state:ElicitSlot

x-amz-lex-input-transcript:I would like to order some flowers.

x-amz-lex-intent-name:OrderFlowers

x-amz-lex-message:What type of flowers would you like to order?

x-amz-lex-session-attributes:e30=

x-amz-lex-slot-to-elicit:FlowerType

x-amz-lex-slots:eyJQaWNrdXBUaW1lIjpudWxsLCJGbG93ZXJUeXBlIjpudWxsLCJQaWNrdXBEYXRlIjpudWxsfQ==

The header values provide the following information:

• x-amz-lex-input-transcript – Provides the transcript of the audio (user input) from the request

• x-amz-lex-message – Provides the transcript of the audio Amazon Lex returned in the response

• x-amz-lex-slots – The base64 encoded version of the slots and values:

{"PickupTime":null,"FlowerType":null,"PickupDate":null}

• x-amz-lex-session-attributes – The base64-encoded version of the session attributes ({})

The client plays the audio in the response body.

2. The user says: roses

a. The client (console) sends the following PostContent (p. 434) request to Amazon Lex:

POST /bot/OrderFlowers/alias/$LATEST/user/4o9wwdhx6nlheferh6a73fujd3118f5w/content HTTP/1.1

x-amz-lex-session-attributes: "e30="

Content-Type: "audio/x-l16; sample-rate=16000; channel-count=1"

Accept: "audio/mpeg"

Request body

input stream ("roses")

The request body is the user input audio stream (roses). The sessionAttributes remains empty.

b. Amazon Lex interprets the input stream in the context of the current intent (it remembers that it had asked this user for information pertaining to the FlowerType slot). Amazon Lex first updates the slot value for the current intent. It then chooses another slot (PickupDate), along with one of its prompt messages (When do you want to pick up the roses?), and returns a response with the following headers:

x-amz-lex-dialog-state:ElicitSlot x-amz-lex-input-transcript:roses x-amz-lex-intent-name:OrderFlowers

x-amz-lex-message:When do you want to pick up the roses?

x-amz-lex-session-attributes:e30=

x-amz-lex-slot-to-elicit:PickupDate

x-amz-lex-slots:eyJQaWNrdXBUaW1lIjpudWxsLCJGbG93ZXJUeXBlIjoicm9zaSdzIiwiUGlja3VwRGF0ZSI6bnVsbH0=

The header values provide the following information:

• x-amz-lex-slots – The base64-encoded version of the slots and values:

{"PickupTime":null,"FlowerType":"roses","PickupDate":null}

• x-amz-lex-session-attributes – The base64-encoded version of the session attributes ({})

The client plays the audio in the response body.

3. The user says: tomorrow

a. The client (console) sends the following PostContent (p. 434) request to Amazon Lex:

POST /bot/OrderFlowers/alias/$LATEST/user/4o9wwdhx6nlheferh6a73fujd3118f5w/content HTTP/1.1

x-amz-lex-session-attributes: "e30="

Content-Type: "audio/x-l16; sample-rate=16000; channel-count=1"

Accept: "audio/mpeg"

Request body

input stream ("tomorrow")

The request body is the user input audio stream ("tomorrow").The sessionAttributes remains empty.

b. Amazon Lex interprets the input stream in the context of the current intent (it remembers that it had asked this user for information pertaining to the PickupDate slot). Amazon Lex updates the slot (PickupDate) value for the current intent. It then chooses another slot to elicit value for (PickupTime) and one of the value elicitation prompts (When do you want to pick up the roses on 2017-03-18?), and returns a response with the following headers:

x-amz-lex-dialog-state:ElicitSlot x-amz-lex-input-transcript:tomorrow x-amz-lex-intent-name:OrderFlowers

x-amz-lex-message:When do you want to pick up the roses on 2017-03-18?

x-amz-lex-session-attributes:e30=

x-amz-lex-slot-to-elicit:PickupTime

x-amz-lex-slots:eyJQaWNrdXBUaW1lIjpudWxsLCJGbG93ZXJUeXBlIjoicm9zaSdzIiwiUGlja3VwRGF0ZSI6IjIwMTctMDMtMTgifQ==

x-amzn-RequestId:3a205b70-0b69-11e7-b447-eb69face3e6f

The header values provide the following information:

• x-amz-lex-slots – The base64-encoded version of the slots and values:

{"PickupTime":null,"FlowerType":"roses","PickupDate":"2017-03-18"}

• x-amz-lex-session-attributes – The base64-encoded version of the session attributes ({})

The client plays the audio in the response body.

4. The user says: 6 pm

a. The client (console) sends the following PostContent (p. 434) request to Amazon Lex:

POST /bot/OrderFlowers/alias/$LATEST/user/4o9wwdhx6nlheferh6a73fujd3118f5w/content HTTP/1.1

x-amz-lex-session-attributes: "e30="

Content-Type: "text/plain; charset=utf-8"

Accept: "audio/mpeg"

Request body

input stream ("6 pm")

The request body is the user input audio stream ("6 pm"). The sessionAttributes remains empty.

b. Amazon Lex interprets the input stream in the context of the current intent (it remembers that it had asked this user for information pertaining to the PickupTime slot). It first updates the slot value for the current intent.

Now Amazon Lex detects that it has information for all of the slots. However, the

OrderFlowers intent is configured with a confirmation message. Therefore, Amazon Lex needs an explicit confirmation from the user before it can proceed to fulfill the intent. It sends a response with the following headers requesting confirmation before ordering the flowers:

x-amz-lex-dialog-state:ConfirmIntent x-amz-lex-input-transcript:six p. m.

x-amz-lex-intent-name:OrderFlowers

x-amz-lex-message:Okay, your roses will be ready for pickup by 18:00 on 2017-03-18.

Does this sound okay?

x-amz-lex-session-attributes:e30=

x-amz-lex-slots:eyJQaWNrdXBUaW1lIjoiMTg6MDAiLCJGbG93ZXJUeXBlIjoicm9zaSdzIiwiUGlja3VwRGF0ZSI6IjIwMTctMDMtMTgifQ==

x-amzn-RequestId:083ca360-0b6a-11e7-b447-eb69face3e6f

The header values provide the following information:

• x-amz-lex-slots – The base64-encoded version of the slots and values:

{"PickupTime":"18:00","FlowerType":"roses","PickupDate":"2017-03-18"}

• x-amz-lex-session-attributes – The base64-encoded version of the session attributes

The client plays the audio in the response body.

5. The user says: Yes

a. The client (console) sends the following PostContent (p. 434) request to Amazon Lex:

POST /bot/OrderFlowers/alias/$LATEST/user/4o9wwdhx6nlheferh6a73fujd3118f5w/content HTTP/1.1

x-amz-lex-session-attributes: "e30="

Content-Type: "audio/x-l16; sample-rate=16000; channel-count=1"

Accept: "audio/mpeg"

Request body

input stream ("Yes")

The request body is the user input audio stream ("Yes"). The sessionAttributes remains empty.

b. Amazon Lex interprets the input stream and understands that the user want to proceed with the order. The OrderFlowers intent is configured with ReturnIntent as the fulfillment activity.

This directs Amazon Lex to return all of the intent data to the client. Amazon Lex returns a response with following:

x-amz-lex-dialog-state:ReadyForFulfillment x-amz-lex-input-transcript:yes

x-amz-lex-intent-name:OrderFlowers x-amz-lex-session-attributes:e30=

x-amz-lex-slots:eyJQaWNrdXBUaW1lIjoiMTg6MDAiLCJGbG93ZXJUeXBlIjoicm9zaSdzIiwiUGlja3VwRGF0ZSI6IjIwMTctMDMtMTgifQ==

Thex-amz-lex-dialog-state response header is set to ReadyForFulfillment. The client can then fulfill the intent.

6. Now, retest the bot. To establish a new (user) context, choose the Clear link in the console. Provide data for the OrderFlowers intent, and include some invalid data. For example:

• Jasmine as the flower type (it is not one of the supported flower types)

• Yesterday as the day when you want to pick up the flowers

Notice that the bot accepts these values because you don't have any code to initialize and validate the user data. In the next section, you add a Lambda function to do this. Note the following about the Lambda function:

• It validates slot data after every user input. It fulfills the intent at the end. That is, the bot

processes the flower order and returns a message to the user instead of simply returning slot data to the client. For more information, see Using Lambda Functions (p. 139).

• It also sets the session attributes. For more information about session attributes, see PostText (p. 444).

After you complete the Getting Started section, you can do the additional exercises (Additional Examples: Creating Amazon Lex Bots (p. 171) ). Book Trip (p. 189) uses session attributes to share cross-intent information to engage in a dynamic conversation with the user.

Next Step

Step 3: Create a Lambda Function (Console) (p. 84)

Step 2b (Optional): Review the Details of the Typed Information Flow (Console)

This section explains flow of information between client and Amazon Lex in which the client uses the PostText API to send requests. For more information, see PostText (p. 444).

1. User types: I would like to order some flowers

a. The client (console) sends the following PostText (p. 444) request to Amazon Lex:

POST /bot/OrderFlowers/alias/$LATEST/user/4o9wwdhx6nlheferh6a73fujd3118f5w/text

"Content-Type":"application/json"

"Content-Encoding":"amz-1.0"

{ "inputText": "I would like to order some flowers", "sessionAttributes": {}

}

Both the request URI and the body provide information to Amazon Lex:

• Request URI – Provides bot name (OrderFlowers), bot alias ($LATEST), and user name (a random string identifying the user). The trailing text indicates that it is a PostText API request (and not PostContent).

 

• Request body – Includes the user input (inputText) and empty sessionAttributes.

When the client makes the first request, there are no session attributes. The Lambda function initiates them later.

b. From the inputText, Amazon Lex detects the intent (OrderFlowers). This intent does not have any code hooks (that is, the Lambda functions) for initialization and validation of user input or fulfillment.

Amazon Lex chooses one of the intent's slots (FlowerType) to elicit the value. It also selects one of the value-elicitation prompts for the slot (all part of the intent configuration), and then sends the following response back to the client. The console displays the message in the response to the user.

The client displays the message in the response.

2. User types: roses

a. The client (console) sends the following PostText (p. 444) request to Amazon Lex:

POST /bot/OrderFlowers/alias/$LATEST/user/4o9wwdhx6nlheferh6a73fujd3118f5w/text

"Content-Type":"application/json"

"Content-Encoding":"amz-1.0"

{

"inputText": "roses", "sessionAttributes": {}

}

The inputText in the request body provides user input. The sessionAttributes remains empty.

b. Amazon Lex first interprets the inputText in the context of the current intent—the service remembers that it had asked the specific user for information about the FlowerType slot.

Amazon Lex first updates the slot value for the current intent and chooses another slot (PickupDate) along with one of its prompt messages—What day do you want the roses to be picked up?— for the slot.

Then, Amazon Lex returns the following response:

The client displays the message in the response.

3. User types: tomorrow

a. The client (console) sends the following PostText (p. 444) request to Amazon Lex:

POST /bot/OrderFlowers/alias/$LATEST/user/4o9wwdhx6nlheferh6a73fujd3118f5w/text

"Content-Type":"application/json"

"Content-Encoding":"amz-1.0"

{ "inputText": "tomorrow", "sessionAttributes": {}

}

The inputText in the request body provides user input. The sessionAttributes remains empty.

b. Amazon Lex first interprets the inputText in the context of the current intent—the service remembers that it had asked the specific user for information about the PickupDate slot.

Amazon Lex updates the slot (PickupDate) value for the current intent. It chooses another slot to elicit value for (PickupTime). It returns one of the value-elicitation prompts—Deliver the roses at what time on 2017-01-05?—to the client.

Amazon Lex then returns the following response:

The client displays the message in the response.

4. User types: 6 pm

a. The client (console) sends the following PostText (p. 444) request to Amazon Lex:

POST /bot/OrderFlowers/alias/$LATEST/user/4o9wwdhx6nlheferh6a73fujd3118f5w/text

"Content-Type":"application/json"

"Content-Encoding":"amz-1.0"

{ "inputText": "6 pm", "sessionAttributes": {}

}

The inputText in the request body provides user input. The sessionAttributes remains empty.

b. Amazon Lex first interprets the inputText in the context of the current intent—the service remembers that it had asked the specific user for information about the PickupTime slot.

Amazon Lex first updates the slot value for the current intent. Now Amazon Lex detects that it has information for all the slots.

The OrderFlowers intent is configured with a confirmation message. Therefore, Amazon Lex needs an explicit confirmation from the user before it can proceed to fulfill the intent. Amazon Lex sends the following message to the client requesting confirmation before ordering the flowers:

The client displays the message in the response.

5. User types: Yes

a. The client (console) sends the following PostText (p. 444) request to Amazon Lex:

POST /bot/OrderFlowers/alias/$LATEST/user/4o9wwdhx6nlheferh6a73fujd3118f5w/text

"Content-Type":"application/json"

"Content-Encoding":"amz-1.0"

{

"inputText": "Yes", "sessionAttributes": {}

}

The inputText in the request body provides user input. The sessionAttributes remains empty.

b. Amazon Lex interprets the inputText in the context of confirming the current intent. It understands that the user want to proceed with the order. The OrderFlowers intent is

configured with ReturnIntent as the fulfillment activity (there is no Lambda function to fulfill the intent). Therefore, Amazon Lex returns the slot data to the client.

Amazon Lex set the dialogState to ReadyForFulfillment. The client can then fulfill the intent.

6. Now test the bot again. To do that, you must choose the Clear link in the console to establish a new (user) context. Now as you provide data for the order flowers intent, try to provide invalid data. For example:

• Jasmine as the flower type (it is not one of the supported flower types).

• Yesterday as the day when you want to pick up the flowers.

Notice that the bot accepts these values because you don't have any code to initialize/validate user data. In the next section, you add a Lambda function to do this. Note the following about the Lambda function:

• The Lambda function validates slot data after every user input. It fulfills the intent at the end.

That is, the bot processes the flowers order and returns a message to the user instead of simply returning slot data to the client. For more information, see Using Lambda Functions (p. 139).

• The Lambda function also sets the session attributes. For more information about session attributes, see PostText (p. 444).

After you complete the Getting Started section, you can do the additional exercises (Additional Examples: Creating Amazon Lex Bots (p. 171) ). Book Trip (p. 189) uses session attributes to share cross-intent information to engage in a dynamic conversation with the user.

Next Step

在文檔中 Amazon Lex (頁 79-90)