通过QnAMaker v4 API发布知识库

时间:2018-06-04 05:08:58

标签: c# botframework qnamaker

我试图将我的机器人从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. 再次向机器人询问土拨鼠。
  5. 到目前为止,api按预期响应,但我的机器人仍然忘记了关键的土拨鼠知识,直到我点击qnamaker.ai网站上的发布。我错过了什么?

1 个答案:

答案 0 :(得分:1)

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

enter image description here

我们可以找到operationState NotStarted ,您需要跟踪operationState并发布您的知识库,直到operationState Succeeded

您可以参考"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;
    }
}