The interaction between users and bots is mostly free-form, so bots need to understand language naturally and contextually. In this exercise you will learn how to detect the user's sentiments and mood using the Azure Text Analytics API.
With Text Analytics APIs, part of the Azure Cognitive Services offering, you can detect sentiment, key phrases, topics, and language from your text. The API returns a numeric score between 0 and 1. Scores close to 1 indicate positive sentiment and scores close to 0 indicate negative sentiment. Sentiment score is generated using classification techniques.
Inside this folder you will find a solution with the code that results from completing the steps in this exercise. You can use this solutions as guidance if you need additional help as you work through this exercise. Remember that for using it, you first need to run npm install
and complete the value of the Text Analytics key in the .env
file.
The following software is required for completing this exercise:
- Latest Node.js with NPM
- A code editor like Visual Studio Code (preferred), or Visual Studio 2017 Community or higher
- An Azure subscription
- The Bot Framework Emulator (make sure it's configured with the
en-US
Locale)
In this task you will create a Text Analytics Account.
-
Browse here, select the Language tab. Find the Text Analytics API and click Create. You will be prompted to agree the terms of use and choose your country, next click Next.
-
Log in with your Azure Subscription account. You should be taken to a page like the following one with an evaluation key with 5000 free requests per month. Save one of the keys for later.
In this task you will create a new module to call the Text Analytics API from the bot.
-
Open the app you've obtained from the previous exercise. Alternatively, you can use the app from the exercise4-LuisDialog folder.
NOTE: If you use the solution provided edit the
.env
file and replace:- the LUIS_MODEL_URL key with your model URL (as explained in exercise 3)
- the AZURE_SEARCH_INDEX and AZURE_SEARCH_KEY with your search index name and key (as explained in exercise 4)
-
Create a new file named
textAnalyticsApiClient.js
and add the following code to it.NOTE: Notice that the client is hitting the
/sentiment
endpoint. The Text Analytics API also provides the/keyPhrases
and/languages
endpoints. Also notice that you can send more than one document to analyze.const restify = require('restify'); module.exports = (config) => { return (query, callback) => { const client = restify.createJsonClient({ url: `https://westus.api.cognitive.microsoft.com`, headers: { 'Ocp-Apim-Subscription-Key': config.apiKey } }); const payload = { documents: [{ language: 'en', id: 'singleId', text: query }] }; const urlPath = '/text/analytics/v2.0/sentiment'; client.post(urlPath, payload, (err, request, response, result) => { if (!err && response && response.statusCode == 200 && result.documents[0]) { callback(null, result.documents[0].score); } else { callback(err, null); } }); }; };
In this task you will introduce the new Text Analytics module and then consume it from a new dialog on your bot.
-
Update the
.env
file adding the following line, replace the TEXT_ANALYTICS_KEY key with the Text Analytics Key you have obtained in Task 1.TEXT_ANALYTICS_KEY=
-
Open the app.js file.
-
Add the following code.
const textAnalytics = require('./textAnalyticsApiClient');
-
Add the following code.
const analyzeText = textAnalytics({ apiKey: process.env.TEXT_ANALYTICS_KEY });
-
At the end of the file, add the following code that creates a new dialog to ask the user for feedback and call the Text Analytics API client to evaluate the user sentiments. Depending on the response (greater or lower than 0.5) a different message is displayed to the user.
bot.dialog('UserFeedbackRequest', [ (session, args) => { builder.Prompts.text(session, 'Can you please give me feedback about this experience?'); }, (session, response) => { const answer = session.message.text; analyzeText(answer, (err, score) => { if (err) { session.endDialog('Ooops! Something went wrong while analzying your answer. An IT representative agent will get in touch with you to follow up soon.'); } else { var msg = new builder.Message(session); var cardImageUrl, cardText; // 1 - positive feeling / 0 - negative feeling if (score < 0.5) { cardText = 'I understand that you might be dissatisfied with my assistance. An IT representative will get in touch with you soon to help you.'; cardImageUrl = 'https://raw.githubusercontent.com/GeekTrainer/help-desk-bot-lab/master/assets/botimages/head-sad-small.png'; } else { cardText = 'Thanks for sharing your experience.'; cardImageUrl = 'https://raw.githubusercontent.com/GeekTrainer/help-desk-bot-lab/master/assets/botimages/head-smiling-small.png'; } msg.addAttachment( new builder.HeroCard(session) .text(cardText) .images([builder.CardImage.create(session, cardImageUrl)]) ); session.endDialog(msg); } }); } ]);
NOTE: For sentiment analysis, it's recommended that you split text into sentences. This generally leads to higher precision in sentiment predictions.
-
Update the last waterfall step for the SubmitTicket dialog. Replace the
session.endDialog();
in the following code:(session, result, next) => { if (result.response) { ... client.post('/api/tickets', data, (err, request, response, ticketId) => { ... session.endDialog(); }); } else { ... } }
with the this code, which calls the
UserFeedbackRequest
dialog.session.replaceDialog('UserFeedbackRequest');
-
Run the app from a console (
nodemon app.js
) and open the emulator. Type the bot URL as usual (http://localhost:3978/api/messages
). -
Type
I need to reset my password
and next choose a severity. Confirm the ticket submission, and check the new request for feedback. -
Type
It was very useful and quick
. You should see the following response, which means it was a positive feedback. -
Repeat the ticket submission and when the bot asks for feedback, type
it was useless and time wasting
. You should see a response as follows, which means it was a a negative experience.In the next exercise (7) you will learn how to hand-off the conversation to a human so he can assist the user.
If you want to continue working on your own you can try with these tasks:
- You can add Speech Recognition to the bot by using another Microsoft Cognitive Services. You can try the Bing Speech API.