Building a multilingual question and answer bot with Amazon Lex
You can use Amazon Lex to build a question and answer chatbot. However, if you live in a non-English-speaking country or your business has global reach, you will want a multilingual bot to cater to all your users. This post describes how to achieve that by using the multi-language functionality of your question and answer bot (QnABot).
The QnABot can detect the predominant language in an interaction by using Amazon Comprehend, a natural language processing (NLP) service that uses machine learning to find insights and relationships in text.
The bot then uses Amazon Translate, a neural machine translation service to translate the question to English. Then it can return a preconfigured answer in the end user’s language or translate it back from the default English text.
Although QnABot allows the end-user to interact with an Amazon Lex bot using text or voice, the multilingual feature primarily supports text interactions. Multilingual voice interactions are currently limited to use with Alexa skills.
The solution consists of three easy steps:
- Configure the multi-language functionality.
- Set up the alternate curated answers in a different language.
- Configure your Alexa skill with multiple languages.
For instructions on creating and customizing your bot, see Create a Question and Answer Bot with Amazon Lex and Amazon Alexa or the Q&A Self-Paced Guide. You can also see the following videos on YouTube:
- Deploying Q&A Chatbot Using Cloudformation
- Using the Q&A Chatbot Content Designer, Question Bank and Trying the Chat Experience
- How to Enable Multi-language Support in Q&A Chatbot
- Creating a Alexa Experience from Q&A Chatbot
Prerequisites
To implement this solution, you must have an AWS account. If you don’t have a professional account, the AWS Free Tier lets you gain experience with the AWS platform, products, and services.
You also need to deploy QnABot. If you have not already done so, launch the QnABot on an AWS CloudFormation stack in one of the available Regions.
When specifying your stack name, include QnA
in the name, for example, MultiLangQnABot
.
After you successfully deploy your CloudFormation stack, complete the following steps:
- Open the content designer page of your chatbot.
If this is the first time accessing the chatbot design console, you can find the correct URL on the AWS CloudFormation console on the Output tab of your bot stack. Look for the value for the ContentDesignerURL key. You should also receive an email with temporary credentials to access the QnABot designer.
- On the designer page, choose the menu icon.
- Under Tools, choose Import.
- Expand the Examples/Extensions
- Next to blog-samples, choose Load.
Configuring the multi-language functionality
You are now ready to configure the multi-language functionality. Complete the following steps:
- In the content designer page, under Tools, choose Settings.
- For the ENABLE_MULTI_LANGUAGE_SUPPORT parameter, change the value from false to true.
- Choose Save.
- To test the bot, open the client webpage.
- From the designer page, under Tools, choose QnABot Client.
- Enter the following questions (in English, Spanish, and French):
- How do I modify Q and A Bot content ?
- ¿Cómo modifico el contenido de Q y A Bot ?
- Comment modifier le contenu Q et A Bot ?
The chatbot answers each time in the language you used, as shown in the following animation.
The QnABot is successfully using Amazon Translate to translate the answer automatically into the user’s native language.
Setting up alternate curated answers in a different language
You might need to provide a more natural experience and want to add a curated answer in the native language of your choice. To further customize the translation for each question, you can use the {{handlebar}}
functionality. The QnABot provides the {{handlebar}}
function ifLang
, which takes the locale as a quoted parameter. You can use any of the languages that Amazon Translate supports. For more information, see What Is Amazon Translate?
For example, to customize the translation in Spanish, the ifLang
function uses es
as the locale parameter. See the following code:
Additionally, if an unknown language is detected, you can support that with a default response by using the defaultLang
function. See the following code:
As an example, modify the question you used earlier. Go back to the content designer and complete the following steps:
- Under Tools, choose Edit.
- Select 001 and choose the pencil icon on the right.
- Replace the text in the answer with the following code:
Multi-language and handlebars, in general, also support markdown answers. For example, you could modify the preceding code to highlight the name of the interface that isn’t translated. See the following code:
- Choose Advanced and enter the new code in the Markdown Answer box.
- Choose Update.
If you try to ask your questions again, the answers are different because the chatbot is using your curated version.
You can also import the sample or extension named Language / Multiple Language Support.
This adds two questions to the system: Language.000
and Language.001
. The first question allows the end-user to set their preferred language explicitly; the latter resets the preferred language and allow the QnABot to choose the locale based on the automatically detected predominant language.
Debugging the answers in a different language
You can use the ENABLE_DEBUG_RESPONSES setting to see how local language questions are translated to English by QnABot, and to tune the content as needed to ensure QnABot finds the best answer to a non-English question.
Complete the following steps to set up and test:
- In the content designer page, under Tools, choose Settings.
- For the ENABLE_DEBUG_RESPONSES parameter, change the value from false to true.
- Choose Save.
- To test the bot, open the client webpage.
- From the designer page, under Tools, choose QnABot Client.
- Try one of the question we used before, you can read the translation and use this information to tune your answer.
Configuring your Alexa skill with multiple languages
You first need to create your Alexa skill. For instructions, see Create a Question and Answer Bot with Amazon Lex and Amazon Alexa.
When your Alexa skill is working, add the additional languages by completing the following steps:
- On the Alexa developer console, open your skill.
- From the drop-down menu with your default language, choose Language settings.
- Add all the languages you want to support and choose Save.
- Under CUSTOM, choose JSON Editor.
- Copy the JSON from the editor, switch to the other language you want to support, and enter it in the editor pane (this overwrites the default).
- Choose Save Model.
- Choose Invocation and change the invocation name.
- Choose Save Model.
- Repeat these steps for any language you want to support.
- Build the model.
Testing your Alexa skill
You can now test your Alexa skill in other languages.
- On the Alexa developer console, select your skill.
- Choose Test.
- Change the language and type the invocation name of your skill for that language.
- After Alexa gives her initial greeting, ask the question you used before or any other question you added in the content designer.
Alexa answers you in the selected language.
Now your multilingual chatbot can be published on your website or as an Alexa skill. To integrate the QnABot in your website, you can use lex-web-ui. For instructions, see Deploy a Web UI for your Chatbot.
Conclusion
This post shows you how to configure and use the out-of-the-box feature of your QnABot to localize answers in any language Amazon Translate supports. It is an inexpensive way to deploy a multi-language chatbot, and you don’t need to adapt it to accommodate new Amazon Lex features.
As of this writing, this approach works for text-based interactions only; support for voice is limited to use with Alexa skills only.
For more information about experimenting with the capabilities of QnABot, see the Q&A Self-Paced Guide.
About the Authors
Fabrizio is a Specialist Solutions Architect for Database and Analytics in the AWS Canada Public Sector team. He has worked in the analytics field for the last 20 years, and has recently, and quite by surprise, become a Hockey Dad after moving to Canada.
As a Solutions Architect at AWS supporting our Public Sector customers, Raj excites customers by showing them the art of the possible of what they can build on AWS and helps accelerate their innovation. Raj loves solving puzzles, mentoring, and supporting hackathons and seeing amazing ideas come to life.
Tags: Archive
Leave a Reply