This post is Private. Only a writer or those who know its URL can access this post.

Article information
Show article in Markdown
Report article
Help us understand the problem. What is going on with this article?

[JAWS-UG CLI] Amazon Lex 入門 (2) カスタムスロットタイプの作成

More than 1 year has passed since last update.

この記事について

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

1. カスタムスロットタイプの作成

スロットとは、ボットがクライアントから取得したい情報です。
例えば、何かの注文を受け付けるボットを作成したいとします。
その際、ボットは「何を注文したいのか」「いつ受け取りたいのか」などをクライアントから取得する必要があります。
このように、取得した情報の一つ一つをスロットといいます。

スロットタイプとは、スロットの中身を事前定義したものです。
例えば、パスタの注文を受け付けるボットを作成するとします。
その場合、スロットタイプとして「ミートソース」「シーフード」「カルボナーラ」などの商品名をあらかじめ事前に定義しておく必要があります。

このハンズオンでは、花の注文を受け付けるボットを作成します
マネージメントコンソールから簡単に同様のボットを作成することが可能です。

作成するスロットは以下の通りです。

  • 注文する花
  • 花を取りに行く日
  • 花を取りに行く時刻

「花を取りに行く日」および「花を取りに行く時刻」は、後述するビルトインスロットタイプで定義済みのため、「注文する花」のみをカスタムスロットタイプとして定義します。

1.1. カスタムスロットの定義

コマンド
SLOT_TYPE_FILE_NAME="FlowerTypes.json"
コマンド
cat << EOF > ${SLOT_TYPE_FILE_NAME}
{
    "enumerationValues": [
        {
            "value": "tulips"
        },
        {
            "value": "lilies"
        },
        {
            "value": "roses"
        }
    ],
    "name": "FlowerTypes",
    "description": "Types of flowers to pick up"
}
EOF

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

1.2. パラメーターの指定

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

コマンド
SLOT_TYPE_NAME="FlowerTypes"

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

コマンド
aws lex-models get-slot-types \
    --name-contains ${SLOT_TYPE_NAME}
結果
{
    "slotTypes": []
}

1.3. カスタムスロットタイプの作成

変数の確認

コマンド
cat << EOF

    SLOT_TYPE_NAME:${SLOT_TYPE_NAME}
    SLOT_TYPE_FILE_NAME:${SLOT_TYPE_FILE_NAME}

EOF
結果
    SLOT_TYPE_NAME:FlowerTypes
    SLOT_TYPE_FILE_NAME:FlowerTypes.json

カスタムスロットタイプの作成

コマンド
aws lex-models put-slot-type \
    --name ${SLOT_TYPE_NAME} \
    --cli-input-json file://${SLOT_TYPE_FILE_NAME}
結果
{
    "enumerationValues": [
        {
            "value": "tulips"
        },
        {
            "value": "lilies"
        },
        {
            "value": "roses"
        }
    ],
    "name": "FlowerTypes",
    "checksum": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "version": "$LATEST",
    "lastUpdatedDate": 1507444439.456,
    "createdDate": 1507444439.456,
    "valueSelectionStrategy": "ORIGINAL_VALUE",
    "description": "Types of flowers to pick up"
}

作成したリソースの確認

作成したスロットタイプを確認します。

コマンド
aws lex-models get-slot-types \
    --name-contains ${SLOT_TYPE_NAME}
結果
{
    "slotTypes": [
        {
            "version": "$LATEST",
            "createdDate": 1507444439.456,
            "name": "FlowerTypes",
            "lastUpdatedDate": 1507444439.456,
            "description": "Types of flowers to pick up"
        }
    ]
}

スロットタイプの最新バージョンを取得します。

コマンド
SLOT_TYPE_VERSION=$(aws lex-models get-slot-type-versions \
    --name ${SLOT_TYPE_NAME} \
    --query "sort_by(slotTypes,&lastUpdatedDate)[0].version" \
    --output text) \
    && echo ${SLOT_TYPE_VERSION}
結果
$LATEST

スロットタイプの内容を確認します。

コマンド
aws lex-models get-slot-type \
    --name ${SLOT_TYPE_NAME} \
    --slot-type-version ${SLOT_TYPE_VERSION}
結果
{
    "enumerationValues": [
        {
            "value": "tulips"
        },
        {
            "value": "lilies"
        },
        {
            "value": "roses"
        }
    ],
    "name": "FlowerTypes",
    "checksum": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "version": "$LATEST",
    "lastUpdatedDate": 1507444439.456,
    "createdDate": 1507444439.456,
    "valueSelectionStrategy": "ORIGINAL_VALUE",
    "description": "Types of flowers to pick up"
}

2. 補足:ビルトインスロット

既存のビルトインスロットタイプを確認します。

Lexでは、一般的に利用されるスロットタイプが事前定義されており、インテント(次章で説明)の作成時に利用することが可能です。

2.1. 一覧の確認

ビルトインスロットタイプの一覧を表示します。

各ビルトインスロットタイプの詳細はドキュメントを確認してください。

Slot Type Reference

コマンド
aws lex-models get-builtin-slot-types
結果
{
    "nextToken": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
",
    "slotTypes": [
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Actor"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.AdministrativeArea"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.AggregateRating"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Airline"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Airport"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Animal"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Artist"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.AT_CITY"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.AT_REGION"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Athlete"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Author"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Book"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.BookSeries"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.BroadcastChannel"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.CivicStructure"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Color"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Comic"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Corporation"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Country"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.CreativeWorkType"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.DATE"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.DayOfWeek"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.DE_CITY"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.DE_FIRST_NAME"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.DE_REGION"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Dessert"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.DeviceType"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Director"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Drink"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.DURATION"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.EducationalOrganization"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.EmailAddress"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.EUROPE_CITY"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.EventType"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Festival"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.FictionalCharacter"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.FinancialService"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Food"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.FoodEstablishment"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.FOUR_DIGIT_NUMBER"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Game"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.GB_CITY"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.GB_FIRST_NAME"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.GB_REGION"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Genre"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Landform"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.LandmarksOrHistoricalBuildings"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Language"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.LocalBusiness"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.LocalBusinessType"
        }
    ]
}
結果(--next-tokenオプションを指定して再度実行)
{
    "slotTypes": [
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.MedicalOrganization"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Month"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Movie"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.MovieSeries"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.MovieTheater"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.MusicAlbum"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.MusicCreativeWorkType"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.MusicEvent"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.MusicGroup"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Musician"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.MusicPlaylist"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.MusicRecording"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.MusicVenue"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.MusicVideo"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.NUMBER"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Organization"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Percentage"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Person"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.PhoneNumber"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.PostalAddress"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Professional"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.ProfessionalType"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.RadioChannel"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Residence"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Room"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.ScreeningEvent"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Service"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.SocialMediaPlatform"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.SoftwareApplication"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.SoftwareGame"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.SpeedUnit"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.Sport"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.SportsEvent"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.SportsTeam"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.StreetAddress"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.TelevisionChannel"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.TIME"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.TVEpisode"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.TVSeason"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.TVSeries"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.US_CITY"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.US_FIRST_NAME"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.US_LAST_NAME"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.US_STATE"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.VideoGame"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.WeatherCondition"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.WeightUnit"
        },
        {
            "supportedLocales": [
                "en-US"
            ],
            "signature": "AMAZON.WrittenCreativeWorkType"
        }
    ]
}   

以上です。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした