Top

PVA Series – real QnA chatbot with QnA maker


I was recently taking part in a Regarding365 hackathon where our goal was to build solutions for companies struggling with communications during the time of COVID-19 pandemic. Sometime after a friend of mine asked me a question about building the same solution I did during that hackathon. The solution was – QnA chatbot using QnA maker as a database. How to do it? Read on!

Basically the most important part of this post is the screencast below. Watching it you can learn how to build the solution in a step-by-step approach:

QnA approach in PVA

There are two ways you can build QnA chatbot using Power Virtual Agents. First and the simplest one is to simply create a set of topics, where a trigger phrases are related to a specific question and that has just one node inside: Message, that display an answer to that question.

Furthermore, if you already have a web page built in a way, that clearly shows a structure of questions and answers, you can use “Suggest topics” functionality:

  1. Hit the “Suggest topics” button
  2. Then input URL of the page you want to “crawl”
  3. Then “Add”
  4. And “Start” to begin gathering data.

After the processing is done, you will find all found QnA pairs under the “Suggested” section. Right form there you can choose which you want to add to the list of your existing Topics.

Second approach uses QnA maker (https://www.qnamaker.ai/) as a datasource and Power Automate as the middleware that calls it.

How to setup QnA maker with PVA

I will describe it here step-by-step. You must include 4 services.

Azure service

  1. Go to https://portal.azure.com and create new resource. You should search for “Cognitive Services” and look for “QnA Maker” in search results.
  2. Provision such a service.

QnA Maker

  1. Once Azure service is provisioned, go to https://www.qnamaker.ai/
  2. Then from the top bar: “Create a knowledge base”
  3. In the displayed form, Step 1 is what we already done in point no. 1 above, so you can proceed to step 2 ๐Ÿ™‚
  4. In step 2 select the service you provisioned.
  5. Step 3 – name your KB
  6. Step 4 – populate it. You can populate it using a URL of the site that has a clear question-answer structure or from a file.
  7. Finally step 5 – create your KB.

Once you provision new KB and review all QnA’s, click “Save and train” button and once that is done – “Publish”:

When KB is published, it will display all information related to its details. Remember host and authorization. Also, above the “host” in the POST url, you can find Knowledgebase ID. Also keep it.

Power Automate

Now go to Power Automate and create new solution.

Remember! Flows that are available to be called from Power Virtual Agents must be kept inside any solution (in the same environment) AND they must be trigger by either “Request” or “When Power Virtual Agents calls a flow”.

In the solution, create new Power Automate. Very simple one:

  1. Use “When Power Virtual Agents calls a flow” as a trigger. Set up one input variable, eg. Question.
  2. Add “Generate answer” action. Input:
    1. Knowledge Base ID – copied from published KB details
    2. Service Host – Host from published KB details
    3. Endpoint Key – Authorization from published KB details
    4. Question – input form the trigger
  3. Add “Return value(s) to Power Virtual Agents” action, to return found answer. Set up one text variable, and as its value provide: outputs('Generate_answer')?['body/answers'][0]['answer'].

Power Virtual Agents

Finally, bind all services together. In your PVA add new node, that will be “Call an action”:

From a list pick the flow you created. Now provide user’s question as an input variable and then display user an answer:

Recap

And that’s basically it! This way you can bind services together to create a real QnA chatbot.

You can go even a step beyond. As I showed in the video. So that you can not only display matching answer, but if there is no such data, to post a question to human agent, then supply QnA database with the new answer. So that the DB will actually grow as more and more questions are asked by users. Options are unlimited!

Oh, if you would like to add new question-answer pair to your database using REST API, then get familiar with this resource: https://westus.dev.cognitive.microsoft.com/docs/services/5a93fcf85b4ccd136866eb37/operations/5ac266295b4ccd1554da7600

And that’s it! I hope this post will help you to build your own chatbots. If you have any questions – post them in comments below or ask them via contact form.


Tomasz Poszytek

Hi, I am Tomasz. I am expert in the field of process automation and business solutions' building using Power Platform. I am Microsoft MVP and Nintex vTE.

9 Comments
  • naina

    hey can you please share the detailed picture of the power automate flow of sending the question to teams and using adaptive card and then saving the new pair of question answer to QnA , you can hide the important details. I am on a project on this thing it would be really helpful if you can help

    April 19, 2020 at 7:24 pm Reply
  • Simon Jones

    Hi Tomasz, your blog posts have been a great help getting more familiar with PVA. I followed your video and am adding a Flow to submit a new question to my QnA service. Everything works fine – the adaptive card takes input from the user in teams, creates a new entry in QnA under the Editorial source and the publish action returns a 204 code indicating the KB has published, however when I enter the same phrase in the chat bot the new QnA entry is not returned. If I manually publish the KB in QnA Maker it works fine, and the weird thing is the Last published date is being updated. Any ideas what the problem could be?
    Thanks,
    Simon

    May 19, 2020 at 8:21 pm Reply

Post a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.