无法使用 SDK 将文件上传到 QnaMaker 知识库
Can't Upload a File to QnaMaker Knowledge Base using SDK
我目前有一个 Azure Function,我希望每天左右更新 QnaMaker 知识库。目前一切都已连接并且工作正常,但是我只能将 Qna 对象(qna 对)而不是 urls 发送到我网站上的文件。因此,在我下面提供的示例中,虽然它应该用 2 个问题填充知识库和 url 中的文件,但它只填充问题。
目前这没有给我任何类型的错误,实际上我调用 KB 的响应代码返回为 204。所以它通过了,但仍然没有按预期将文件添加到 KB .
注意:本例中导入的文件 (alice-I.html) 是本次演示的随机文件(不是我的,为了安全),但问题是一样的。如果我直接从 KB 站点本身将此文件添加到 QnaMaker,它工作正常,但它不会从 Azure 功能代码更新。
如果能深入了解正在发生的事情,那就太好了。
内容正在发送到知识库
string replace_kb = @"{
'qnaList': [
{
'id': 0,
'answer': 'A-1',
'source': 'Custom Editorial',
'questions': [
'Q-1'
],
'metadata': []
},
{
'id': 1,
'answer': 'A-2',
'source': 'Custom Editorial',
'questions': [
'Q-2'
],
'metadata': [
{
'name': 'category',
'value': 'api'
}
]
}
],
'files': [
{
'fileName': 'alice-I.html',
'fileUri': 'https://www.cs.cmu.edu/~rgs/alice-I.html'
}
]
}";
向知识库发送内容的代码
using (var clientF = new HttpClient())
using (var requestF = new HttpRequestMessage())
{
requestF.Method = HttpMethod.Put;
requestF.RequestUri = new Uri(<your-uri>);
requestF.Content = new StringContent(replace_kb, Encoding.UTF8, "application/json");
requestF.Headers.Add("Ocp-Apim-Subscription-Key", <your-key>);
var responseF = await clientF.SendAsync(requestF);
if (responseF.IsSuccessStatusCode)
{
log.LogInformation("{'result' : 'Success.'}");
log.LogInformation($"------------>{responseF}");
}
else
{
await responseF.Content.ReadAsStringAsync();
log.LogInformation($"------------>{responseF}");
}
}
所以我仍然不知道如何让上面的工作正常,但我让它以不同的方式工作。基本上我使用了此处列出的 UpdateKbOperationDTO Class:class
这仍然不是完美的解决方案,但它允许我使用代码而不是界面使用文件更新我的知识库。
下面是我的新代码:
QnAMakerClient qnaC = new QnAMakerClient(new ApiKeyServiceClientCredentials(<subscription-key>)) { Endpoint = "https://<your-custom-domain>.cognitiveservices.azure.com"};
log.LogInformation("Delete-->Start");
List<string> toDelete = new List<string>();
toDelete.Add("<my-file>");
var updateDelete = await qnaC.Knowledgebase.UpdateAsync(kbId, new UpdateKbOperationDTO
{
// Create JSON of changes ///
Add = null,
Update = null,
Delete = new UpdateKbOperationDTODelete(null, toDelete)
});
log.LogInformation("Delete-->Done");
log.LogInformation("Add-->Start");
List<FileDTO> toAdd = new List<FileDTO>();
toAdd.Add(new FileDTO("<my-file>", "<url-to-file>"));
var updateAdd = await qnaC.Knowledgebase.UpdateAsync(kbId, new UpdateKbOperationDTO
{
// Create JSON of changes ///
Add = new UpdateKbOperationDTOAdd(null, null, toAdd),
Update = null,
Delete = null
});
log.LogInformation("Add-->Done");
我目前有一个 Azure Function,我希望每天左右更新 QnaMaker 知识库。目前一切都已连接并且工作正常,但是我只能将 Qna 对象(qna 对)而不是 urls 发送到我网站上的文件。因此,在我下面提供的示例中,虽然它应该用 2 个问题填充知识库和 url 中的文件,但它只填充问题。
目前这没有给我任何类型的错误,实际上我调用 KB 的响应代码返回为 204。所以它通过了,但仍然没有按预期将文件添加到 KB .
注意:本例中导入的文件 (alice-I.html) 是本次演示的随机文件(不是我的,为了安全),但问题是一样的。如果我直接从 KB 站点本身将此文件添加到 QnaMaker,它工作正常,但它不会从 Azure 功能代码更新。
如果能深入了解正在发生的事情,那就太好了。
内容正在发送到知识库
string replace_kb = @"{
'qnaList': [
{
'id': 0,
'answer': 'A-1',
'source': 'Custom Editorial',
'questions': [
'Q-1'
],
'metadata': []
},
{
'id': 1,
'answer': 'A-2',
'source': 'Custom Editorial',
'questions': [
'Q-2'
],
'metadata': [
{
'name': 'category',
'value': 'api'
}
]
}
],
'files': [
{
'fileName': 'alice-I.html',
'fileUri': 'https://www.cs.cmu.edu/~rgs/alice-I.html'
}
]
}";
向知识库发送内容的代码
using (var clientF = new HttpClient())
using (var requestF = new HttpRequestMessage())
{
requestF.Method = HttpMethod.Put;
requestF.RequestUri = new Uri(<your-uri>);
requestF.Content = new StringContent(replace_kb, Encoding.UTF8, "application/json");
requestF.Headers.Add("Ocp-Apim-Subscription-Key", <your-key>);
var responseF = await clientF.SendAsync(requestF);
if (responseF.IsSuccessStatusCode)
{
log.LogInformation("{'result' : 'Success.'}");
log.LogInformation($"------------>{responseF}");
}
else
{
await responseF.Content.ReadAsStringAsync();
log.LogInformation($"------------>{responseF}");
}
}
所以我仍然不知道如何让上面的工作正常,但我让它以不同的方式工作。基本上我使用了此处列出的 UpdateKbOperationDTO Class:class
这仍然不是完美的解决方案,但它允许我使用代码而不是界面使用文件更新我的知识库。
下面是我的新代码:
QnAMakerClient qnaC = new QnAMakerClient(new ApiKeyServiceClientCredentials(<subscription-key>)) { Endpoint = "https://<your-custom-domain>.cognitiveservices.azure.com"};
log.LogInformation("Delete-->Start");
List<string> toDelete = new List<string>();
toDelete.Add("<my-file>");
var updateDelete = await qnaC.Knowledgebase.UpdateAsync(kbId, new UpdateKbOperationDTO
{
// Create JSON of changes ///
Add = null,
Update = null,
Delete = new UpdateKbOperationDTODelete(null, toDelete)
});
log.LogInformation("Delete-->Done");
log.LogInformation("Add-->Start");
List<FileDTO> toAdd = new List<FileDTO>();
toAdd.Add(new FileDTO("<my-file>", "<url-to-file>"));
var updateAdd = await qnaC.Knowledgebase.UpdateAsync(kbId, new UpdateKbOperationDTO
{
// Create JSON of changes ///
Add = new UpdateKbOperationDTOAdd(null, null, toAdd),
Update = null,
Delete = null
});
log.LogInformation("Add-->Done");