cancel
Showing results for 
Search instead for 
Did you mean: 
fchopo

How to create a PVA bot using Custom Question Answering

Microsoft recently released the Question Answering in Public Preview which allows to add unstructured documents to a knowledge base and extract relevant answers to questions. This feature could save a lot of effort in creating a PVA bot, since we don't need to enter topics manually, and we will be able to use it to extract the information based on users' questions.

For instance, imagine you want to create a PVA bot to answer questions about new devices and accessories from Microsoft, but you do not want to enter all the information manually. We could create a knowledge base with some unstructured content (like PDF documents) and let the Question Answering Service to manage all that information, so it is able to handle questions and return answers. 

Let's go step by step on how to achieve this.

 

Create QnA and Text Analytics services

First of all, we need to create a new QnA service using the "Preview" to try Custom Question & Answering:

 

01-CreateQnA.png

 

When clicking on "Create a QnA Service" we will need to select an Azure account (we highly recommend you create a new one if you don't have it) and then select the "Custom question answering (preview)" feature:

 

02-SelectQAnswering.png

 

Finally, we will create a Text Analytics resource with Custom Question Answering feature:

03-CreateTextAnalytics.png

 

 The service will be deployed in our blade within minutes.

 

Create a knowledge base and connect it to the QnA service

After creating the QnS service, we can go to QnA Maker and then create a new knowledge base:

04-CreateKB.png

 

In the last step we are going to populate this knowledge base with some PDF documents. For instance, we will ingest the following unstructured document introducing Surface Products: Introducing surface laptop 4 and new access Remember to check the "unstructured content" checkbox!

05-UnustructuredDocument.png

 

Finally we are going to save changes clicking on "Create your KB". 

Once the knowledge base is created, we can test the service and check if we get some answers depending on our questions. For instance, we can ask the system for the "headset price", and according to the information found on the PDF document, we get a short answer and the text passage is based on.

06-TestQnA.png

 

If we think that the knowledge base is complete, we can "publish" it, so it can be accessed by other services or applications, like a PVA bot. When we publish the Knowledge Base, some important information is provided to access it and that we will need in next steps:

10-PublishKB.png

 

Whenever we want to make an HTTP call to this service, we will need those parameters.

In conclusion, we created a QnA service with a Knowledge Base, which is using Text Analytics, that will be able to get some answers to users' questions based on the information found in a PDF document. In order to use this service, we will need to make an HTTP call with some parameters, like the URL, the subscription key and the question.

 

Create a new PVA bot

What about creating a Power Virtual Agent that is able to submit the questions to the QnA service created previously? Instead of creating topics manually, which can be a hard and tedious task, we are going to get answers based on PDF documents which contain all the needed information!

We are going to create a new PVA bot in any of our environments (default if we only have one):

07-CreateNewBot.png

 

Whatever the users' questions are, we want them to be redirected to the QnA service instead of using the custom topics in our bot. In order to do that, we must add a fallback topic to our bot. What is this about? If the bot can not determine the users' intent, it will redirect that question to the fallback topic. In order to configure it, we have to go to Settings > System fallback:

08-Fallback.png

 

Once the fallback topic is added, we are going to edit it. We need to call an action (a Power Automate flow), which will make the call to the QnA service to get an answer based on users' question:

09-Call-PA-Flow.png

 

This will open a new window with a Power Automate flow based on a Power Virtual Agents Template. We are going to update this flow (and rename it if we want).

 

Create a Power Automate flow to query the Custom Answering Service

We are going to update the flow with the following steps:

  1. Add an input in the trigger, which will be the user's question.
  2. Make an HTTP call to the QnA service to get an answer based on the user's question. We will need the parameters that were shown when we published the Knowledge Base.
  3. Check if the call has been successful (status code=200) or not.
  4. If it has been successful:
    1. Parse the response of the HTTP call and capture the answer value.
    2. As there could be multiple answers, we are going to get the first one (which is supposed to be the most relevant).
    3. Return the answer (if any) to the Power Virtual Agent bot.
  5. If not, return an error message.

The first 2 steps should look like the following:

11-Flow01.png

 

In order to make the HTTP call (POST) we must use the following URL:  

 

 

https://qansweringservice.cognitiveservices.azure.com/qnamaker/v5.0-preview.2/knowledgebases/<your_knoweldgebase_id>/generateAnswer

 

 

 

Moreover, we just need to provide the Ocp-Apim-Subscription-Key to get authorization to perform the query to the service. Finally, we embed in the body of the query the users' question and set the parameter "includeUnstructuredContent" to "true", so it will query all unstructured content attached to the knowledge base.

 

 

{
"question": "@{triggerBody()['text']}",
"includeUnstructuredSources":true
}

 

 

 

The second part of the flow (following the first one) should look like this:

11-Flow02.png

 

 In order to parse the output of the HTTP call, we need to provide a valid schema, which can be generated from a sample output (clicking on "generate from sample"). You can find the schema used in this sample in the attachment section.

As the HTTP call may return more than one answer, we are using a "compose" action to get the first one, and access to the "answer" field using the following expression:

 

 

first(body('Parse_JSON')?['answers'])?['answer']

 

 

 

Finally, we are going to return the answer value to the PVA bot.

 

Call the Power Automate flow from the PVA bot

At this point we can come back to the PVA bot designer and update the fallback topic, calling the flow we just created (we called it Microsoft Devices QuestionAnswering) in the previous step, capturing the answer parameter that it returns and showing it to the user with the message action (we don't need the escalate action anymore):

12-Fallback.png

 

As you can see, we are passing the UnrecognizedTriggerPhrase value as the action's (flow) input value and the output value (answer) is showed in a message.

Finally, we only need to save the changes made to the fallback topic.

 

Testing the PVA bot

At this point, we can test the PVA bot, making questions about Microsoft devices and getting responses based on the information stored in a PDF document:

13-surface-models.png

 

Final steps and conclusions

We created a PVA bot that is able to manage questions about Microsoft devices without the need to add any kind of information manually. Instead of this, we created a QnA service based on Text Analytics (part of Azure Cognitive Services) that is able to process unstructured content and return responses depending on users' questions. Therefore, we could add more documents to the knowledge base, and create a powerful PVA bot within minutes.

The Custom Question Answering service is with no doubt a powerful tool that will reduce time and complexity in creating PVA bots.

 

Comments