Open In App

AWS CLI for Conversational Interfaces

Last Updated : 25 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Chatbots and voice assistants have now made it easy for users to have conversations with applications, AWS has services—Amazon Lex and Amazon Polly, to name a few—that make the development of intelligent applications easier. But the AWS CLI makes it easy to manage these services programmatically. AWS CLI is used by developers to manage bots, sessions, and intents of conversational interfaces using just a couple of simple command-line commands. This article explains how to use AWS CLI to effectively manage conversational interfaces while giving an introduction to some key concepts, commands, and real-world examples.

Primary Terminologies

  • AWS CLI: The AWS CLI provides a single tool for all AWS services and is used to run and manage multiple such services. By using only one tool to manipulate so many different AWS services, developers could easily automate and control their infrastructure with command line scripts.
  • Amazon Lex: Amazon Lex is a service provided by AWS that enables the user to create any form of conversational interface, voice or text, and delivers advanced functionality to these interactions by using automatic speech recognition (ASR) and natural language understanding (NLU).
    • Intent: An intent in Amazon Lex is an aim or objective that a user tries to achieve. For example, the intent may be to reserve a flight, order a pizza, or check the weather.
    • Slot: A slot is an information entity needed to fulfill an intent; for example, flight booking may require slots to hold information about the departure city, destination city, and travel dates.
    • Utterances: These are possible phrases, or alternatively, words that a user might say while interacting with the system in question, thus facilitating the comprehension of different ways of asking the same question or command by the bot.
  • Amazon Polly: Amazon Polly is a service that turns text into lifelike speech, allowing developers to create applications that talk and build totally new categories of conversational and interactive experiences.

Architecting AWS CLI for Conversational Interfaces: Integrating Lex and Polly

Here's a visual representation of how AWS CLI interacts with conversational AI services like Amazon Lex and Amazon Polly. This architecture demonstrates the seamless integration between these components to create efficient and scalable conversational interfaces

Aws CLI architecture

Step-by-Step Process: Managing Conversational Interfaces Using AWS CLI

Step 1: Configure AWS

Now configure AWS by providing Access Key and Secret Key

aws configure
Configure AWS

To Config AWS CLI Please Refer To Our Article - AWS CLI Configuration

Step 2: Define Your Intent

Create a JSON File for the Intent

Create a file named intent-definition.json with the following content:

{

"name": "BookFlight",

"description": "An intent for booking flights",

"sampleUtterances": [

"I want to book a flight",

"Book a flight for me",

"Book a ticket from {Origin} to {Destination}"

],

"slots": [

{

"name": "Origin",

"slotType": "AMAZON.City",

"slotConstraint": "Required",

"valueElicitationPrompt": {

"messages": [

{

"content": "Where are you flying from?",

"contentType": "PlainText"

}

],

"maxAttempts": 2

}

},

{

"name": "Destination",

"slotType": "AMAZON.City",

"slotConstraint": "Required",

"valueElicitationPrompt": {

"messages": [

{

"content": "Where are you flying to?",

"contentType": "PlainText"

}

],

"maxAttempts": 2

}

}

],

"fulfillmentActivity": {

"type": "ReturnIntent"

}

}

Define Your Intent

Step 3: Create the Intent

Use the AWS CLI to create the intent:

aws lex-models put-intent --name "BookFlight" --region us-east-1 --cli-input-json file://intent-definition.json
Create the Intent

Step 4: Define Your Bot

Create a JSON File for the Bot

Create a file named bot-definition.json with the following content:

{

"name": "ChatBot",

"description": "A sample Lex bot for flight booking",

"locale": "en-GB", // Change this to the desired locale

"intents": [

{

"intentName": "BookFlight",

"intentVersion": "$LATEST"

}

],

"abortStatement": {

"messages": [

{

"content": "Sorry, I couldn't process your request. Please try again later.",

"contentType": "PlainText"

}

]

},

"clarificationPrompt": {

"maxAttempts": 2,

"messages": [

{

"content": "Sorry, I didn't understand. Can you please repeat?",

"contentType": "PlainText"

}

]

},

"childDirected": false,

"idleSessionTTLInSeconds": 300

}

Define Your Bot

Step 5: Create the Bot

Use the AWS CLI to create the bot:

aws lex-models put-bot --name "ChatBot" --region us-east-1 --cli-input-json file://bot-definition.json
Create the Bot

Now wait for some time it take time to create. We can check status by using following command

It change status BUILDING to READY

aws lex-models get-bot-versions --name "ChatBot" --region us-east-1
Changing Status

Step 6: Create a Bot Alias

Before going to create a Bot Alias update Bot version by using following command

aws lex-models create-bot-version --name "ChatBot" --region us-east-1
Create a Bot Alias

Create a Bot Alias

Use the AWS CLI to create an alias for the bot:

aws lex-models create-bot-alias --bot-name "ChatBot" --bot-version "1" --name "Prod" --region us-east-1
Create a Bot Alias

Step 7: Test the Bot

Test the Bot Using post-text

Use the AWS CLI to send a test message to the bot:

aws lex-runtime post-text --bot-name "ChatBot" --bot-alias "Prod" --user-id "test-user" --input-text "I want to book a flight"
Test the Bot

Provide Slot Value for Origin

You need to send the value for the Origin slot. You can do this by sending another request with the slot value:

aws lex-runtime post-text --bot-name "ChatBot" --bot-alias "Prod" --user-id "test-user" --input-text "I am flying from New York"
Provide Slot Value for Origin

Provide Slot Value for Destination

Once you provide the Origin, the bot will ask for the Destination slot. You need to send the value for Destination:

aws lex-runtime post-text --bot-name "ChatBot" --bot-alias "Prod" --user-id "test-user" --input-text "I want to go to Los Angeles"
Provide Slot Value for Destination

Step 8: Enable Text-to-Speech with AWS Polly

Amazon Polly can be integrated with your bot to convert text responses into speech.

aws polly synthesize-speech --text "Hello! How can I assist you today?" --output-format mp3 --voice-id Joanna chatbot-response.mp3
Enabling AWS Polly

Use a media player to listen to the generated speech output.

To download the file to your local machine:

Use scp (Secure Copy Protocol) if you're working from a local machine with access to the EC2 instance:

scp -i /path/to/your/key.pem ec2-user@<EC2-Instance-IP>:/home/ec2-user/chatbot-response.mp3 /local/directory/
Downloading the file

Now we can check our downloaded file in our local system

checking Downloaded file

Here is the Audio to listen

Conclusion

AWS CLI provides automation and simplification capabilities to do tasks under services like Amazon Lex and Amazon Polly. With the help of the AWS CLI, developers can efficiently create, update, and test conversational bots for text and voice interaction. This will enhance productivity, reduce the time taken up by manual configuration, and lead to flexible chatbot development that can scale up. This can help build simple text-based chatbots or more advanced voice-assisted interfaces that manage the full flow of conversational systems for the developer using this tool.


Next Article
Article Tags :

Similar Reads