练习 2:添加新表达 (AWS CLI) - Amazon Lex V1

如果您使用的是 Amazon Lex V2,请改为参阅 Amazon Lex V2 指南

 

如果您使用的是 Amazon Lex V1,我们建议您将机器人升级到 Amazon Lex V2。我们不再向 V1 添加新功能,强烈建议使用 V2 以获得全新的机器人。

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

练习 2:添加新表达 (AWS CLI)

为完善 Amazon Lex 用来识别用户请求的机器学习模型,我们再向机器人添加一个示例语句。

添加新表达的过程分为四步。

  1. 使用 GetIntent 操作从 Amazon Lex 获取意图。

  2. 更新目的。

  3. 使用 PutIntent 操作将更新后的意图发送回 Amazon Lex。

  4. 使用 GetBotPutBot 操作重建使用该目的的所有自动程序。

要运行本练习中的命令,您需要知道将在其中运行命令的区域。有关区域列表,请参阅 模型构建配额

来自 GetIntent 操作的响应包含一个名为 checksum 的字段,该字段标识目的的一个特定修订版。在使用 PutIntent 操作更新目的时必须提供校验和值。如果不提供,您会收到以下错误消息:

            An error occurred (PreconditionFailedException) when calling 
            the PutIntent operation: Intent intent name already exists. 
            If you are trying to update intent name you must specify the 
            checksum.
        
注意

以下 AWS CLI 示例针对 Linux、Unix 和 macOS 编排了格式。对于 Windows,请将 "\$LATEST" 更改为 $LATEST 并将每行末尾的反斜杠 (\) 继续符替换为脱字号 (^)。

更新 OrderFlowers 目的 (AWS CLI)
  1. 在 AWS CLI 中,从 Amazon Lex 获取意图。Amazon Lex 将输出发送到一个名为 OrderFlowers-V2.json. 的文件

    aws lex-models get-intent \ --region region \ --name OrderFlowers \ --intent-version "\$LATEST" > OrderFlowers-V2.json
  2. 在文本编辑器中打开 OrderFlowers-V2.json

    1. 找到并删除 createdDatelastUpdatedDateversion 字段。

    2. 将下面的内容添加到 sampleUtterances 字段中:

      I want to order flowers
    3. 保存该文件。

  3. 使用以下命令将更新后的意图发送给 Amazon Lex:

    aws lex-models put-intent \ --region region \ --name OrderFlowers \ --cli-input-json file://OrderFlowers-V2.json

    Amazon Lex 会发送以下响应:

    {
        "confirmationPrompt": {
            "maxAttempts": 2, 
            "messages": [
                {
                    "content": "Okay, your {FlowerType} will be ready for pickup by {PickupTime} on {PickupDate}.  Does this sound okay?", 
                    "contentType": "PlainText"
                }
            ]
        }, 
        "name": "OrderFlowers", 
        "checksum": "checksum", 
        "version": "$LATEST", 
        "rejectionStatement": {
            "messages": [
                {
                    "content": "Okay, I will not place your order.", 
                    "contentType": "PlainText"
                }
            ]
        }, 
        "createdDate": timestamp, 
        "lastUpdatedDate": timestamp, 
        "sampleUtterances": [
            "I would like to pick up flowers", 
            "I would like to order some flowers", 
            "I want to order flowers"
        ], 
        "slots": [
            {
                "slotType": "AMAZON.TIME", 
                "name": "PickupTime", 
                "slotConstraint": "Required", 
                "valueElicitationPrompt": {
                    "maxAttempts": 2, 
                    "messages": [
                        {
                            "content": "Pick up the {FlowerType} at what time on {PickupDate}?", 
                            "contentType": "PlainText"
                        }
                    ]
                }, 
                "priority": 3, 
                "description": "The time to pick up the flowers"
            }, 
            {
                "slotType": "FlowerTypes", 
                "name": "FlowerType", 
                "slotConstraint": "Required", 
                "valueElicitationPrompt": {
                    "maxAttempts": 2, 
                    "messages": [
                        {
                            "content": "What type of flowers would you like to order?", 
                            "contentType": "PlainText"
                        }
                    ]
                }, 
                "priority": 1, 
                "slotTypeVersion": "$LATEST", 
                "sampleUtterances": [
                    "I would like to order {FlowerType}"
                ], 
                "description": "The type of flowers to pick up"
            }, 
            {
                "slotType": "AMAZON.DATE", 
                "name": "PickupDate", 
                "slotConstraint": "Required", 
                "valueElicitationPrompt": {
                    "maxAttempts": 2, 
                    "messages": [
                        {
                            "content": "What day do you want the {FlowerType} to be picked up?", 
                            "contentType": "PlainText"
                        }
                    ]
                }, 
                "priority": 2, 
                "description": "The date to pick up the flowers"
            }
        ], 
        "fulfillmentActivity": {
            "type": "ReturnIntent"
        }, 
        "description": "Intent to order a bouquet of flowers for pick up"
    }

现在您已更新了目的,接下来要重建使用它的所有自动程序。

重建 OrderFlowersBot 自动程序 (AWS CLI)
  1. 在 AWS CLI 中,使用以下命令获取 OrderFlowersBot 自动程序的定义并将其保存到一个文件中。

    aws lex-models get-bot \ --region region \ --name OrderFlowersBot \ --version-or-alias "\$LATEST" > OrderFlowersBot-V2.json
  2. 在文本编辑器中,打开 OrderFlowersBot-V2.json。删除 createdDatelastUpdatedDatestatusversion 字段。

  3. 在文本编辑器中,将下面一行添加到自动程序定义中。

    "processBehavior": "BUILD",
  4. 在 AWS CLI 中,通过运行以下命令构建自动程序的新修订版:

    aws lex-models put-bot \ --region region \ --name OrderFlowersBot \ --cli-input-json file://OrderFlowersBot-V2.json

    服务器的响应如下:

    {
        "status": "BUILDING", 
        "intents": [
            {
                "intentVersion": "$LATEST", 
                "intentName": "OrderFlowers"
            }
        ], 
        "name": "OrderFlowersBot", 
        "locale": "en-US", 
        "checksum": "checksum", 
        "abortStatement": {
            "messages": [
                {
                    "content": "Sorry, I'm not able to assist at this time", 
                    "contentType": "PlainText"
                }
            ]
        }, 
        "version": "$LATEST", 
        "lastUpdatedDate": timestamp, 
        "createdDate": timestamp 
        "clarificationPrompt": {
            "maxAttempts": 2, 
            "messages": [
                {
                    "content": "I didn't understand you, what would you like to do?", 
                    "contentType": "PlainText"
                }
            ]
        }, 
        "voiceId": "Salli", 
        "childDirected": false, 
        "idleSessionTTLInSeconds": 600, 
        "description": "Bot to order flowers on the behalf of a user"
    }
    

下一个步骤

练习 3:添加 Lambda 函数 (AWS CLI)