AI Builder – Using Sentiment Analysis with Power Virtual Agent

AI Builder - Using Sentiment Analysis with Power Virtual Agent
Power Virtual Agent -> AI Builder Sentiment Analysis

The project

If you’ve been reading my posts, you may know by now, that I’m often trying to find out ways to push the limits of the Power Platform in its citizen developer statement. At the recent MS Ignite conference there’s been a few changes and features releases. 2 of those features are focused in this article: Power Virtual Agents and AI Builder prebuilt models.
I’ve been quite intrigued by the bot framework, namely Power Virtual Agents and after attending a recent user group meeting where Tiago Mendonça showed us how easy it is to get started, I went on and created my first demo bot. Quite easy indeed!!
So once I’ve confirmed that it was easy to get this started, I head back into “pushing the limits” mode… what about integrating AI Builder prebuilt model within the Bot and adapt the responses based on the sentiment analysis result? Now, that seems like a challenge for me!

The potential use cases I see for this scenario are mostly around being able to route chats more efficiently not just based on the subjects detected but also take into account the sentiment/mood the user seems to be in and escalate up to an agent faster if that seems like a more responsive approach.
In this article, I will provide the details of how I’ve been able to take this challenge and resolve it. The different components used are:

  • Power Virtual Agent: the bot that will interact with the end-user
  • Common Data Service: where the solution resides to contain the flow and any data we want to persist
  • Power Automate: the flow that will use the predict action to do the sentiment analysis
  • AI Builder: use a prebuilt model to process the message from the user

Shortcuts

Complementary to this article, you can test the bot by visiting this demo site: bit.ly/AIBuilderBot
I’ve also uploaded a walkthrough video of the PVA topics and Power Automate flows on my Youtube channel.

Step 1: Create a bot (Power Virtual Agent)

Power Virtual Agent website

First step is to connect to the Power Virtual Agent website and create a preview account: http://aka.ms/trypva

Bots

From there, I simply created a new Bot, selecting the environment that has AI Builder enabled (be sure it’s in a supported region. For more info see my reference table).

AI Builder - Using Sentiment Analysis with Power Virtual Agent
Warning message

Word of caution.. wait until the 4 steps are over before moving ahead… experienced situations where work was lost!

Default – No topic found message

For the time being, there are no options to trap and modify the “default” topic when such a topic is not found.

Authoring – Greeting topic

So to integrate the Sentiment Analysis, I’ve decided to put this in the “Greeting” topic which is the one where the conversation usually starts.

Step 2: Create a topic for the Sentiment Analysis process

AI Builder - Using Sentiment Analysis with Power Virtual Agent
Create a new topic

Before integrating the process with the “Greeting” topic, let’s start by creating a new one so we can isolate and author it by itself.

Sentiment Analysis topic creation

There’s no need to include trigger phrases as this will be triggered by another of the topic processes.

Start by “Ask a question”

The first step is to ask a question and keep track of the answer. A good practice is to name the response differently than the default name.

Call an action – Create a flow

The next step is to call an action / create a flow, this will bring up the Power Automate maker with a template for Power Automate integration.

Power Automate template

The template is a good start because it removes the requirements of building the httprequests for input and output of the flow that allow the integration with the bot.

AI Builder - Using Sentiment Analysis with Power Virtual Agent
Common Data Service

From the template, trying to add a predict action is not possible in this case. As you can see in this image, the “Common Data Service – Current Environment” is not available. That’s because the flow is not contained within a solution. Let’s save this flow and move on to the next step and resolve this!

Step 3: Create a solution in CDS

AI Builder - Using Sentiment Analysis with Power Virtual Agent
Create a CDS solution

Within the Power Automate maker interface, go in the Solution section and create a new one. Using an existing one, if applicable, can also be considered.

AI Builder - Using Sentiment Analysis with Power Virtual Agent
Add existing flow within a solution

In that solution, we import the flow that was created from the template.

AI Builder - Using Sentiment Analysis with Power Virtual Agent
Select the flow to add

The flow will now be part of the solution and we’ll be able to use the Predict action.

Step 4: Edit the flow and integrate the Predict action

AI Builder - Using Sentiment Analysis with Power Virtual Agent
Add the Predict action in the flow

Editing the flow that is in the solution, now has access to the Common Data Service – Current Environment – Predict action.

Before actually using the Sentiment Analysis model, there’s a bit of maintenance to do in the http request steps. Since the template includes a pair of string and number variables when in fact for my use case I’ll need only a string for input (the message from the user) and for output (the sentiment obtained from the model). Here’s how I’ve done it:

AI Builder - Using Sentiment Analysis with Power Virtual Agent
HTTP request received – edit Body JSON

Remove the flow_input_number from the request JSON.

AI Builder - Using Sentiment Analysis with Power Virtual Agent
HTTP request response – edit Body JSON

Remove the flow_output_number from the response JSON.

AI Builder - Using Sentiment Analysis with Power Virtual Agent
Remove the initialize variable

Since we don’t need the number variable, remove the initialize step.

Now, time to integrate the Predict action here’s how:

AI Builder - Using Sentiment Analysis with Power Virtual Agent
Predict – define the Request Payload

Select the SentimentAnalysis model, then set the Request Payload as {“text”:”@{triggerBody()?[‘flow_input_string’]}”, “language”:”en”}

AI Builder - Using Sentiment Analysis with Power Virtual Agent
Return the Sentiment Analysis result

Last modification to the flow, is to return the Sentiment Analysis result back to the bot. Doing so, by modifying the response body as { “flow_output_string”: “@{body(‘Parse_JSON’)?[‘predictionOutput’]?[‘result’]?[‘sentiment’]}” }
Time to save the flow and go back to our bot!

Step 5: Invoke the action and manage the results

Now that the flow is created, we must include it in our topic processing and provide it with the user message. Then we’ll look at the sentiment received back from the AI Builder prebuilt model invoked in the flow and based on what it is, adapt the bot’s response and following actions.

Action that triggers the Sentiment Analysis flow

As you can see in this image, we’re calling the newly created flow with the UserMessage text and receiving the sentiment in a variable flow_output_string (which I may have renamed in my flow) that we’ll use for the next conditions.

Conditions on the Sentiment Analysis result

The overall sentiment detected from the user message can be any of the following:

  • positive
  • negative
  • neutral
  • mixed

In my topic, I’ve chosen to take the following actions based on the result received:

Positive message handling

If it’s a positive message, the bot will ask if the user wants to share it (could be used as a customer reference) and then proceed to the actions based on the user’s choice. In my current demo I simply give back a message but in a real-life scenario, we could expand further, save the data into CDS, trigger a flow to post a Tweet, etc. etc.

Negative message handling

If it’s a negative message, the bot will end the conversation and transfer the chat to an agent, including the original message as information. Here again, in a real-life scenario, more questions could be asked to the user before issuing the transfer.

Neutral and other message types handling

To wrap up the conditions, here the bot provides feedback for neutral messages and any other types of messages (mixed or errors for example).

Step 6: Tie this all up together

In order to make this experience complete and allow it to be somewhat endless, we will add an intermediate action as well as trigger our AI processing topic from the greeting. 

Go to another topic

At the end of each condition where we want to come back to the beginning of the process (where the bot asks the user for a new message to inject in the sentiment analysis model), we select the Go to another topic menu option. The keyword to understand here is another!

Intermediate topic

After saving the original topic, simply go and create a new one. There’s no need to have trigger phrases or a message, just add an action to Go to another topic which points back to the AI Builder topic that was saved in the previous step. 
Save it and go back to the other one!

Add the intermediate topic to the main one

At each end of condition handling where we want to loop back, add the action to go the intermediate topic.

All loops included

In our scenario, I loop back in all situations, but the one where we transfer to an agent.

Edit greeting topic

The last action to take is to go edit the greeting topic and have it redirect to our AI Builder topic at the end. I’ve also removed one of the redundant messages from this flow.

Final step: Test, publish and embed in a webpage

Now that we have the bot which will handle the user messages and process them via the Sentiment Analysis model, let’s publish it and embed it on a webpage so we can have people (like you) test it out!

Test and publish

In the Power Virtual Agent interface, you can test directly your bot and confirm that it reacts the way you want. Once satisfied with the results, simply publish it to have it available on different channels.

Demo website

In the channels section, you get all the facilities and information to embed the bot. For this post, I’ve simply used the Demo website, which you can view and test here: http://bit.ly/AIBuilderBot

Have fun! I will try to post a summary video soon 😉

Summary

Having read this article, you should now know how to integrate an AI Builder prebuilt model within a Power VirtualAgent bot, all this with the following components:

  • Power Virtual Agent: the bot that interacts with the end-user
  • Common Data Service: where the solution resides to contain the flow and any data we want to persist
  • Power Automate: the flow that uses the predict action to do the sentiment analysis
  • AI Builder: take advantage of a prebuilt model to process the message from the user

The potential use cases I see for this scenario are mostly around being able to route chats more efficiently not just based on the subjects detected but also take into account the sentiment/mood the user seems to be in and escalate up to an agent faster if that seems like a more responsive approach.

Learn and explore

The Power Platform really allows business users to resolve business issues quickly while ensuring organizations that the safeguards can be put in place to protect their data and reputation. Make sure to learn and explore the capabilities first in a development/community environment. I hope this article has been useful for you, don’t hesitate to share and comment back.

About me

I’m Éric Sauvé, aka ZePowerDiver, solution architect and team lead at XRM Vision. I’m a community leader for TDGi‘s global hackathons in Canada. Power Platform enthusiast, delivering App in a Day sessions, leading local community events such as Montreal Power Platform Happy Hour, actively participating in Montreal Business Application User Group and speaking in Power Platform and Dynamics CE related conferences.

You can reach me on:

LinkedIn: https://www.linkedin.com/in/zepowerdiver
Twitter: https://twitter.com/ZePowerDiver
Facebook: https://www.facebook.com/ZePowerDiver/
YouTube: https://www.youtube.com/c/ZePowerDiver
Blogger: https://www.zepowerdiver.com/

Reference:

Sauvé, É (2019). AI Builder – Using Sentiment Analysis with Power Virtual Agent. Available at: http://www.zepowerdiver.com/2019/12/ai-builder-using-sentiment-analysis.html [Accessed: 1st April 2020].

Find more great Power Platform content here.

Share this on...

Rate this Post:

Share: