如何在现有的QNA机器人中添加路易斯



我有一个现有的QNA bot(c#,sdk-v4(,现在我想向其添加路易斯,而无需使用路易斯模板创建新的机器人。

我的qnabot.cs文件 -

public class QnABot : ActivityHandler
    {
        private readonly IConfiguration _configuration;
        private readonly ILogger<QnABot> _logger;
        private readonly IHttpClientFactory _httpClientFactory;

        public QnABot(IConfiguration configuration, ILogger<QnABot> logger, IHttpClientFactory httpClientFactory)
        {
            _configuration = configuration;
            _logger = logger;
            _httpClientFactory = httpClientFactory;
        }
        protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            var httpClient = _httpClientFactory.CreateClient();
            var qnaMaker = new QnAMaker(new QnAMakerEndpoint
            {
                KnowledgeBaseId = _configuration["QnAKnowledgebaseId"],
                EndpointKey = _configuration["QnAAuthKey"],
                Host = GetHostname()
            },
            null,
            httpClient);
            _logger.LogInformation("Calling QnA Maker");
            // The actual call to the QnA Maker service.
            var response = await qnaMaker.GetAnswersAsync(turnContext);
            if (response != null && response.Length > 0)
            {
                awaitturnContext.SendActivityAsync(
              MessageFactory.Text(response[0].Answer), cancellationToken);
            }
            else
            {
                await turnContext.SendActivityAsync(MessageFactory.Text("No QnA Maker answers were found."), cancellationToken);
            }
        }
        private string GetHostname()
        {
            var hostname = _configuration["QnAEndpointHostName"];
            if (!hostname.StartsWith("https://"))
            {
                hostname = string.Concat("https://", hostname);
            }
            if (!hostname.EndsWith("/qnamaker"))
            {
                hostname = string.Concat(hostname, "/qnamaker");
            }
            return hostname;
        }
    }

我知道可以使用知识库的Luis应用程序进行调度工具,但我不知道如何处理该机器人中的路易斯意图。我如何将路易斯集成在此机器人中?

您可以将路易斯添加到现有的QNA bot中,但是从本质上讲,您将从此示例中复制大量代码,因此只要从示例开始并通过任何代码复制您想从现有的QNA bot中远离。

您的OnMessageactivity应该从直接调用QNAMAKER客户端的看起来像这样的外观转移到这样的外观,在该外观中,用户的输入将传递给Luis Dispatch应用程序,该应用程序决定了将用户路由到达的意图。

在[dispatchToPintent]内处理用户的路由#l51(方法,案例语句中的字符串匹配了门户中的路易斯应用下的意图名称。

不必说,您需要在bot Microsoft.Bot.Builder.Ai.LUIS中包含一些其他软件包,而您需要在项目中创建IBotServices接口和BotServices类以及其他更改。

此处记录了整个过程。

最新更新