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:
- Hit the “Suggest topics” button
- Then input URL of the page you want to “crawl”
- Then “Add”
- 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.
- Go to https://portal.azure.com and create new resource. You should search for “Cognitive Services” and look for “QnA Maker” in search results.
- Provision such a service.
- Once Azure service is provisioned, go to https://www.qnamaker.ai/
- Then from the top bar: “Create a knowledge base”
- In the displayed form, Step 1 is what we already done in point no. 1 above, so you can proceed to step 2 🙂
- In step 2 select the service you provisioned.
- Step 3 – name your KB
- 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.
- 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.
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:
- Use “When Power Virtual Agents calls a flow” as a trigger. Set up one input variable, eg. Question.
- Add “Generate answer” action. Input:
- Knowledge Base ID – copied from published KB details
- Service Host – Host from published KB details
- Endpoint Key – Authorization from published KB details
- Question – input form the trigger
- Add “Return value(s) to Power Virtual Agents” action, to return found answer. Set up one text variable, and as its value provide:
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:
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.