

# Different ways to create a bot with Amazon Lex V2
<a name="create-bot"></a>

You can create a bot with Amazon Lex V2 in the following ways:

1. Use the Amazon Lex V2 console to create a bot using a website interface. For more information, see [Creating a bot using the Amazon Lex V2 console](create-bot-console.md).

1. Use the Descriptive Bot Builder to create a bot using Amazon Bedrock's generative AI capabilities. For more information, see [Use a description to build a bot in Lex V2 with the descriptive bot builder](nld-bots.md).

1. Use bot templates to create a preconfigured bot that matches common business use-cases. For more information, see [Creating Amazon Lex V2 bots using templates](bot-templates.md).

1. Use an [AWS SDK](https://aws.amazon.com/tools/#sdk) to create a bot using API operations.

1. Use the Automated Chatbot designer to create a bot using existing chat transcripts between agents and customers. For more information, see [Creating Amazon Lex V2 bots using the Automated Chatbot Designer](designing.md).

1. Import an existing bot definition. For more information, see [Importing bots in Lex V2](import.md).

1. Use CloudFormation to create a bot. For more information, see [Creating Amazon Lex V2 resources with AWS CloudFormation](creating-resources-with-cloudformation.md).

**Topics**
+ [Creating a bot using the Amazon Lex V2 console](create-bot-console.md)
+ [Creating Amazon Lex V2 bots using templates](bot-templates.md)
+ [Creating Amazon Lex V2 bots using the Automated Chatbot Designer](designing.md)

# Creating a bot using the Amazon Lex V2 console
<a name="create-bot-console"></a>

You create an Amazon Lex V2 bot to interact with your users to elicit information to accomplish a task. For example, you can create a bot that gathers the information needed to order flowers or to book a hotel room. To create a bot using the AWS Console, start by defining the name, description, and some basic information.

1. Sign in to the AWS Management Console and open the Amazon Lex console at [https://console.aws.amazon.com/lex/](https://console.aws.amazon.com/lex/).

1. Choose **Create bot**.

1. In the **Creation method** section, choose **Traditional** and then select **Create a blank bot**.

1. In the **Bot configuration** section, give the bot a name and an optional description.

1. In the **IAM permissions** section, choose an AWS Identity and Access Management (IAM) role that provides Amazon Lex V2 permission to access other AWS services, such as Amazon CloudWatch. You can have Amazon Lex V2 create the role, or you can choose an existing role with CloudWatch permissions. 

1. In the **Children's Online Privacy Protection Act (COPPA)** section, choose the appropriate response.

1. In the **Idle session timeout** section, choose the duration that Amazon Lex V2 keeps a session with a user open. Amazon Lex V2 maintains session variables for the duration of the session so that your bot can resume a conversation with the same variables.

1. In the **Advanced settings** (optional) section, add tags that help identify the bot, control access and monitor resources.

1. Choose **Next** to create the bot and move to adding a language.

# Creating Amazon Lex V2 bots using templates
<a name="bot-templates"></a>

Amazon Lex V2 offers pre-built solutions to create experiences at scale and drive digital engagement. The pre-built bot templates automates and standardizes client experiences. The bot templates provide ready-to-use conversation flows along with both training data and dialog prompts, for both voice and chat modalities. You can expedite the delivery of bot solutions while optimizing resources, so that you can focus on customer relationships.

You can create pre-built bots based on your business use case. You can use the CloudFormation console to select the pre-built options for the related services, such as Amazon S3, Amazon Connect and DynamoDB.

Currently, Amazon Lex V2 supports the following business verticals:
+ Financial services
+ Retail orders
+ Auto insurance
+ Telecommunications
+ Airline services
+ Additional templates will be available in the future.

You can build a bot with the business solution template provided, and customize it for your business requirements.

**Note**  
The templates create resources outside of Amazon Lex V2 through CloudFormation stacks. The stack may need to be modified in other consoles such as Lambda and DynamoDB.

**Prerequisites to build and deploy the bot template:**
+ An AWS account
+ Access to the following AWS services:
  + Amazon Lex V2 to create bots
  + Lambda for the business login functions
  + DynamoDB to create the tables
  + IAM access to create policies and roles
  + AWS CloudFormation to run the stack
+ IAM access and secret key credentials
+ Amazon Connect instance (optional)

**Note**  
The use of different AWS services incurs respective usage costs for each service.

**To build a bot from Amazon Lex V2 templates:**

1. Sign in to the AWS Management Console and open the Amazon Lex console at [https://console.aws.amazon.com/lex/](https://console.aws.amazon.com/lex/).

1. Select **Bot templates** from the left navigation pane.

1. Select which business vertical you want to use for your bot template. NOTE: There are 5 bot templates currently available. More to come soon.

1. Select **Create** for the template you want to use. A tab opens in CloudFormation where you can edit the parameters for the CloudFormation stack. All the options are already completed for the template you have chosen. You can also learn more about how the bot template works by selecting **Learn more**.

1. In the CloudFormation console, CloudFormation creates a default configuration for each of the values for the template you have chosen. You can also select your own stack name, CloudFormation parameters, Amazon DynamoDB table, and (optional) Amazon Connect parameters. 

1. At the bottom of the window, select **Create stack**.

1. CloudFormation processes the request in the background for several minutes to configure your new bot. NOTE: The process automatically creates resources for a DynamoDB table, an Amazon Connect contact flow, and an Amazon Connect instance. You can track the progress in the CloudFormation console, and then navigate back to the Amazon Lex V2 console once the CloudFormation stack creation is completed.

1. If successfully built, a message appears and you can select **Go to bots list** to go to the **Bots** page, where you find your new bot that is ready for your testing and use. 

**Configuring your bot template**

**Lambda functions** – The bot template automatically creates the needed Lambda functions for your deployment. If multiple bots are part of the template solution, then multiple Lambda functions are listed in the CloudFormation parameters. If you have existing Lambda functions to deploy with your bot, you can enter the name of your custom Lambda function.

**Amazon DynamoDB** – The bot template automatically creates the DynamoDB table needed to load your sample policy data. You can also enter the name of your custom DynamoDB table. Your custom DynamoDB table should be formatted in the same way as the default table created by the bot template deployment.

**Amazon Connect** – You can configure your Amazon Connect instance to work with your new bot template by entering the ConnectInstanceARN and a unique ContactFlowName. With the use of Amazon Connect, you can test your bot using an IVR system from end-to-end.

**Troubleshooting your bot template**
+ Check that you have the proper permissions to create the template that you are choosing. Users need CloudFormation:CreateStack permission along with permissions for the AWS resources that are listed within the template. A list of resources that need user permissions are at the bottom of the **Create template** page.
+ If your bot template fails to be created, the red banner within the Amazon Lex V2 console provides a link to the CloudFormation stack that is responsible for creating the template. Within the CloudFormation console, you can view the events tab to see the specific error that caused the template to fail. Once you have reviewed the CloudFormation error, see [ Troubleshooting CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/troubleshooting.html) for more information.
+ Bot templates work with the sample data only. You must populate the DynamoDB table with your data to make the templates work with your custom data.

# Creating Amazon Lex V2 bots using the Automated Chatbot Designer
<a name="designing"></a>

The Automated Chatbot Designer helps you design bots from existing conversation transcripts. It analyzes the transcripts and suggests an initial design with intents and slot types. You can iterate on the bot design, add prompts, build, test, and deploy the bot.

After you create a new bot or add a language to your bot using the Amazon Lex V2 console or API, you can upload transcripts of conversations between two parties. The automated chatbot designer analyzes the transcripts and determines the intents and slot types for the bot. It also labels the conversations that influenced the creation of a particular intent or slot type for your review. 

You use the Amazon Lex V2 console or the API to analyze conversation transcripts and suggest intents and slot types for a bot.

**Note**  
You can only use transcripts in the English (US) language.

You can review the suggested intents and slot types after the chatbot designer finishes the analysis. After you've added a suggested intent or slot type, you can modify it or delete it from the bot design using the console or the API. 

The automated chatbot designer supports conversation transcript files using the Contact Lens for Amazon Connect schema. If you are using a different contact center application, you must transform the conversation transcripts to the format used by the chatbot designer. For information, see [Input transcript format](designing-input-format.md).

To use the automated chatbot designer, you must allow the IAM role that is running the designer access. For the specific IAM policy, see [Allow users to use the Automated Chatbot Designer](security_iam_id-based-policy-examples.md#security_iam-bot-designer). To enable Amazon Lex V2 to encrypt output data with an optional AWS KMS key, you need to update the key with the policy shown in [Allow users to use a AWS KMS key to encrypt and decrypt files](security_iam_id-based-policy-examples.md#security_iam-bot-key).

**Note**  
If you use a KMS key, you must provide a **KMS key policy**, regardless of the IAM role used.

**Topics**
+ [Importing conversation transcripts](designing-import.md)
+ [Creating intents and slot types](designing-create.md)
+ [Input transcript format](designing-input-format.md)
+ [Output transcript format](designing-output-format.md)

# Importing conversation transcripts
<a name="designing-import"></a>

Importing conversation transcripts is a three-step process: 

1. Prepare the transcripts for importing by converting them to the correct format. If you are using Contact Lens for Amazon Connect the transcripts are already in the correct format. 

1. Upload the transcripts to an Amazon S3 bucket. If you are using Contact Lens, your transcripts are already in an S3 bucket.

1. Analyze the transcripts using the Amazon Lex V2 console or API operations. The time that it takes to complete training depends on the volume of transcripts and the complexity of the conversation. Typically, 500 lines of transcripts are analyzed every minute.

Each of these steps is described in the following sections.

## Importing transcripts from Contact Lens for Amazon Connect
<a name="import-connect"></a>

The Amazon Lex V2 automated chatbot designer is compatible with Contact Lens transcript files. To use Contact Lens transcript files, you must turn on Contact Lens and note the location of its output files. 

**To export transcripts from Contact Lens**

1. Turn on Contact Lens in your Amazon Connect instance. For instructions, see [ Enable Contact Lens for Amazon Connect ](https://docs.aws.amazon.com/connect/latest/adminguide/enable-analytics.html) in the *Amazon Connect administrator guide*.

1. Note the location of the S3 bucket that Amazon Connect is using for your instance. To see the location, open the **Data storage** page in the Amazon Connect console. For instructions, see [ Update instance settings ](https://docs.aws.amazon.com/connect/latest/adminguide/update-instance-settings.html) in the *Amazon Connect administrator guide*.

After you have turned on Contact Lens and noted the location of your transcript files, go to [Analyze your transcripts using Amazon Lex V2 console](#import-import) for instructions to import and analyze your transcripts.

## Prepare transcripts
<a name="import-prepare"></a>

Prepare your transcripts by creating transcript files. 
+ Create one transcript file per conversation listing the interaction between the parties. Each interaction in the conversation can span multiple lines. You can provide both redacted and non-redacted versions of the conversation.
+ The file must be in the JSON format specified in [Input transcript format](designing-input-format.md).
+ You must provide at least 1,000 conversational turns. To improve the discovery of your intents and slot types, you should provide around 10,000 or more conversational turns. The automated chatbot designer will only process the first 700,000 turns. 
+ There is no limit to the number of transcript files that you can upload, nor is there a file size restriction.

If you plan to filter the transcripts that you import by date, the files must be in the following directory structure:

```
<path or bucket root>
   --> yyyy
      --> mm
         --> dd
            --> transcript files
```

 The transcript file must contain the date in the format "yyyy-mm-dd" somewhere in the file name. 

**To export transcripts from other contact center applications**

1. Use your contact center application's tools to export conversations. The conversation must contain at least the information specified in [Input transcript format](designing-input-format.md).

1. Transform the transcripts produced by your contact center application to the format described in [Input transcript format](designing-input-format.md). You are responsible for performing the transformation.

We provide three scripts for preparing transcripts. They are:
+ A script to combine Contact Lens transcripts with Amazon Lex V2 conversation logs. Contact Lens transcripts don't include parts of Amazon Connect conversations that interact with Amazon Lex V2 bots. The script requires conversation logs to be turned on for Amazon Lex V2, and appropriate permissions to query conversation log CloudWatch Logs and Contact Lens S3 buckets.
+ A script to transform Amazon Transcribe call analytics to the Amazon Lex V2 input format.
+ A script to transform Amazon Connect chat transcripts to the Amazon Lex V2 input format.

You can download the scripts from this GitHub repository: [ https://github.com/aws-samples/amazon-lex-bot-recommendation-integration ](https://github.com/aws-samples/amazon-lex-bot-recommendation-integration).

## Upload your transcripts to an S3 bucket
<a name="import-upload"></a>

If you are using Contact Lens, your transcript files are already contained in an S3 bucket. For the location and file names of your transcript files, see [ Example Contact Lens output files ](https://docs.aws.amazon.com/connect/latest/adminguide/contact-lens-example-output-files.html) in the *Amazon Connect administrator guide*.

If you are using another contact center application and you have not set up an S3 bucket for your transcript files, follow this procedure. Otherwise, if you have an existing S3 bucket, after logging in to the Amazon S3 console, follow this procedure starting with step 5.

**To upload files to an S3 bucket**

1. Sign in to the AWS Management Console and open the Amazon S3 console at [https://console.aws.amazon.com/s3/](https://console.aws.amazon.com/s3/).

1. Choose **Create bucket**.

1. Give the bucket a name and choose a Region. The Region must be the same one that you use for Amazon Lex V2. Set the other options as required for your use case.

1. Choose **Create bucket**.

1. From the list of buckets, choose an existing bucket or the bucket that you just created

1. Choose **Upload**.

1. Add the transcript files that you want to upload.

1. Choose **Upload**.

## Analyze your transcripts using Amazon Lex V2 console
<a name="import-import"></a>

You can only use automated bot design in an empty language. You can add a new language to an existing bot, or create a new bot. 

**To create a new language in a new bot**

1. Sign in to the AWS Management Console and open the Amazon Lex console at [https://console.aws.amazon.com/lex/](https://console.aws.amazon.com/lex/).

1. Choose **Create bot**

1. Choose **Start with Automated Chatbot Designer**. Fill out the information to create your new bot.

1. Choose **Next**

1. In **Add language to bot** fill out the information for the language.

1. In the **Transcript file location on S3** section, choose the S3 bucket that contains your transcript files and the local path to the files if necessary.

1. You can optionally choose the following:
   + A AWS KMS key to encrypt the transcript data during processing. If you don't select a key, a service AWS KMS key is used.
   + To filter the transcripts to a specific date range. If you choose to filter the transcripts, they must be in the correct folder structure. For more information, see [Prepare transcripts](#import-prepare).

1. Choose **Done**.

Wait for Amazon Lex V2 to process the transcript. You see a completion message when the analysis is complete.

**How to stop analyzing your transcript**

In case you need to stop the analysis of the transcripts you have uploaded, you can stop a running `BotRecommendation` job, which has a `BotRecommendationStatus` status as processing. You can click on the **Stop processing** button present on the banner after submitting a job from the console or by using CLI SDK for the `StopBotRecommendation` API. For more information, see [StopBotRecommendation](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_StopBotRecommendation.html)

After calling the `StopBotRecommendation`, the internal `BotRecommendationStatus` is set to `Stopping` and you are not charged. To make sure the job has stopped, you can call the `DescribeBotRecommendation` API and verify that the `BotRecommendationStatus` is `Stopped`. This usually takes 3-4 minutes.

You are not charged for the processing after the `StopBotRecommendation` API is called.

# Creating intents and slot types
<a name="designing-create"></a>

After the chatbot designer creates intents and slot types, you select the intents and slot types to add to your bot. You can review the details of each intent and slot type to help you decide which recommendations are the most relevant to your use case.

You can click on a recommended intent’s name to view the sample utterances and slots that the chatbot designer has suggested. If you select **Show associated transcripts**, you can also scroll through the conversations that you provided. These transcripts influence the chatbot designer’s recommendation of this intent. If you click on a sample utterance, you can review the primary conversation and the relevant turn of dialog, which influenced that specific utterance.

You can click on a specific slot type’s name to view the slot values that have been recommended. If you select **Show associated transcripts**, you can review the conversations that influenced this slot type, with the agent prompt that elicits for the slot type highlighted. If you click on a specific slot type value, you can review the primary conversation and the relevant turn of dialog that influenced this value.

**To review and add intents and slot type**

1. Sign in to the AWS Management Console and open the Amazon Lex console at [https://console.aws.amazon.com/lex/](https://console.aws.amazon.com/lex/).

1. From the list of bots, choose the bot you want to work with.

1. Choose **View languages**.

1. From the list of languages, choose the language to work with.

1. In **Conversation structure**, choose **Review**.

1. In the list of intents and slot types, choose the ones to add to the bot. You can choose an intent or slot type to see details and the associated transcripts.

Intents are sorted by the confidence that Amazon Lex V2 has that the intent is associated with the processed transcripts.

# Input transcript format
<a name="designing-input-format"></a>

The following is the input file format for generating intents and slot types for your bot. The input file must contain these fields. Other fields are ignored. 

The input format is compatible with the output format from Contact Lens for Amazon Connect. If you are using Contact Lens, you don't need to modify your transcript files. For more information, see [ Example Contact Lens output files ](https://docs.aws.amazon.com/connect/latest/adminguide/contact-lens-example-output-files.html). If you are using another contact center application, you must transform your transcript file to this format. 

```
{
    "Participants": [
        {
            "ParticipantId": "string",
            "ParticipantRole": "AGENT | CUSTOMER"
        }
    ],
    "Version": "1.1.0",
    "ContentMetadata": {
        "RedactionTypes": [
            "PII"
        ],
        "Output": "Raw | Redacted"
    },
    "CustomerMetadata": {
        "ContactId": "string"
    },
    "Transcript": [
        {
            "ParticipantId": "string",
            "Id": "string",
            "Content": "string"
        }
    ]
}
```

The following fields must be present in the input file:
+ **Participants**   Identifies the participants in the conversation and the role that they play.
+ **Version**   The version of the input file format. Always "1.1.0".
+ **ContentMetadata**   Indicates whether you removed sensitive information from the transcript. Set the `Output` field to "Raw" if the transcript contains sensitive information.
+ **CustomerMetadata**   A unique identifier for the conversation.
+ **Transcript**   The text of the conversation between parties in the conversation. Each turn of the conversation is identified with a unique identifier.

# Output transcript format
<a name="designing-output-format"></a>

The output transcript format is nearly the same as the input transcript format. However it also includes some customer metadata and a field listing segments that influenced the suggestion of intents and slot types. You can download the output transcript from the **Review** page in the console or using the Amazon Lex V2 API. For more information, see [Input transcript format](designing-input-format.md).

```
{
    "Participants": [
        {
            "ParticipantId": "string",
            "ParticipantRole": "AGENT | CUSTOMER"
        }
    ],
    "Version": "1.1.0",
    "ContentMetadata": {
 
        "RedactionTypes": [
            "PII"
        ],
        "Output": "Raw | Redacted"
    },
    "CustomerMetadata": {
        "ContactId": "string",
        "FileName": "string",
        "InputFormat": "Lex"
    },
    "InfluencingSegments": [
        {
            "Id": "string",
            "StartTurnIndex": number,
            "EndTurnIndex": number,
            "Intents": [
                {
                    "Id": "string",
                    "Name": "string",
                    "SampleUtteranceIndex": [
                        {
                            "Index": number,
                            "Content": "String"
                        }
                    ]
                }
            ],
            "SlotTypes": [
                {
                    "Id": "string",
                    "Name": "string",
                    "SlotValueIndex": [
                        {
                            "Index": number,
                            "Content": "String"
                        }
                    ]
                }
            ]
        }
    ],
    "Transcript": [
        {
 
            "ParticipantId": "string",
            "Id": "string",
            "Content": "string"
        }
    ]
}
```
+ **CustomerMetadata** – There are two fields added to the `CustomerMetadata` field, the name of the input file that contains the conversation and the input format, which is always "Lex".
+ **InfluencingSegments** – Identifies the segments of the conversation that influenced the suggestion of an intent or slot type. The ID of the intent or slot type identifies the specific one influenced by the conversation.