通过 QnAMaker v4 发布知识库 API

Publishing a knowledgebase via QnAMaker v4 API

我正在尝试将我的机器人从 QnAMaker v2 API 迁移到 QnAMaker v4 API。我能够将更新发送到知识库,但似乎没有发布。这是我正在使用的代码。

    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();

    }

我的测试流程是

  1. 向机器人询问土拨鼠。
  2. 运行 这个代码。
  3. 验证关于土拨鼠的 q/a 对是否在知识库中。
  4. 再次向机器人询问土拨鼠。

到目前为止,api 的响应符合预期,但在我点击 qnamaker.ai 网站上的“发布”之前,我的机器人仍然对重要的土拨鼠知识一无所知。我错过了什么?

根据您的代码,您将第一个请求发送到 update Knowledgebase,将执行异步操作,并将如下消息写入您的控制台 window。

我们可以发现operationStateNotStarted,需要追踪operationState 并发布您的知识库,直到 operationStateSucceeded.

您可以参考"Update knowledge base"更新您现有的知识库并根据operationId追踪operationState .

来自 "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;
    }
}