如何在Azure Bot服务上同时使用英语和法语QnaMaker?

时间:2018-04-06 20:13:12

标签: node.js azure botframework chatbot qnamaker

我们旁边有一个聊天机器人和一个按钮,它可以改变英语和法语之间的机器人语言。我还有一个QnAmaker包含英语和法语问题。当机器人处于英语模式时,我们是否有办法只处理英语问题?同样适用于法语。

Note: my bot react like this:
    user: what is teww?
                       Do you mean this, select one of question below: BOT
                         1. French question
                         2. English question
                         3  French question

我希望防止这种情况发生,我正在考虑为英语创建两个qnamaker一个用于法语。但是,我不知道在天蓝色僵尸服务器中处理这个qna变化?有办法解决它吗?

平台:Azure Bot服务           Node.js的           Microsoft QnA Maker

1 个答案:

答案 0 :(得分:3)

是的,您可以为您的英语和法语问题创建2个QnAMaker知识库。您可以利用Text Analytics API检测机器人recevie中间件中的用户输入语言,该中间件显示在Determine the locale by using analytics

请参阅以下代码段:

var recognizer1 = new cognitiveservices.QnAMakerRecognizer({
    knowledgeBaseId: <knowledgeBaseId_1>,
    subscriptionKey: <subscriptionKey_1>,
    top: 4
});

var basicQnAMakerDialog = new cognitiveservices.QnAMakerDialog({
    recognizers: [recognizer1],
    qnaThreshold: 0.3,
});
var recognizer2 = new cognitiveservices.QnAMakerRecognizer({
    knowledgeBaseId: <knowledgeBaseId_2>,
    subscriptionKey: <subscriptionKey_2>,
    top: 4
});

var basicQnAMakerDialog2 = new cognitiveservices.QnAMakerDialog({
    recognizers: [recognizer2],
    qnaThreshold: 0.3,
});

bot.dialog('en', basicQnAMakerDialog);
bot.dialog('zh', basicQnAMakerDialog2);

bot.dialog('/', [(session, args) => {
    switch (session.preferredLocale()) {
        case 'zh':
            session.replaceDialog('zh', args)
            break;
        case 'en-US':
        default:
            session.replaceDialog('en', args)
            break;
    }
}])

bot.use({
    receive: function (event, next) {
        if (event.text) {
            var options = {
                method: 'POST',
                url: 'https://westus2.api.cognitive.microsoft.com/text/analytics/v2.0/languages?numberOfLanguagesToDetect=1',
                body: {
                    documents: [{
                        id: 'message',
                        text: event.text
                    }]
                },
                json: true,
                headers: {
                    'Ocp-Apim-Subscription-Key': '230f82f08c014accbd067ddc2c0ea98f'
                }
            };
            request(options, function (error, response, body) {
                if (!error && body) {
                    if (body.documents && body.documents.length > 0) {
                        var languages = body.documents[0].detectedLanguages;
                        if (languages && languages.length > 0) {
                            event.textLocale = languages[0].iso6391Name;
                        }
                    }
                }
                next();
            });
        } else {
            next();
        }
    }
});