0

I'm attempting to migrate my bot from QnAMaker v2 API to QnAMaker v4 API. I am able to send updates to the knowledge base, but the publish doesn't seem to take. Here's the code I'm using.

    static void Main(string[] args)
    {
        MainAsync(args).Wait();
    }

    static async Task MainAsync(string[] args)
    {
        Console.WriteLine("We're starting.");
        var client = new HttpClient();
        client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", mySubKey);
        var uri = new Uri($"https://westus.api.cognitive.microsoft.com/qnamaker/v4.0/knowledgebases/{myKBId}");
        var payload = "{\"add\": {\"qnaList\": [{\"id\": 0,\"answer\": \"A woodchuck could chuck all the wood he could chuck if a woodchuck could chuck wood.\",\"source\": \"Custom Editorial\",\"questions\": [\"How much wood could a woodchuck chuck if a woodchuck could chuck wood?\"],\"metadata\": []}]},\"delete\": {},\"update\": {}}";
        var method = new HttpMethod("PATCH");
        var request = new HttpRequestMessage(method, uri);
        request.Content = new StringContent(payload, Encoding.UTF8, "application/json");
        var response = await client.SendAsync(request);
        var responseMessage = await response.Content.ReadAsStringAsync();
        Console.Write(responseMessage);
        Console.ReadLine();
        method = new HttpMethod("POST");
        payload = "";
        request = new HttpRequestMessage(method, uri);
        request.Content = new StringContent(payload, Encoding.UTF8, "application/json");
        response = await client.SendAsync(request);
        responseMessage = await response.Content.ReadAsStringAsync();
        Console.WriteLine(responseMessage);
        Console.ReadLine();

    }

My test process is

  1. Ask the bot about woodchucks.
  2. Run this code.
  3. Verify the q/a pair about woodchucks is in the knowledge base.
  4. Ask the bot about woodchucks again.

So far the api responds as expected, but my bot remains oblivious to critical woodchuck knowledge until I click publish on the qnamaker.ai site. What am I missing?

Nicolas R
  • 13,812
  • 2
  • 28
  • 57
Billdr
  • 1,557
  • 2
  • 17
  • 30

1 Answers1

1

Based on your code, you send the first request to update Knowledgebase, an asynchronous operation will be executed, and the message like below would be written to your Console window.

enter image description here

We can find the operationState is NotStarted, you need to trace the operationState and publish your Knowledgebase until the the operationState is Succeeded.

You can refer to "Update knowledge base" to update your existing Knowledgebase and trace the operationState based on operationId.

Code snippet from "Update knowledge base":

var done = false;
while (true != done)
{
    response = await GetStatus(operation);
    Console.WriteLine(PrettyPrint(response.response));

    var fields = JsonConvert.DeserializeObject<Dictionary<string, string>>(response.response);

    String state = fields["operationState"];
    if (state.CompareTo("Running") == 0 || state.CompareTo("NotStarted") == 0)
    {
        var wait = response.headers.GetValues("Retry-After").First();
        Console.WriteLine("Waiting " + wait + " seconds...");
        Thread.Sleep(Int32.Parse(wait) * 1000);
    }
    else
    {
        Console.WriteLine("Press any key to continue.");
        done = true;
    }
}
Fei Han
  • 26,415
  • 1
  • 30
  • 41