1

I am using QnA Maker v4.0 and I am trying to update my knowledge base programmatically using C# and following the documentation provided in:

https://westus.dev.cognitive.microsoft.com/docs/services/5a93fcf85b4ccd136866eb37/operations/5ac266295b4ccd1554da7600

I am able to add new questions and answers pairs to my knowledge base, but when I try to delete some of them, I am not able to do it. I get no message error, it's just that if I enter in QnAMaker portal, I can still see the question I am trying to delete is there.

I have tried to delete a complete source and that's working well so I am a bit lost on how can I delete just a given pair of questions and answer instead of the whole source.

What I have tried so far is following the documentation as follows:

Add new question an answer pair

string json = "{\"add\":{\"qnaList\":[{\"id\":123456789,\"answer\":\"fooanswer\",\"source\":\"Editorial\",\"questions\":[\"fooquestion\"],\"metadata\":null},{\"id\":987654321,\"answer\":\"fooanswer2\",\"source\":\"Editorial\",\"questions\":[\"fooquestion2\"],\"metadata\":null}],\"urls\":null,\"files\":null},\"delete\":null,\"update\":null}"

public async void UpdateKnowledgeBase(string json)
    {
    var knowledgebaseid = "<my kb id>";
    var client = new HttpClient();
    var querystring = HttpUtility.ParseQueryString(string.Empty);
    // Request headers
    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "QnA Subscription Key>");
    var uri = "https://westus.api.cognitive.microsoft.com/qnamaker/v4.0/knowledgebases/"+ this.KnowledgeBaseId + "?" + querystring; 

    // Request body
    byte[] byteData = Encoding.UTF8.GetBytes(json);
    using (var content = new ByteArrayContent(byteData))
        {
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var method = new HttpMethod("PATCH");
            var request = new HttpRequestMessage(method, uri)
                {
                    Content = content
                };
                HttpResponseMessage response;
                response = await client.SendAsync(request);
        }
    }

This will create the question and answer pair in my knowledge base: Question and answers pair created programmatically in the KB

Deleting question and answer pair

However, if I try now to follow the documentation and delete the question and answer pair with id 123456789, nothing will happen. To do so I am doing:

json = "{\"add\":null,\"delete\":{\"ids\":[123456789],\"sources\":null},\"update\":null}"

And, after calling the UpdateKnowledgeBase method with this json, I will get no error but my question and answer pair still appears in the QnA Maker portal. If I add the source, it will delete all the Editorial source. That's ok, but I also want to delete just one pair and not all of them. Is that possible?

Updating an existing question and answer pair

When updating an existing question and answer pair, I have the same problem, I am not getting any kind of mistake but I can't see the changes in the QnA Maker portal, what makes me think that no changes are being done.

My question therefore is, how can I do to update and/or delete questions and answer pairs without removing all the source?

Thank you in advance!

Marisa
  • 1,135
  • 3
  • 20
  • 33

2 Answers2

3

Deleting question and answer pair

I tested your case with the same values for creation then deleting.

The problem in your case is that the id value you provided during the add phase is changed when the item is added: even if I put "id": 123456789, the created item has an id value which is the increment of the current id existing in my knowledge base.

Then when I want to delete, if I specify the right id, the delete operation is successful.

To ensure that you have the same problem, check the id of the added item by downloading the KB (GET to https://westus.api.cognitive.microsoft.com/qnamaker/v4.0/knowledgebases/:kbId/:environment/qna, where :kbId is your knowledge base id and :environment is Test or Prod).


Updating an existing question and answer pair

I guess it is the same here: you may be trying to update an id which is in fact not the right one.

Nicolas R
  • 13,812
  • 2
  • 28
  • 57
  • Thank you. I was confused thinking that when adding a new pair I could decide the id. This fixed my problem. However, I have a new question: how can I get the id of the pair I've just added? Because if I want to delete it, I will need to know the id to do it. I know that there is a function that gives me the id of all the pairs in my KB, but is there any way to obtain it as soon as I create it? – Marisa Jan 04 '19 at 11:51
  • I can't find a way to get those id no: you have to ensure that the Patch operation is successful (by checking the status of the operation using the operationId you got on the response of the patch call), then you have to download the KB. By the way, there is a Nuget package that you can use instead of calling directly the API: https://www.nuget.org/packages/Microsoft.Azure.CognitiveServices.Knowledge.QnAMaker/ – Nicolas R Jan 04 '19 at 13:09
0

To delete required question, you can add 'source' and 'metadata' with each question(s) answer pair while adding the question in knowledge base. Then you will have to make an API call to download the knowledgebase, once you get the response, parse the response json using the value of source and key (that you defined in metadata), in this case you can extract question id and delete that.

Muhammad Murad Haider
  • 1,357
  • 2
  • 18
  • 34