PVA Series – real QnA chatbot with QnA maker
Table of contents:
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.
Azure service
- 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.
QnA Maker
- 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.
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:
- 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:
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.
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
Tomasz Poszytek
Hey! There’s nothing fancy in that card π Simply follow steps described in this post: https://poszytek.eu/en/microsoft-en/office-365-en/powerautomate-en/register-response-from-custom-adaptive-cards-sent-from-power-automate-to-teams/ to understand how to send Adaptive Card and then get response.
naina
thanks for the reply … i did that adaptive card implementation thing
please guide how to implement luis in powerVA ?
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
Simon Jones
I found the solution to the problem I was having publishing. You need to retrieve the operationid from the PATCH call and then call Get Operation Details with that operationid until the status is returned as Succeeded. Here are the steps for the flow:
– Add a Parse Json after the Update KB HTTP call
– Add a Do Until loop and make an HTTP call to Get Operation Details
– Add a Parse Json after Get Operation Details and put the operationState into a variable that is checked in the Do loop
– Publish after the Do loop
Works like a dream now!
Tomasz Poszytek
Ah, awesome! So that was just a matter of the asynchronous operation. I am glad you found it and shared it with us π
Tomasz Poszytek
Are you calling this endpoint: https://westus.dev.cognitive.microsoft.com/docs/services/5a93fcf85b4ccd136866eb37/operations/5ac266295b4ccd1554da75fe? After sending request to “update” (https://westus.dev.cognitive.microsoft.com/docs/services/5a93fcf85b4ccd136866eb37/operations/5ac266295b4ccd1554da7600)? In my case it just need some time to take effect.
Simon Jones
Sorry for the slow reply Tomasz, I’m calling https://myqnaname.cognitiveservices.azure.com/qnamaker/v4.0/operations/{operationid} using the operation id returned from the update, then I have a Do until loop that’s checking the OperationState until its equal to Succeeded. Typically it looks like it takes 8 calls until the operation state is succeeded. The final call is a POST to https://myqnaname.cognitiveservices.azure.com/qnamaker/v4.0/knowledgebases/{KB id}
Hope this helps.
Tomasz Poszytek
Thank you very much for sharing this! π