0

我正在尝试将我的机器人从 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 网站上的发布。我错过了什么?

4

1 回答 1

1

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

在此处输入图像描述

我们可以找到operationStateis NotStarted,您需要跟踪并发布您的operationState知识库,直到出现。operationStateSucceeded

您可以参考“更新知识库”来更新您现有的知识库并跟踪operationState基于operationId

“更新知识库”中的代码片段:

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;
    }
}
于 2018-06-05T07:28:58.393 回答