4

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');
        });
    } 
]);
Mickael B.
  • 4,755
  • 4
  • 24
  • 48
Fraangel
  • 99
  • 2
  • 8

3 Answers3

2

You will have to forward the user message to QnaMaker from the method/dialog associated to the intent detected by LUIS. Take a look to this article (https://blog.botframework.com/2017/11/17/qna-maker-node-js-bots/) to find how implement QnAMaker in Node.js

Something like:

var recognizer = new cognitiveservices.QnAMakerRecognizer({
    knowledgeBaseId: 'set your kbid here', 
    subscriptionKey: 'set your subscription key here'});

var context = session.toRecognizeContext();

recognizer.recognize(context, function (error, result) { // your code... }

You should explore the samples also and try to understand how everything works: https://github.com/Microsoft/BotBuilder-CognitiveServices/tree/master/Node/samples/QnAMaker

If you vary a lot your question; could be possible that QnA won't detect the question you expected and in that case you will have to train your KB more (like you do in LUIS with the utterances/intents)

Ezequiel Jadib
  • 14,767
  • 2
  • 38
  • 43
  • As I wrote above I want to combine Luis and QnA, not only use QnA as you suggested me. It's a precise question and I'm finding a precise answer. – Fraangel Jan 10 '18 at 20:39
  • That's the precise answer, once you get back from LUIS, in the method/dialog associated to your intent, you need to call QnA. – Ezequiel Jadib Jan 10 '18 at 20:46
  • Sorry but I cannot figure out, may you provide an example of code? – Fraangel Jan 10 '18 at 20:48
  • I just edited the question, please take a look to my code – Fraangel Jan 10 '18 at 21:18
  • You don't have to add the qnamaker recognizer to the list of recognizer of the intent dialog; if you do that, basically both recognizer will run in parallel and compete each other. Again, you should go through the samples and read the readme files to have more context of how things work – Ezequiel Jadib Jan 11 '18 at 01:46
  • I think of the main problem of the OP would be the fact that all his KB are grouped in a unique intent of LUIS, hence he will not be 100% sure to get the QnA answer if he is calling QnAMaker after with his original question and not the one exactly matching in QnAMaker – Nicolas R Jan 11 '18 at 09:23
1

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'
});
D4RKCIDE
  • 3,439
  • 1
  • 18
  • 34
  • Is that what the dispatch tool does? https://github.com/Microsoft/botbuilder-tools/tree/master/packages/Dispatch#create-bot-dispatch-using-bot-file – tatigo Oct 26 '18 at 16:09
  • 1
    Dispatch did not exist when this question was answered, but yes, you could use dispatch for this. – D4RKCIDE Oct 26 '18 at 17:34
  • Hi, I'm switching from BotBuilder V3 to BotBuilder V4. I followed the sample on GitHub: "nlp-with-dispatch" but the case I need it's not covered. What I'm looking for is not to switch between LUIS and QnA but it's to query directly a QnAMaker KB once to bot founded the corresponding LUIS intent. Should dispatch could be used to do that like in V3? Thanks – Fraangel Jan 18 '19 at 15:41
1

As of 2018 (BotBuilder V4)

You can now use the Dispatch Command Line tool to dispatch intent across multiple bot modules such as LUIS models and QnA.

So first you will get to LUIS app that will decide based on the score to redirect to another LUIS app or to a QnA.

Dispatch Tool
Example using LUIS as dispatch service

tatigo
  • 2,174
  • 1
  • 26
  • 32
  • Hi, I'm switching from BotBuilder V3 to BotBuilder V4. I followed the sample on GitHub: "nlp-with-dispatch" but the case I need it's not covered. What I'm looking for is not to switch between LUIS and QnA but it's to query directly a QnAMaker KB once to bot founded the corresponding LUIS intent. In all articles I didn't find nothing like what I want. Any idea how to reach that? Thanks – Fraangel Jan 18 '19 at 15:39
  • @Fraangel you use the LUIS dispatch service to get the intent, and then call the QnA service OnTurnAsync method of the bot – tatigo Jan 18 '19 at 18:38
  • thanks for your answer. Even if I need only one LUIS app I have to use dispatch? About "QnA service OnTurnAsync method" where I have to use it? I was wondering to use it when I found an intent in Luis. – Fraangel Jan 19 '19 at 16:15
  • @Faangel Dispatch is just a LUIS app, you can call it as you like. Just go thru other examples of the bot framework. https://github.com/Microsoft/BotBuilder-Samples – tatigo Jan 21 '19 at 17:30