将Microsoft QnA Maker连接到Bot Framework

时间:2017-05-31 21:25:47

标签: facebook frameworks bots

我使用QnA Maker创建了一个机器人,并设置了连接到我们的FB页面和Microsoft Bot Framework的FB应用程序。但是,缺少某些东西。如何将Microsoft QnA制造商连接到Bot Framework? (FWIW - 目标是FB Messenger机器人,可以回答有关非盈利事件的常见问题解答)。感谢

3 个答案:

答案 0 :(得分:0)

您无法直接将QnAMaker端点链接到FB。首先需要使用QnAMaker模板创建Bot服务,然后在FB通道上启用它。见https://docs.microsoft.com/en-us/bot-framework/azure/azure-bot-service-quickstart

答案 1 :(得分:0)

您需要在QNA制造商处注册并使用以下代码获取回复。无需在机器人框架上注册。

示例请求

string responseString = string.Empty;

var query = “hi”; //User Query
var knowledgebaseId = “YOUR_KNOWLEDGE_BASE_ID”; // Use knowledge base id created.
var qnamakerSubscriptionKey = “YOUR_SUBSCRIPTION_KEY”; //Use subscription key assigned to you.

//Build the URI
Uri qnamakerUriBase = new Uri("https://westus.api.cognitive.microsoft.com/qnamaker/v1.0");
var builder = new UriBuilder($"{qnamakerUriBase}/knowledgebases/{knowledgebaseId}/generateAnswer");

//Add the question as part of the body
var postBody = $"{{\"question\": \"{query}\"}}";

//Send the POST request
using (WebClient client = new WebClient())
{
    //Set the encoding to UTF8
    client.Encoding = System.Text.Encoding.UTF8;

    //Add the subscription key header
    client.Headers.Add("Ocp-Apim-Subscription-Key", qnamakerSubscriptionKey);
    client.Headers.Add("Content-Type", "application/json");
    responseString = client.UploadString(builder.Uri, postBody);
}

示例回复

using Newtonsoft.Json; 

private class QnAMakerResult
{
    /// <summary>
    /// The top answer found in the QnA Service.
    /// </summary>
    [JsonProperty(PropertyName = "answer")]
    public string Answer { get; set; }

    /// <summary>
    /// The score in range [0, 100] corresponding to the top answer found in the QnA    Service.
    /// </summary>
    [JsonProperty(PropertyName = "score")]
    public double Score { get; set; }
}
//De-serialize the response
QnAMakerResult response;
try
{
    response = JsonConvert.DeserializeObject< QnAMakerResult >(responseString);
}
catch
{
    throw new Exception("Unable to deserialize QnA Maker response string.");
}

注意:要获取知识库ID和订阅密钥,您需要登录并创建服务

如果您需要任何帮助,请告诉我

答案 2 :(得分:0)

为了在机器人框架中使用QnAMaker,您可以在获得无意图或某种意图时处理。 只需在LUIS对话框中添加它

即可
[LuisModel("modelID", "SubscriptionKey")]
[Serializable]
public class RootDialog : LuisDialog<object>
{
    [LuisIntent("None")]
    public async Task NoneRes(IDialogContext context, LuisResult result)
    {
            var qnadialog = new QnADialog();
            await context.Forward(new QnADialog(), AfterQnADialog, context.Activity, CancellationToken.None);
    }
    private async Task AfterQnADialog(IDialogContext context, IAwaitable<object> result)
    {
        var answerFound = await result;
        // handle after qna response
    }
}

并在QnADialog中添加此内容

[Serializable]
[QnAMaker(authKey: "AuthKey", knowledgebaseId: "KnowledgebaseId", defaultMessage: "please rephrase, I could not understand.", scoreThreshold: 0.5, top: 1, endpointHostName: "https://yourAccount.azurewebsites.net/qnamaker")]
public class QnADialog : QnAMakerDialog
{}

完成。 希望这会有所帮助

相关问题