C#机器人中的QnA Maker服务没有响应

时间:2019-06-21 09:42:07

标签: c# azure bots azure-bot-service

我在C#(SDK v3)的QnA模板中拥有一个漫游器。我试图将LUIS集成到QnA Maker服务中。但是漫游器无法回复查询。 Bot有BasicQnAMakerDialog.csLuisDialog.cs个文件。

BasicQnAMakerDialog.cs文件代码-

using System;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.Bot.Builder.Azure;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.CognitiveServices.QnAMaker;
using Microsoft.Bot.Connector;
using System.Linq;

namespace Microsoft.Bot.Sample.QnABot
{
    [Serializable]
    public class RootDialog : QnAMakerDialog
    {
        public async Task StartAsync(IDialogContext context)
        {
            /* Wait until the first message is received from the conversation and call MessageReceviedAsync 
            *  to process that message. */
            context.Wait(this.MessageReceivedAsync);
        }

        public RootDialog() : base(new QnAMakerService(new QnAMakerAttribute(Utils.GetAppSetting("QnAAuthKey"), Utils.GetAppSetting("QnAKnowledgebaseId"), "No good match found in KB", 0.3)))
        {
        }

        protected override async Task RespondFromQnAMakerResultAsync(IDialogContext context, IMessageActivity message, QnAMakerResults result)
        {
            var answer = result.Answers.First().Answer;
            Activity reply = ((Activity)context.Activity).CreateReply();
            await context.Forward(new BasicQnAMakerDialog(), AfterAnswerAsync, message, CancellationToken.None);
            await context.PostAsync(reply);
        }

        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
        {
            // When MessageReceivedAsync is called, it's passed an IAwaitable<IMessageActivity>. To get the message,
            // await the result.
            var message = await result;

            var qnaAuthKey = GetSetting("QnAAuthKey");
            var qnaKBId = Utils.GetAppSetting("QnAKnowledgebaseId");
            var endpointHostName = Utils.GetAppSetting("QnAEndpointHostName");

            // QnA Subscription Key and KnowledgeBase Id null verification
            if (!string.IsNullOrEmpty(qnaAuthKey) && !string.IsNullOrEmpty(qnaKBId))
            {
                // Forward to the appropriate Dialog based on whether the endpoint hostname is present
                if (string.IsNullOrEmpty(endpointHostName))
                    await context.Forward(new BasicQnAMakerPreviewDialog(), AfterAnswerAsync, message, CancellationToken.None);
                else
                    await context.Forward(new BasicQnAMakerDialog(), AfterAnswerAsync, message, CancellationToken.None);
            }
            else
            {
                await context.PostAsync("Please set QnAKnowledgebaseId, QnAAuthKey and QnAEndpointHostName (if applicable) in App Settings. Learn how to get them at https://aka.ms/qnaabssetup.");
            }

        }

        private async Task AfterAnswerAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
        {
            // wait for the next user message
            context.Wait(MessageReceivedAsync);
        }

        public static string GetSetting(string key)
        {
            var value = Utils.GetAppSetting(key);
            if (String.IsNullOrEmpty(value) && key == "QnAAuthKey")
            {
                value = Utils.GetAppSetting("QnASubscriptionKey"); // QnASubscriptionKey for backward compatibility with QnAMaker (Preview)
            }
            return value;
        }
    }

    // Dialog for QnAMaker Preview service
    [Serializable]
    public class BasicQnAMakerPreviewDialog : QnAMakerDialog
    {
        // Go to https://qnamaker.ai and feed data, train & publish your QnA Knowledgebase.
        // Parameters to QnAMakerService are:
        // Required: subscriptionKey, knowledgebaseId, 
        // Optional: defaultMessage, scoreThreshold[Range 0.0 – 1.0]
        public BasicQnAMakerPreviewDialog() : base(new QnAMakerService(new QnAMakerAttribute(RootDialog.GetSetting("QnAAuthKey"), Utils.GetAppSetting("QnAKnowledgebaseId"), "No good match in FAQ.", 0.5)))
        { }
    }

    // Dialog for QnAMaker GA service
    [Serializable]
    public class BasicQnAMakerDialog : QnAMakerDialog
    {
        // Go to https://qnamaker.ai and feed data, train & publish your QnA Knowledgebase.
        // Parameters to QnAMakerService are:
        // Required: qnaAuthKey, knowledgebaseId, endpointHostName
        // Optional: defaultMessage, scoreThreshold[Range 0.0 – 1.0]
        public BasicQnAMakerDialog() : base(new QnAMakerService(new QnAMakerAttribute(RootDialog.GetSetting("QnAAuthKey"), Utils.GetAppSetting("QnAKnowledgebaseId"), "No good match in FAQ.", 0.5, 1, Utils.GetAppSetting("QnAEndpointHostName"))))
        { }

    }  
}

LuisDialog.cs文件代码-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Luis;
using Microsoft.Bot.Builder.Luis.Models;
using Microsoft.Bot.Connector;

namespace Microsoft.Bot.Sample.QnABot
{
    [LuisModel("Luis-app-key", "Luis-auth-key")]
    [Serializable]
    public class LuisDialog : LuisDialog<object>
    {
        [LuisIntent("azureBot")]
        public async Task azureBotIntent(IDialogContext context, IAwaitable<IMessageActivity> message, LuisResult result)
        {
            //await context.PostAsync("No answer found");
            //context.Wait(MessageReceived);

            var messageToForward = await message as Activity;
            await context.Forward(new RootDialog(), this.AfterQnA, messageToForward, CancellationToken.None);
            //check here if I place this below line.
            //await context.PostAsync("No answer found");

        }

         private async Task AfterQnA(IDialogContext context, IAwaitable<IMessageActivity> result) {
         context.Wait(this.MessageReceived);
         }

    }
}

我将MessageController.cs文件的一部分从await Conversation.SendAsync(activity, () => new RootDialog());更改为await Conversation.SendAsync(activity, () => new LuisDialog());

构建它时没有出现任何错误,但是bot无法响应用户查询。任何帮助将不胜感激。

0 个答案:

没有答案