Can't Upload a File to QnaMaker Knowledge Base using SDK

Viewed 381

I currently have an Azure Function that I would like to have update a QnaMaker Knowledge Base every day or so. Currently everything is connected and working fine, however I can only send Qna Objects (qna pairs) and not urls to files on a website of mine. So in the example I provided below, while it should populate the KB with 2 questions and the file from the url, it only populates the questions.

Currently this is not giving me any kind of error, in fact the response code from my call to the KB comes back as 204. So it it getting through, but still not adding the file to the KB as it should.

NOTE: The file being imported in this example (alice-I.html) is a random one for this demonstration (not mine, for security), but the issue is the same. If I directly add this file to the QnaMaker from the KB site itself it works fine, but it won't update from the Azure Function Code.

Any insights into what is happening would be great.

Content Being Sent To Knowledge Base

string replace_kb = @"{
  'qnaList': [
    {
      'id': 0,
      'answer': 'A-1',
      'source': 'Custom Editorial',
      'questions': [
        'Q-1'
      ],
      'metadata': []
    },
    {
      'id': 1,
      'answer': 'A-2',
      'source': 'Custom Editorial',
      'questions': [
        'Q-2'
      ],
      'metadata': [
        {
          'name': 'category',
          'value': 'api'
        }
      ]
    }
  ],
  'files': [
      {
        'fileName': 'alice-I.html',
        'fileUri': 'https://www.cs.cmu.edu/~rgs/alice-I.html'
      }
  ]
}";

Code Sending Content To Knowledge Base

using (var clientF = new HttpClient())
            using (var requestF = new HttpRequestMessage())
            {
                requestF.Method = HttpMethod.Put;
                requestF.RequestUri = new Uri(<your-uri>);
                requestF.Content = new StringContent(replace_kb, Encoding.UTF8, "application/json");
                requestF.Headers.Add("Ocp-Apim-Subscription-Key", <your-key>);

                var responseF = await clientF.SendAsync(requestF);
                if (responseF.IsSuccessStatusCode)
                {
                    log.LogInformation("{'result' : 'Success.'}");
                     log.LogInformation($"------------>{responseF}");
                }
                else
                {
                    await responseF.Content.ReadAsStringAsync();
                    log.LogInformation($"------------>{responseF}");
                }
            }
1 Answers

So I still don't know how to get the above working, but I got it to work a different way. Basically I used the UpdateKbOperationDTO Class listed here: class

This still isn't the perfect solution, but it allows me to update my KB with files using code instead of the interface.

Below is my new code:

QnAMakerClient qnaC = new QnAMakerClient(new ApiKeyServiceClientCredentials(<subscription-key>)) { Endpoint = "https://<your-custom-domain>.cognitiveservices.azure.com"};
log.LogInformation("Delete-->Start");
List<string> toDelete = new List<string>();
toDelete.Add("<my-file>");
var updateDelete = await qnaC.Knowledgebase.UpdateAsync(kbId, new UpdateKbOperationDTO
    {
        // Create JSON of changes ///
        Add = null, 
        Update = null, 
        Delete = new UpdateKbOperationDTODelete(null, toDelete)
    });
log.LogInformation("Delete-->Done"); 


log.LogInformation("Add-->Start");
List<FileDTO> toAdd = new List<FileDTO>();
toAdd.Add(new FileDTO("<my-file>", "<url-to-file>"));
var updateAdd = await qnaC.Knowledgebase.UpdateAsync(kbId, new UpdateKbOperationDTO
    {
        // Create JSON of changes ///
        Add = new UpdateKbOperationDTOAdd(null, null, toAdd),
        Update = null,
        Delete = null
    });
log.LogInformation("Add-->Done"); 
Related