Search code examples
node.jsbotframeworkazure-language-understandingazure-cognitive-servicesazure-qna-maker

How to integrate LUIS and QnA Maker services in single Node.js bot?


I'm developing a chatbot using Microsoft Bot Framework with Node.js SDK. I've integrated LUIS and QnA maker but I want to create this scenario if it's possible. Taking in example the following link and in particular this section:

There are a few ways that a bot may implement a hybrid of LUIS and QnA Maker: Call LUIS first, and if no intent meets a specific threshold score, i.e., "None" intent is triggered, then call QnA Maker. Alternatively, create a LUIS intent for QnA Maker, feeding your LUIS model with example QnA questions that map to "QnAIntent."

Just an example: I have my QnA KB in which I have a pair : " who are you?" / "Hi I'm your bot! " . Then I have my Luis app that recognize this intent called "common" . So, if I write to my bot: " who are you?" it will answer "Hi I'm your bot! " Instead, if I write " tell me who you are" it recognize the LUIS intent related to the question but it will not answer "Hi I'm your bot! " like I imagine.

So what I imagine is: I ask the question "Tell me who you are" --> the bot triggers the intent common (LUIS) --> then I want that the bot will answer me looking into the QnA KB --> "Hi I'm your bot! "

Is it possible?

Hope this code could help:

var intents = new builder.IntentDialog({ recognizers[luisRecognizer,qnarecognizer] });

bot.dialog('/', intents);

intents.matches('common_question', [
    function (session, args, next) {
        session.send('Intent common');
        qnarecognizer.recognize(session, function (error, result) {
            session.send('answerEntity.entity');
        });
    } 
]);

Solution

  • I wrote this because I want more practice with node and this was an excuse to use node, but what Ezequiel is telling you is completely correct. I'll also post on your GitHub issue. This is a functioning node app that does what you need

    var builder = require('botbuilder');
    var restify = require('restify');
    var cog = require('botbuilder-cognitiveservices');
    
    
    
    var server = restify.createServer();
    server.listen(process.env.port || process.env.PORT || 3978, function() {
        console.log('%s listening to %s', server.name, server.url);
    });
    
    var connector = new builder.ChatConnector({
        appId: "APP ID",
        appPassword: "APP PASSWORD"
    });
    server.post('/api/messages', connector.listen());
    
    var bot = new builder.UniversalBot(connector, function(session) {
        session.send('Sorry, I did not understand \'%s\'. Type \'help\' if you need assistance.', session.message.text);
    });
    
    var recognizer = new builder.LuisRecognizer("https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/{LUIS APP ID}?subscription-key={LUIS KEY}&verbose=true&timezoneOffset=0&q=");
    
    bot.recognizer(recognizer);
    var qnaRecognizer = new cog.QnAMakerRecognizer({
        knowledgeBaseId: 'QNA APP ID',
        subscriptionKey: 'QNA SUBSCRIPTION KEY'
    }); 
    
    bot.dialog('Common', function(session) {
        var query = session.message.text;        
        cog.QnAMakerRecognizer.recognize(query, 'https://westus.api.cognitive.microsoft.com/qnamaker/v2.0/knowledgebases/{QNA APP ID}}/generateAnswer', '{QNA SUBSCRIPTION KEY}', 1, 'intentName', (error, results) => {
            session.send(results.answers[0].answer)    
        })    
    }).triggerAction({
        matches: 'Common'
    });