Create chatbot using bot framework sdk and LUIS — Part 4

Create chatbot using bot framework sdk and LUIS — Part 4

In the previous 3 parts we created a simple e-comm chatbot, we completed the create order conversation flow. Saved the order details in a csv. In this part we will be adding intelligence to our bot using LUIS.

We will complete following steps in this part:

  • Create a LUIS app. Train and publish the app.
  • Use the app in our bot service.
  • Update create order dialog to handle slot filling. that is detecting missing entities in the order details and ask user to provide the values for the required entities.
  • Complete create order flow

Microsoft Azure LUIS:

Microsoft’s Azure LUIS is NLU service which is available on Azure. For using LUIS we create LUIS app. Add intents and entities in our app. We also provide sample user utterances for the intents.

For a given text, LUIS app first detects to which intent the given text belongs to. In simple word it takes the users requirement in user’s own words and then interprets it to understand what exactly user’s need is. It then extracts the important words from the user’s text, which we call entities. For exapmple — in our user case, user provides the order description in his own words. In the order descrption we need to find the word which tells the item name like cake or pastry etc. similarly word whcih tells the flavour of the item like chocolate, cheese etc. So in this example item name and flavour are the entities.

For more details check below links:

Why we need LUIS in our bot service?

We are asking user to provide the order description in his own words. The bot needs to understand whether tthe order consists of only one item or more than one item. If more than one item are there in the order that means we need to refer each order item details as sub order and create multiple line items which an order. Also for ech item bot has to check whether the order detail is com plete or not for example if cake is requested then is the size and flavour of the cake are provided by the user or not. For identifying sub-orders in the order description and the required details i.e. entities in the order description we need th Natural Language understanding service. LUIS is the Natural Language Service which we will be using in our bot.

Trending Bot Articles:

1. How Conversational AI can Automate Customer Service

2. Automated vs Live Chats: What will the Future of Customer Service Look Like?

3. Chatbots As Medical Assistants In COVID-19 Pandemic

4. Chatbot Vs. Intelligent Virtual Assistant — What’s the difference & Why Care?

Create a LUIS app using the JSON provided:

You need to create a LUIS app, for that you should have an azure account subscription. Follow the steps mentioned here to create LUIS App.

Intents and entities required for our bot is defined in the json here.

Copy this orderApp.json on your machine. we will be importing this json in the LUIS app to add the required intents and entities in the luis app.

To import the json in your luis app. Go to your luis app, and click on the down arrow next to New App button.

Now select Import as json and browse to ordersApp.json file. This creates the intents and entities in your app.

Order entity is a structured entity. Thre are sub-entities defined under the main order entity. Explore each entity to understand them clearly. Check utternces defined for create_order intent. See how order entity and other sub-entities are marked in the utterances. These are the training data. You can add more training data to improve accuracy. Click on the test tap to test the app.

Above text returns below result, click on the inspect to see the detailed result. LUIS service has detected there are 2 orders in the given text. For each order it has identified the entities i.e. item_name, item_flavour etc. correctly. Expand item_size entity to check the values retrieved for size_unit and size_value entities. Our LUIS app is working prefectly!

Now we can publish the app. Click on the Publish button to publish the app. You will get 2 options Staging Slot and Production Slot. Select production slot. It will take few minutes to publish the app.

After the app is published, go to Manage tab. On Manage screen you will find “Azure Resources” option pane. Click on the “Azure Resources” option you will be on “Prediction Resources” tab. Copy the URL given in the Example Query. Add this URL in the config file or your project. Add a new attribute in config.py. Name the attribute “LUIS_APP_URL” set the value as the URL cpied from Azure Resources. Trim the text “YOUR_QUERY_HERE” from the URL (u will find this text at the end of the URL).

We will be using this URL to send the user provided order description text to the LUIS App.

Add LUIS App helper function:

Add a new script file luisApp.py and add the below code in it. Below code defnes a function that takes a text as input and pass it to the LUIS by concatenating the LUIS_APP_URL and the input text. The final URL is then invoked through get method. The reslt returned is a json which is then returned to the calling function.

Define class for sub-order details:

We will add a new class order_details.py in the project. This will have all order related attributes. Add below code in order_details.py

Add new attributes in user_details.py

In user_details we will add 2 new attributes orders_list and current_order. Orders_list is a list type attribute this will be used to maintain the list of sub-orders. current_order is int type attribute this is to keep track of the current sub-order while iterating through the sub-orders for chceking completeness of the su-orders.

Modify createorder_dialog to add new steps for LUIS call:

Open createorder_dialog.py add 2 new steps act_step and completeorder_step after order_step. Declare these new steps in the waterfall dialog in init function. Now we have to define these functions.

In act_steps, we will add code to accept the order description provided by the user. Pass this text to LUIS by calling getLuisResponse function of luisApp module. Now iterate through the “order” entities returned by luis. For each sub-order (or order entity) create an orderDetails object. Save the attributes/entities details obtained from Luis in the orderDetails object. Append this object in the orders_list. Add orders list in the context.

In completeorder_step start a new sub-dialog completeorder_dialog to check the completeness of each sub-orders.

We will create the new sub-dialog script after completing all the changes required in createorder_dailog.

We need to update summary_step as well. In the summary step we will add code to fetch the orders_list from the context object. Loop throgh each row and form the order description for each ro wby concatenating item name, quanity, flavour and size values.

Below is the updated createorder_dialog.py:

Add new sub-dialog completeorder_dialog:

Create a new dialog module in oprations folder. Add 4 waterfall steps — flavour_step, quantity_step, size_step and summary_step.

In the flavour step fetch the orders_list from the context. check if for the current_order (this attribute is set to 0 in createorder_dialog) index in orders_list, item_flavour attribute has value or not. If not then prompt user to provide the flavour name for the requested item. Otherwise proceed to the next step.

In the quantity_step fetch orders_list from context. Now check if any value is provided by the user, if yes then set the value as flavour for the the current_order in orders_list. Proceed to check if quantity value is provided or not. If not provided then prompt for the quantity else proceed to the next step.

In the size_step check if any new value is provided by the user, if yes then set it as quantity for the current_order in orders_list. Since size attribute is applicable for only item cake so this value is required only if the item_name is cake. Now check if the current_order item_name is Cake then check the size value, if provided then proceed to the next step else ask user to provide the size.

In the summary_step, check if any new value is provided by the user then set it as size value for the current order in orders_list. Now increase the current_order by 1. If current_order is greater than the length of the orders_list that means we have reached the end of the list hence end the dialog and go back to the createorder_dialog. Else continue completeorder_dialog from the first step.

Below is the code for completeorder_dialog:

Update App.py as we have added new sub-dialog:

With this we have completed coding for this part. In the next part we will complete view order and cancel order flow. We will now test the app using Bot Framework emulator. Refer to Part-1 for steps to run emulator.

Important links:

Source Code — https://github.com/sushmita-mishra/e-comm-chatobot-luis

Part-1 — https://medium.com/@sushmita_mishra18/create-chatbot-using-bot-framework-sdk-and-luis-part-1-e42c26d60366

Part-2 — https://medium.com/@sushmita_mishra18/create-chatbot-using-bot-framework-sdk-and-luis-part-2-5bf9f72f4a6a

Part-3 — https://medium.com/@sushmita_mishra18/create-chatbot-using-bot-framework-sdk-and-luis-part-3-8b479aa16e6d

Don’t forget to give us your 👏 !


Create chatbot using bot framework sdk and LUIS — Part 4 was originally published in Chatbots Life on Medium, where people are continuing the conversation by highlighting and responding to this story.


Posted

in

by

Tags: