我试图将我的机器人从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();
}
我的测试过程是
到目前为止,api按预期响应,但我的机器人仍然忘记了关键的土拨鼠知识,直到我点击qnamaker.ai网站上的发布。我错过了什么?
答案 0 :(得分:1)
根据您的代码,您将第一个请求发送到update Knowledgebase,将执行异步操作,并将下面的消息写入您的控制台窗口。
我们可以找到operationState
NotStarted
,您需要跟踪operationState
并发布您的知识库,直到operationState
Succeeded
。
您可以参考"Update knowledge base"更新现有知识库,并根据 operationId 跟踪operationState
。
代码段:
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;
}
}