QnA Maker API - How to programatically delete structured QnA files?

Copper Contributor

Hello all,

 

I am working on a programmatic solution to feed QnAs into QnA Maker. In order to do so, I use the API to provide structured data, as outlined here

 

https://docs.microsoft.com/en-us/azure/cognitive-services/qnamaker/concepts/data-sources-supported#s...

 

The reason is, I cannot use files nor urls and thus leave these fields blank when submitting my data. The input I being used looks like

{"id":<Integer>,
"answer":"<Textual Content>",
"questions":["<Textual Content>"],
"metadata":[]}

As it turns out, I cannot provide any URLs, as the repositories are on premise and cannot be accessed by QnA Maker.

 

Now, my observation is that I cannot programatically delete my documents using the "id" which I provided in the initial submission. Actually, it appears that QnA Maker replaces my "id" by some internal counting which starts from 0 for fresh KBs. Thus, it turns out that I can delete my Q&As if I just guess these ids.

 

My questions therefore are:

  1. Is the above observation correct? A hint pointing at the documentation on how these ids which are needed for deleting would be helpful.
  2. If yes, are there any thoughts of extending the behavior toward a fallback approach, i.e., only if no "id" is given, this counting starting from 0 is used?
  3. In the meantime, is there a way to programtically retrieve this internal "id" from QnA Maker, ideally as part of the success response?

Thank you!

 

6 Replies

Hi Christian,

As far as my understanding goes, you could use the any resource/URLs and the QnAMaker would extract and create KB.

 

Also, I just did a quick testing with your scenario and I am able to delete a particular document from the QnAMaker KB with the ID provided during the initial submission.

 

You can refer to Update Knowledge API : https://westus.dev.cognitive.microsoft.com/docs/services/5a93fcf85b4ccd136866eb37/operations/5ac2662...

 

Firstly, I used the below body to create 2 documents in the KB.

{

  "add": {

    "qnaList": [

      {

        "id": 3001,

        "answer": "You can change the default message if you use the QnAMakerDialog. See this for details: https://docs.botframework.com/en-us/azure-bot-service/templates/qnamaker/#navtitle",

        "source": "Custom Editorial",

        "questions": [

          "How can I change the default message from QnA Maker?"

        ],

        "metadata": []

      },

      {

        "id": 3002,

        "answer": "You can use our REST apis to manage your KB. See here for details: https://westus.dev.cognitive.microsoft.com/docs/services/58994a073d9e04097c7ba6fe/operations/58994a0...",

        "source": "Custom Editorial",

        "questions": [

          "How do I programmatically update my KB?"

        ]

      }

    ]

  }

}

 

I verified that it created using the "TEST" button in the QnAMaker website.

 

Next, I used the below body to delete just 1 document.

{

 "delete": {

    "ids": [

      3001

    ],

    "sources": [

      "Custom Editorial"

    ]

  }

}

 

I verified that it got deleted by using the "TEST" button again and the document is not found.

Hope it helps!

 

Dhina

 

Hi Dhina,

 

I tried to reproduce your suggestion. I submitted your JSON but the Ids got ignored again.

What I did is I used the patch snippet from 

https://docs.microsoft.com/en-us/azure/cognitive-services/qnamaker/quickstarts/java#Update

to submit your JSON. If I download the knowledge base, I receive the following

 

"qnaDocuments": [   

{      "id": 188,      "answer": "You can change the default message if you use the QnAMakerDialog. See this for details: https://docs.botframework.com/en-us/azure-bot-service/templates/qnamaker/#navtitle",     

"source": "Custom Editorial",     

"questions": [        "How can I change the default message from QnA Maker?"      ],     

"metadata": [],     

"alternateQuestionClusters": [],     

"changeStatus": "Update",     

"kbId": "..."   

},   

{     

"id": 189,     

"answer": "You can use our REST apis to manage your KB. See here for details: https://westus.dev.cognitive.microsoft.com/docs/services/58994a073d9e04097c7ba6fe/operations/58994a0...

"source": "Custom Editorial",     

"questions": [        "How do I programmatically update my KB?"      ],     

"metadata": [],     

"alternateQuestionClusters": [],     

"changeStatus": "Update",     

"kbId": "..."    } 

]

}

 

As you see, the ids 188 and 189 rather than 3001 and 3002.

 

Is there anything obvious what I can do wrong at client side to have the Ids not be used within the QnA Maker?

 

Best regards

Christian

 

Interesting. But, after you had submitted the JSON with 3001 and 3002, did you try deleting by passing either 3001 or 3002 as parameters? I am asking because, in my case, when I used these ids in my delete payload, the article got deleted (which I confirmed using the TEST button). That indicates to me that the IDs are intact.

Hi Dhina,

 

the thing is that

{

"delete": {

    "sources": [

      "Custom Editorial"

    ]

  }

}

in your above JSON empties the entire source. Thus, not only document 3001 is deleted but in my tests also document 3002.

 

Thank you and best regards

Christian

 

Hi Christian,

 

The JSON payload that I provided in my earlier post had the "ID" as well in it, but yours did not have it, so  I guess it ended up deleting all.

 

Here is the JSON payload once again for your reference:

{

 "delete": {

    "ids": [

      3001

    ],

    "sources": [

      "Custom Editorial"

    ]

  }

}

Yes internal id's get created for the questions that a created programatically using API. what i can think of is to specify some metadata (key-value pair) that you can use further to identify the question that you just have created in the KB. you may need to follow several step for deletion. First, you can get the KB JSON using an API Call, Next you need to parse and filter the response JSON. This may include several steps i.e. first you need to extract 'qnaDocuments' array (array that contains all the questions from KB) from JSON, then you need to filter 'qnaDocuments' array by the 'Source' that you had specified while creating the questions. From here you can get the id's of all questions pertaining to that particular source. Next you need to further filter this by metadata that  you need to provide to uniquely identify each questions that you create through API which can further provide you the internal id of the question that you need to delete. Rest is simple API call to delete the question using the returned id.
hope this will help  @dhinagaja