Amazon Lex is a service for building conversational interfaces into any application. The new Amazon Lex V2 console and APIs make it easier to build, deploy, and manage bots. The Amazon Lex V2 console and APIs provide a simple information architecture in which the bot intents and slot types are scoped to a specific language. Versioning is performed at the bot level so that resources such as intents and slot types don’t have to be versioned individually. All the resources associated with the bot (language, intents, and slot types) are archived as part of the created bot version. This new methodology makes it easier to manage your bots. You can find more information in the Amazon Lex V2 API reference.
The Amazon Lex V2 console and APIs introduce a new method to build and interact with your bots. This post illustrates different methods to interact with a Amazon Lex V2 chatbot programmatically using the:
For this post, we use English (US), which is en_US.
Interacting via the AWS CLI
The AWS Command Line Interface (CLI) is a unified tool to manage your AWS services. With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts.
To demonstrate the AWS CLI, you can use AWS CloudShell. AWS CloudShell is a browser-based shell that makes it easy to securely manage, explore, and interact with your AWS resources. CloudShell is pre-authenticated with your console credentials.
There are some cases where AWS CloudShell don’t have the latest version of the AWS CLI installed. If you receive an error “Invalid choice: ‘lexv2-runtime’”, follow the instructions contained in this guide to update the AWS CLI on AWS CloudShell.
And this time when I run the “aws lexv2-runtime” command it is recognized:
[cloudshell-user@ip-10-1-148-180 ~]$ aws lexv2-runtime
usage: aws [options] [ ...] [parameters]
To see help text, you can run:
aws help
aws help
aws help
aws: error: the following arguments are required: operation
After collecting the required information, enter your bot’s parameters (please keep in mind your IDs will be different):
To get started, you will submit the text “I would like to see the dentist” by using the following command:
aws lexv2-runtime recognize-text --bot-id 'UFIDGBA6DE' --bot-alias-id 'TSTALIASID' --locale-id 'en_US' --session-id 'test_session' --text 'I would like to see the dentist'
Diving deep into the response you will notice the dialog state is ElicitSlot, or in other words, the chatbot is requiring the user to provide a value for the slot type ‘AppointmentType’.
Interact programmatically with Amazon Lex V2 via Boto3
Boto is the AWS SDK for Python. It enables Python developers to create, configure, and manage AWS services, such as Amazon Elastic Compute Cloud (Amazon EC2) and Amazon Simple Storage Service (Amazon S3). Boto provides an easy-to-use, object-oriented API, as well as low-level access to AWS services.
Support for Amazon Lex V2 APIs is introduced in version 1.16.59.
To interact with your Amazon Lex bot using the Boto3 lexv2-runtime, you need to use the values that you collected earlier for botID, AliasId, and localeID.
Boto3 code example
After I collected the required information, I have the following parameters (these will be different for your account):
The first message you send to your bot is “I would like to see the dentist “:
import boto3
# LexV2 client uses 'lexv2-runtime'
client = boto3.client('lexv2-runtime')
# Submit the text 'I would like to see a dentist'
response = client.recognize_text(
botId='UFIDGBA6DE',
botAliasId='TSTALIASID',
localeId='en_US',
sessionId="test_session",
text='I would like to see the dentist')
We can dive deeper into the response to see the dialog state is ElicitSlot, meaning the chatbot is requiring the user to provide a value for the slot type AppointmentType:
The AWS SDK for DotNet provides an interface that you can use from your DotNet applications to interact with your bots. You can use the SDK for DotNet to build client applications for users.
The following application interacts with the . It uses the AmazonLexRuntimeV2Client from the SDK for DotNet to call the RecognizeText operation to conduct a conversation with the bot.
To run the application, you need the following values:
botId –The identifier assigned to the bot when you created it. You can see the bot ID on the Amazon Lex console on the Bot details
botAliasId – The identifier assigned to the bot alias when you created it. You can see the bot alias ID on the Amazon Lex console on the Aliases If you can’t see the alias ID in the list, choose the gear icon on the upper right and turn on Alias ID.
accessKey and secretKey – The authentication keys for your account. If you don’t have a set of keys, create them using the AWS Identity and Access Management (IAM) console.
sessionId – An identifier for the session with the Amazon Lex bot. In this case, the code uses a random UUID.
Additionally, if your bot isn’t in the US East (N. Virginia) AWS Region, make sure that you change the region value.
Let’s look at the following sample code to understand how we can easily interact with Amazon Lex V2 from the DotNet framework.
First, we create an AmazonLexRuntimeV2Client object with the access key and secret:
// create a LexV2 runtime client
AmazonLexRuntimeV2Client client =
new AmazonLexRuntimeV2Client(access_key
, secret_key
, Amazon.RegionEndpoint.GetBySystemName(preferred_region));
Provide the following information:
// provide the parameters
// Randomly generated GUID unique per session
string sessionId = Guid.NewGuid().ToString();
// provide the unique BotId
string botId = "XXXXX";
//provide the unique BotAliasId
string botAliasId = "XXXXX";
// refer to the list of supported locales
string localeId = "en_US";
The request uses the required URI parameters botAliasId, botId, localeId, and sessionId.
The user initiates the conversation by providing the intent:
string userInput1 = ""
Next, we call the GetRecongizeTextV2Response method by passing the required parameters and the AmazonLexRuntimeV2Client client object:
var response1 = GetRecongizeTextV2Response(client, botId, botAliasId, localeId, sessionId, userInput1)
Inside the GetRecongizeTextV2Response method, we create a RecognizeTextRequest object, containing the parameters to the RecognizeText operation:
The RecognizeTextRequest in JSON format looks like the following:
{
"BotAliasId": "YOURBOTALIASID",
"BotId": "YOURBOTID",
"LocaleId": "en_US",
"RequestAttributes": {
},
"SessionId": "cb447544-1920-432d-aa41-4ff2237a0a62",
"SessionStateValue": null,
"Text": "I would like to see the dentist"
}
Then we send the user input to Amazon Lex. Client applications can use this API to send requests to Amazon Lex at runtime. You can send requests synchronously or asynchronously. For this post, we send the request using the RecognizeTextAsync method asynchronously. Amazon Lex interprets the user input using the machine learning model that it built for the bot. See the following code:
As you can see, Amazon Lex returns the next message inside the Content as a plain text, with HttpStatusCode as 200.
DotNet SDK code example
The following is the full source code:
using System;
using Amazon.LexModelsV2;
using Amazon.LexRuntimeV2;
using Amazon.LexRuntimeV2.Model;
// This is a sample application to interact with a bot using RecognizeText API using DotNet.
namespace OrderFlowerV2
{
class OrderFlowers
{
const string access_key = "XXXXXXXXXX"; // provide you access key
const string secret_key = "XXXXXXXXXX"; // provide the secret
const string preferred_region = "XXXXX"; // provide an appropriate region
static void Main(string[] args)
{
// create a LexV2 runtime client
AmazonLexRuntimeV2Client client = new AmazonLexRuntimeV2Client(access_key,
secret_key, Amazon.RegionEndpoint.GetBySystemName(preferred_region));
string sessionId = Guid.NewGuid().ToString(); // Randomly generated GUID unique per session.
string botId = "XXXXX"; // provide the unique BotId
string botAliasId = "XXXXX"; //provide the unique BotAliasId
string localeId = "en_US"; // refer to the list of supported locales
// utterance 1
string userInput1 = "I would like to order flowers";
var response1 = GetRecongizeTextV2Response(client
, botId, botAliasId, localeId, sessionId, userInput1);
Console.WriteLine("User - "+userInput1);
foreach (var message in response1.Messages)
{
Console.WriteLine("Bot - "+message.Content);
}
// utterance 2
string userInput2 = "1 rose";
var response2 = GetRecongizeTextV2Response(client
, botId, botAliasId, localeId, sessionId, userInput2);
Console.WriteLine("User - "+userInput2);
foreach (var message in response2.Messages)
{
Console.WriteLine("Bot - "+message.Content);
}
// utterance 3
string userInput3 = "next Monday";
var response3 = GetRecongizeTextV2Response(client, botId, botAliasId, localeId, sessionId, userInput3);
Console.WriteLine("User - "+userInput3);
foreach (var message in response3.Messages)
{
Console.WriteLine("Bot - "+message.Content);
}
// utterance 4
string userInput4 = "7 in the evening";
var response4 = GetRecongizeTextV2Response(client, botId, botAliasId, localeId, sessionId, userInput4);
Console.WriteLine("User - "+userInput4);
foreach (var message in response4.Messages)
{
Console.WriteLine("Bot - "+message.Content);
}
// utterance 5
string userInput5 = "Yes";
var response5 = GetRecongizeTextV2Response(client, botId, botAliasId, localeId, sessionId, userInput5);
Console.WriteLine("User - "+userInput5);
foreach (var message in response5.Messages)
{
Console.WriteLine("Bot - "+message.Content);
}
}
///
/// Call Amazon LexV2 RecognizeTextAsync method to get the response from the Bot
///
///
///
///
///
///
///
///
private static RecognizeTextResponse GetRecongizeTextV2Response(AmazonLexRuntimeV2Client client, string botId, string botAliasId, string localeId, string sessionId, string userInput)
{
// create a RecognizeTextResponse object
RecognizeTextResponse response = new RecognizeTextResponse();
try
{
// create RecognizeTextRequest object with the required parameters
RecognizeTextRequest request = new RecognizeTextRequest
{
BotAliasId = botAliasId,
BotId = botId,
LocaleId = localeId,
SessionId = sessionId,
Text = userInput
};
// call the RecognizeText async method to get the response
response = client.RecognizeTextAsync(request).GetAwaiter().GetResult();
}catch(AmazonLexModelsV2Exception exception)
{
Console.WriteLine("AmazonLexModelsV2Exception - "+exception.Message);
}
catch(AmazonLexRuntimeV2Exception exception)
{
Console.WriteLine("AmazonLexRuntimeV2Exception - "+exception.Message);
}
catch(Exception exception)
{
Console.WriteLine("Exception - "+exception.Message);
}
return response;
}
}
}
Conclusion
Now that you have learned how to interact with your Amazon Lex v2 chatbot programmatically with the AWS CLI, Boto3, and, the AWS SDK for DotNet, you can integrate your bots with your applications or with different platforms. If you want to learn more about Amazon Lex v2, you can explore building a banking bot with support for English and Spanish. You can also dive deep into Amazon Lex through the Developer Guide.
About the Authors
Juan Bustos is an AI Services Specialist Solutions Architect at Amazon Web Services, based in Dallas, TX. Outside of work, he loves spending time writing and playing music as well as trying random restaurants with his family.
Wrick Talukdar is a Senior Solutions Architect with Amazon Web Services and is based in Calgary, Canada. Wrick works with enterprise AWS customers to transform their business through innovative use of cloud technologies. Outside work he enjoys reading and photography.
Esther Lee is a Product Manager for AWS Language AI Services. She is passionate about the intersection of technology and education. Out of the office, Esther enjoys long walks along the beach, dinners with friends and friendly rounds of Mahjong.
Leave a Reply