LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

[JAWS-UG CLI] Amazon Lex 入門 (3) インテントの作成

Last updated at Posted at 2017-10-08

この記事について

JAWS-UG CLI専門支部 #95 Lex入門 (秋のVUIキャンペーン第1弾)で実施するハンズオン用の手順書です。

前提条件

必要な権限

作業にあたっては、以下の権限を有したIAMユーザもしくはIAMロールを利用してください。

  • 以下のサービスの対するフルコントロール権限
    • IAM
    • Lex

0. 準備

0.1. リージョンを指定

バージニアリージョンを利用します。(リンク先にサービスが提供されているリージョンの一覧が記載されています)

API Reference

コマンド
export AWS_DEFAULT_REGION="us-east-1"

0.2. 資格情報を確認

コマンド
aws configure list

インスタンスプロファイルを設定したEC2インスタンスでアクセスキーを設定せずに実行した場合、以下のようになります。

結果
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile                <not set>             None    None
access_key     ****************QSAA         iam-role
secret_key     ****************c1xY         iam-role
    region                us-west-2              env    AWS_DEFAULT_REGION

0.3. バージョン確認

コマンド
aws --version
結果
(可能な限り最新版を利用しましょう)

0.4. バージョンアップ(必要に応じて)

コマンド
sudo pip install -U awscli

0.5 変数の確認

コマンド
cat << ETX

    SLOT_TYPE_NAME: ${SLOT_TYPE_NAME}

ETX
結果

    SLOT_TYPE_NAME: FlowerTypes

1. インテントの作成

インテントとは、ボットの利用を通してユーザーが到達したい状態およびその過程を定義したものです。

例えば、以下のような設定要素が存在します。

  • sampleUtterances
    • クライアントが入力/発声する文章のサンプル
      • インテントの発動
      • インテントを満たすための質問
    • 多少の揺れはLexが(ある程度は)適切に解釈してくれる(と思う)
  • slots
    • インテントを満たすための入力データ
  • valueElicitationPrompt
    • インテントを満たすためにクライアントに行う質問
  • maxAttempts
    • スロットへの入力を再試行する回数の上限
  • confirmationPrompt
    • インテントが満たされた際に内容に問題がないかを確認するためのメッセージ
  • rejectionStatement
    • インテントを満たすことができなかった際のクライアントに対するメッセージ

これらの要素を利用して、最終的に取得したい情報および取得の段取りを定義します。

1.1. インテントの定義

コマンド
INTENT_FILE_NAME="OrderFlowers.json"

以下の通り、インテントを定義します。

以下の定義では、priorityを指定することで「注文したい花」「配達日」「配達時刻」の順にボットから質問されていきます。
確認の際の質問文には、前の質問で得た結果を含めることができます。

コマンド
cat << EOF > ${INTENT_FILE_NAME}
{
    "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",
    "rejectionStatement": {
        "messages": [
            {
                "content": "Okay, I will not place your order.",
                "contentType": "PlainText"
            }
        ]
    },
    "sampleUtterances": [
        "I would like to pick up flowers",
        "I would like to order some flowers"
    ],
    "slots": [
        {
            "slotType": "${SLOT_TYPE_NAME}",
            "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"
        },
        {
            "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"
        }
    ],
    "fulfillmentActivity": {
        "type": "ReturnIntent"
    },
    "description": "Intent to order a bouquet of flowers for pick up"
}
EOF

cat ${INTENT_FILE_NAME}
コマンド
jsonlint -q ${INTENT_FILE_NAME}

1.2. パラメーターの指定

カスタムスロットタイプ名を指定します。

コマンド
INTENT_NAME="OrderFlowers"

同名のカスタムスロットタイプが存在しないことを確認します。

コマンド
aws lex-models get-intents \
    --name-contains ${INTENT_NAME}
結果
{
    "intents": []
}

1.3. インテントの作成

コマンド
aws lex-models put-intent \
    --name ${INTENT_NAME} \
    --cli-input-json file://${INTENT_FILE_NAME}
結果
{
    "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": "4c382d30-209f-4f0d-829d-6fd541f66630",
    "version": "$LATEST",
    "rejectionStatement": {
        "messages": [
            {
                "content": "Okay, I will not place your order.",
                "contentType": "PlainText"
            }
        ]
    },
    "createdDate": 1507448465.917,
    "lastUpdatedDate": 1507448465.917,
    "sampleUtterances": [
        "I would like to pick up flowers",
        "I would like to order some 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"
}

作成したリソースの確認

インテントが作成されたことを確認します。

コマンド
aws lex-models get-intents \
    --name-contains ${INTENT_NAME}
結果
{
    "intents": [
        {
            "version": "$LATEST",
            "createdDate": 1507448465.917,
            "name": "OrderFlowers",
            "lastUpdatedDate": 1507448465.917,
            "description": "Intent to order a bouquet of flowers for pick up"
        }
    ]
}

インテントの最新バージョンを確認します。

コマンド
INTENT_VERSION=$(aws lex-models get-intent-versions \
    --name ${INTENT_NAME} \
    --query "sort_by(intents,&lastUpdatedDate)[0].version" \
    --output text) \
    && echo ${INTENT_VERSION}
結果
$LATEST

インテントの内容を確認します

コマンド
aws lex-models get-intent \
    --name ${INTENT_NAME} \
    --intent-version ${INTENT_VERSION}
結果
{
    "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": "4c382d30-209f-4f0d-829d-6fd541f66630",
    "version": "$LATEST",
    "rejectionStatement": {
        "messages": [
            {
                "content": "Okay, I will not place your order.",
                "contentType": "PlainText"
            }
        ]
    },
    "createdDate": 1507448465.917,
    "lastUpdatedDate": 1507448465.917,
    "sampleUtterances": [
        "I would like to pick up flowers",
        "I would like to order some 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"
}

2. 補足:ビルトインインテント

スロットタイプ同様に、インテントも事前定義されたものが提供されています。
詳細はドキュメントを確認してください。

Standard Built-in Intents

(オーディオ機器の操作に対応するインテント?)

2.1. ビルトインインテントの確認

ビルトインインテントの一覧

コマンド
aws lex-models get-builtin-intents
結果
{
    "intents": [
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.CancelIntent"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.HelpIntent"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.LoopOffIntent"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.LoopOnIntent"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.NextIntent"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.PauseIntent"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.PreviousIntent"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.RepeatIntent"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.ResumeIntent"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.ShuffleOffIntent"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.ShuffleOnIntent"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.StartOverIntent"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.StopIntent"
        }
    ]
}

特定のビルトインインテントの確認

コマンド
aws lex-models get-builtin-intent \
    --signature "AMAZON.HelpIntent"
結果
{
    "slots": [],
    "supportedLocales": [
        "en-US"
    ],
    "signature": "AMAZON.HelpIntent"
}

以上です。

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up