

サポート終了通知: 2025 年 9 月 15 日、 AWS は Amazon Lex V1 のサポートを終了します。 V1 2025 年 9 月 15 日を過ぎると、Amazon Lex V1 コンソールまたは Amazon Lex V1 リソースにはアクセスできなくなります。Amazon Lex V2 を使用している場合は、代わりに [Amazon Lex V2 ガイド](https://docs.aws.amazon.com/lexv2/latest/dg/what-is.html)を参照してください。

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# 演習 1: Amazon Lex ボットを作成する (AWS CLI)
<a name="gs-cli-create"></a>

通常、ボットを作成するときは、以下のことを行います。

1. スロットタイプを作成し、ボットで扱う情報を定義します。

1. インテントを作成し、ボットでサポートするユーザーアクションを定義します。前に作成したカスタムスロットタイプを使用し、インテントに必要なスロット (パラメータ) を定義します。

1. 定義したインテントを使用するボットを作成します。

この演習では、CLI を使用して新しい Amazon Lex ボットを作成してテストします。ボットの作成には、用意されている JSON 構造を使用します。この演習のコマンドを実行するには、コマンドが実行されるリージョンを確認しておく必要があります。リージョンのリストについては、「[モデル構築のクォータ](gl-limits.md#gl-limits-model-building)」を参照してください。

**Topics**
+ [ステップ 1: サービスにリンクされたロールを作成する (AWS CLI)](gs-create-role.md)
+ [ステップ 2: カスタムスロットタイプを作成する (AWS CLI)](gs-create-flower-types.md)
+ [ステップ 3: インテントを作成する (AWS CLI)](gs-cli-create-order-flowers.md)
+ [ステップ 4: ボットを作成する (AWS CLI)](gs-cli-create-order-flowers-bot.md)
+ [ステップ 5: ボットをテストする (AWS CLI)](gs-create-test.md)

# ステップ 1: サービスにリンクされたロールを作成する (AWS CLI)
<a name="gs-create-role"></a>

Amazon Lex は AWS Identity and Access Management 、ボットに代わって AWS サービスを呼び出すサービスにリンクされたロールを引き受けます。ロールは、アカウント内で Amazon Lex ユースケースにリンクされ、アクセス権限が事前に定義されています。詳細については、「[Amazon Lex のサービスリンクロールの使用](using-service-linked-roles.md)」を参照してください。

コンソールで Amazon Lex ボットを作成済みである場合、サービスにリンクされたロールは自動的に作成されています。「[ステップ 2: カスタムスロットタイプを作成する (AWS CLI)](gs-create-flower-types.md)」へ進んでください。

**サービスリンクロールの作成 (AWS CLI)**

1. で AWS CLI、次のコマンドを入力します。

   ```
   aws iam create-service-linked-role --aws-service-name lex.amazonaws.com
   ```

1. 次のコマンドを使用してポリシーをチェックします。

   ```
   aws iam get-role --role-name AWSServiceRoleForLexBots
   ```

   レスポンスは次のとおりです。

## 次のステップ
<a name="gs-create-next-2"></a>

[ステップ 2: カスタムスロットタイプを作成する (AWS CLI)](gs-create-flower-types.md)

# ステップ 2: カスタムスロットタイプを作成する (AWS CLI)
<a name="gs-create-flower-types"></a>

カスタムスロットタイプを作成し、注文できる花の種類を列挙値とします。次のステップで `OrderFlowers` インテントを作成するときに、このタイプを使用します。*スロットタイプ*は、インテントのスロット (パラメータ) に指定できる値を定義します。

この演習のコマンドを実行するには、コマンドが実行されるリージョンを確認しておく必要があります。リージョンのリストについては、「[モデル構築のクォータ](gl-limits.md#gl-limits-model-building)」を参照してください。

**カスタムスロットタイプを作成するには (AWS CLI)**

1. **FlowerTypes.json** という名前のテキストファイルを作成します。このテキストファイル内に [FlowerTypes.json](gs-cli-create-flower-types-json.md) の JSON コードをコピーします。

1. を使用して [PutSlotType](API_PutSlotType.md)オペレーションを呼び出し AWS CLI 、スロットタイプを作成します。例は、Unix、Linux、および macOS 用にフォーマットされています。Windows の場合は、各行末のバックスラッシュ (\$1) Unix 連結文字をキャレット (^) に置き換えてください。

   ```
   aws lex-models put-slot-type \
       --region region \
       --name FlowerTypes \
       --cli-input-json file://FlowerTypes.json
   ```

   サーバーからのレスポンスは次のとおりです。

   ```
   {
       "enumerationValues": [
           {
               "value": "tulips"
           }, 
           {
               "value": "lilies"
           }, 
           {
               "value": "roses"
           }
       ], 
       "name": "FlowerTypes", 
       "checksum": "checksum", 
       "version": "$LATEST", 
       "lastUpdatedDate": timestamp, 
       "createdDate": timestamp, 
       "description": "Types of flowers to pick up"
   }
   ```

## 次のステップ
<a name="gs-create-next-3"></a>

[ステップ 3: インテントを作成する (AWS CLI)](gs-cli-create-order-flowers.md)

# FlowerTypes.json
<a name="gs-cli-create-flower-types-json"></a>

次のコードは、`FlowerTypes` カスタムスロットタイプを作成するために必要な JSON データです。

```
{
    "enumerationValues": [
        {
            "value": "tulips"
        },
        {
            "value": "lilies"
        },
        {
            "value": "roses"
        }
    ],
    "name": "FlowerTypes",
    "description": "Types of flowers to pick up"
}
```

# ステップ 3: インテントを作成する (AWS CLI)
<a name="gs-cli-create-order-flowers"></a>

`OrderFlowersBot` ボットのインテントを作成し、3 つのスロット (パラメータ) を指定します。スロットを使用することで、ボットはインテントを達成できます。
+ `FlowerType` は、注文できる花の種類を指定するカスタムスロットタイプです。
+ `AMAZON.DATE` および `AMAZON.TIME` は、花の配送日時をユーザーから取得するための組み込みスロットタイプです。

この演習のコマンドを実行するには、コマンドが実行されるリージョンを確認しておく必要があります。リージョンのリストについては、「[モデル構築のクォータ](gl-limits.md#gl-limits-model-building)」を参照してください。

**`OrderFlowers` インテントを作成するには (AWS CLI)**

1. **OrderFlowers.json** という名前のテキストファイルを作成します。このテキストファイル内に [OrderFlowers.json](gs-cli-create-order-flowers-json.md) の JSON コードをコピーします。

1. で AWS CLI、 [PutIntent](API_PutIntent.md)オペレーションを呼び出してインテントを作成します。例は、Unix、Linux、および macOS 用にフォーマットされています。Windows の場合は、各行末のバックスラッシュ (\$1) Unix 連結文字をキャレット (^) に置き換えてください。

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

   サーバーは以下のように応答します。

## 次のステップ
<a name="gs-create-next-4"></a>

[ステップ 4: ボットを作成する (AWS CLI)](gs-cli-create-order-flowers-bot.md)

# OrderFlowers.json
<a name="gs-cli-create-order-flowers-json"></a>

次のコードは、`OrderFlowers` インテントを作成するために必要な JSON データです。

```
{
    "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": "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"
        },
        {
            "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"
}
```

# ステップ 4: ボットを作成する (AWS CLI)
<a name="gs-cli-create-order-flowers-bot"></a>

`OrderFlowersBot` ボットには、1 つのインテント (前のステップで作成した `OrderFlowers` インテント) があります。この演習のコマンドを実行するには、コマンドが実行されるリージョンを確認しておく必要があります。リージョンのリストについては、「[モデル構築のクォータ](gl-limits.md#gl-limits-model-building)」を参照してください。

**注記**  
次の AWS CLI 例は、Unix、Linux、macOS 用にフォーマットされています。Windows の場合は、`"\$LATEST"` を `$LATEST` に変更してください。

**`OrderFlowersBot` ボットを作成するには (AWS CLI)**

1. **OrderFlowersBot.json** という名前のテキストファイルを作成します。このテキストファイル内に [OrderFlowersBot.json](gs-cli-create-order-flowers-bot-json.md) の JSON コードをコピーします。

1. で AWS CLI、 [PutBot](API_PutBot.md)オペレーションを呼び出してボットを作成します。例は、Unix、Linux、および macOS 用にフォーマットされています。Windows の場合は、各行末のバックスラッシュ (\$1) Unix 連結文字をキャレット (^) に置き換えてください。

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

   サーバーからのレスポンスは次のとおりです。ボットを作成または更新すると、`status` フィールドは `BUILDING` に設定されます。これは、ボットの使用準備が整っていないことを示します。ボットの使用準備が整ったことを確認するには、次のステップで [GetBot](API_GetBot.md) オペレーションを使用します。

   

1. 新しいボットの使用準備が整っているかどうかを確認するには、次のコマンドを実行します。`status` フィールドから `READY` が返されるまで、このコマンドを繰り返します。例は、Unix、Linux、および macOS 用にフォーマットされています。Windows の場合は、各行末のバックスラッシュ (\$1) Unix 連結文字をキャレット (^) に置き換えてください。

   ```
   aws lex-models get-bot \
       --region region \
       --name OrderFlowersBot \
       --version-or-alias "\$LATEST"
   ```

   レスポンス内で `status` フィールドを探します。

   ```
   {
       "status": "READY", 
       
       ...
       
   }
   ```

## 次のステップ
<a name="gs-create-next-5"></a>

[ステップ 5: ボットをテストする (AWS CLI)](gs-create-test.md)

# OrderFlowersBot.json
<a name="gs-cli-create-order-flowers-bot-json"></a>

次のコードは、Amazon Lex ボット `OrderFlowers` の構築に必要な JSON データを示しています。

```
{
    "intents": [
        {
            "intentVersion": "$LATEST",
            "intentName": "OrderFlowers"
        }
    ],
    "name": "OrderFlowersBot",
    "locale": "en-US",
    "abortStatement": {
        "messages": [
            {
                "content": "Sorry, I'm not able to assist at this time",
                "contentType": "PlainText"
            }
        ]
    },
    "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"
}
```

# ステップ 5: ボットをテストする (AWS CLI)
<a name="gs-create-test"></a>

ボットをテストするには、テキストベースまたは音声ベースのテストを使用できます。

**Topics**
+ [テキスト入力を使用してテストする (AWS CLI)](gs-create-test-text.md)
+ [音声入力を使用してボットをテストする (AWS CLI)](gs-create-test-speech.md)

# テキスト入力を使用してテストする (AWS CLI)
<a name="gs-create-test-text"></a>

 テキスト入力でボットが正しく動作することを確認するには、[PostText](API_runtime_PostText.md) オペレーションを使用します。この演習のコマンドを実行するには、コマンドが実行されるリージョンを確認しておく必要があります。リージョンのリストについては、「[ランタイム Service Quotas](gl-limits.md#gl-limits-runtime)」を参照してください。

**注記**  
次の AWS CLI 例は、Unix、Linux、macOS 用にフォーマットされています。Windows の場合は、`"\$LATEST"` を `$LATEST` に変更し、各行末のバックスラッシュ (\$1) 連結文字をキャレット (^) に置き換えてください。

**テキストを使用してボットをテストするには (AWS CLI)**

1. で AWS CLI、`OrderFlowersBot`ボットとの会話を開始します。例は、Unix、Linux、および macOS 用にフォーマットされています。Windows の場合は、各行末のバックスラッシュ (\$1) Unix 連結文字をキャレット (^) に置き換えてください。

   ```
   aws lex-runtime post-text \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --input-text "i would like to order flowers"
   ```

   Amazon Lex は、ユーザーのインテントを認識し、次のレスポンスを返すことで会話を開始します。

   ```
   {
       "slotToElicit": "FlowerType", 
       "slots": {
           "PickupDate": null, 
           "PickupTime": null, 
           "FlowerType": null
       }, 
       "dialogState": "ElicitSlot", 
       "message": "What type of flowers would you like to order?", 
       "intentName": "OrderFlowers"
   }
   ```

1. 以下のコマンドを実行して、ボットとの会話を終了します。

   ```
   aws lex-runtime post-text \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --input-text "roses"
   ```

   ```
   aws lex-runtime post-text  \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --input-text "tuesday"
   ```

   ```
   aws lex-runtime post-text  \
       --region region \
       --bot-name OrderFlowersBot --bot-alias "\$LATEST" \
       --user-id UserOne \
       --input-text "10:00 a.m."
   ```

   ```
   aws lex-runtime post-text  \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --input-text "yes"
   ```

    注文を確認すると、Amazon Lex はフルフィルメントレスポンスを送信して会話を完了します。

   ```
   {
       "slots": {
           "PickupDate": "2017-05-16", 
           "PickupTime": "10:00", 
           "FlowerType": "roses"
       }, 
       "dialogState": "ReadyForFulfillment", 
       "intentName": "OrderFlowers"
   }
   ```

## 次のステップ
<a name="gs-create-next-test"></a>

[音声入力を使用してボットをテストする (AWS CLI)](gs-create-test-speech.md)

# 音声入力を使用してボットをテストする (AWS CLI)
<a name="gs-create-test-speech"></a>

音声ファイルを使用してボットをテストするには、[PostContent](API_runtime_PostContent.md) オペレーションを使用します。音声ファイルは、Amazon Polly テキスト読み上げ機能のオペレーションを使用して生成します。

この演習のコマンドを実行するには、Amazon Lex および Amazon Polly コマンドが実行されるリージョンを確認しておく必要があります。Amazon Lex のリージョンのリストについては「[ランタイム Service Quotas](gl-limits.md#gl-limits-runtime)」を参照してください。Amazon Polly でサポートされているリージョンとエンドポイントの一覧については、*Amazon Web Services 全般のリファレンス*の「[AWS リージョンとエンドポイント](https://docs.aws.amazon.com/general/latest/gr/rande.html#pol_region)」を参照してください。

**注記**  
次の AWS CLI 例は、Unix、Linux、macOS 用にフォーマットされています。Windows の場合は、`"\$LATEST"` を `$LATEST` に変更し、各行末のバックスラッシュ (\$1) 連結文字をキャレット (^) に置き換えてください。

**音声入力を使用してボットをテストするには (AWS CLI)**

1. で AWS CLI、Amazon Polly を使用してオーディオファイルを作成します。例は、Unix、Linux、および macOS 用にフォーマットされています。Windows の場合は、各行末のバックスラッシュ (\$1) Unix 連結文字をキャレット (^) に置き換えてください。

   ```
   aws polly synthesize-speech \
       --region region \
       --output-format pcm \
       --text "i would like to order flowers" \
       --voice-id "Salli" \
       IntentSpeech.mpg
   ```

1. 音声ファイルを Amazon Lex に送信するには、次のコマンドを実行します。Amazon Lex は、レスポンスの音声を指定の出力ファイルに保存します。

   ```
   aws lex-runtime post-content \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --content-type "audio/l16; rate=16000; channels=1" \
       --input-stream IntentSpeech.mpg \
       IntentOutputSpeech.mpg
   ```

   Amazon Lex は、レスポンスで最初のスロットをリクエストします。音声レスポンスは指定の出力ファイルに保存されます。

   ```
   {
       "contentType": "audio/mpeg", 
       "slotToElicit": "FlowerType", 
       "dialogState": "ElicitSlot", 
       "intentName": "OrderFlowers", 
       "inputTranscript": "i would like to order some flowers", 
       "slots": {
           "PickupDate": null, 
           "PickupTime": null, 
           "FlowerType": null
       }, 
       "message": "What type of flowers would you like to order?"
   }
   ```

1. バラの花束を注文するには、次の音声ファイルを作成して Amazon Lex に送信します。

   ```
   aws polly synthesize-speech \
       --region region \
       --output-format pcm \
       --text "roses" \
       --voice-id "Salli" \ 
       FlowerTypeSpeech.mpg
   ```

   ```
   aws lex-runtime post-content \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --content-type "audio/l16; rate=16000; channels=1" \
       --input-stream FlowerTypeSpeech.mpg \
       FlowerTypeOutputSpeech.mpg
   ```

1. 配達日を設定するには、次の音声ファイルを作成して Amazon Lex に送信します:

   ```
   aws polly synthesize-speech \
       --region region \
       --output-format pcm \
       --text "tuesday" \
       --voice-id "Salli" \ 
       DateSpeech.mpg
   ```

   ```
   aws lex-runtime post-content \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --content-type "audio/l16; rate=16000; channels=1" \
       --input-stream DateSpeech.mpg \
       DateOutputSpeech.mpg
   ```

1. 配送時間を設定するには、次の音声ファイルを作成して Amazon Lex に送信します:

   ```
   aws polly synthesize-speech \
       --region region \
       --output-format pcm \
       --text "10:00 a.m." \
       --voice-id "Salli" \
       TimeSpeech.mpg
   ```

   ```
   aws lex-runtime post-content \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --content-type "audio/l16; rate=16000; channels=1" \
       --input-stream TimeSpeech.mpg \
       TimeOutputSpeech.mpg
   ```

1. 配達を確認するには、次の音声ファイルを作成して Amazon Lex に送信します。

   ```
   aws polly synthesize-speech \
       --region region \
       --output-format pcm \
       --text "yes" \
       --voice-id "Salli" \
       ConfirmSpeech.mpg
   ```

   ```
   aws lex-runtime post-content \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --content-type "audio/l16; rate=16000; channels=1" \
       --input-stream ConfirmSpeech.mpg \
       ConfirmOutputSpeech.mpg
   ```

   配達を確認すると、Amazon Lex からインテントの達成を確認するレスポンスが送信されます。

   ```
   {
       "contentType": "text/plain;charset=utf-8", 
       "dialogState": "ReadyForFulfillment", 
       "intentName": "OrderFlowers", 
       "inputTranscript": "yes", 
       "slots": {
           "PickupDate": "2017-05-16", 
           "PickupTime": "10:00", 
           "FlowerType": "roses"
       }
   }
   ```

## 次のステップ
<a name="gs-cli-next-exercise-2"></a>

[演習 2: 新しい発話を追加する (AWS CLI)](gs-cli-update-utterance.md)