单个机器人中的LUIS和Azure搜索

时间:2018-08-03 14:39:11

标签: azure luis azure-search

对于Bot Builder我是一个新手。我需要使用LUI的意图,然后最终调用azure-search来搜索意图。

但是,下面是我的代码。在Aco-Saerch意图中,我想从用户的消息中提取意图,然后在azure搜索中传递给查询。

在对话框 SearchDialog 中,瀑布功能在第一个功能之后没有继续进行。谁能帮我解决这里的问题。

我正在尝试使用以下git-hub共享代码中的azure搜索代码和库:Realstate azure search node js code

将来,我也想添加QnA识别器。目的是:以qnA为基础搜索查询,如果找到,则返回其他值,使用LUIS找出意图,然后将其传递给天蓝色搜索。

var util = require('util');
var _ = require('lodash');
var builder = require('botbuilder');
var restify = require('restify');

/// <reference path="../SearchDialogLibrary/index.d.ts" />
var SearchLibrary = require('../SearchDialogLibrary');
var AzureSearch = require('../SearchProviders/azure-search');

// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
    console.log('%s listening to %s', server.name, server.url);
});

// Create chat bot and listen for messages
var connector = new builder.ChatConnector({
    appId: process.env.MICROSOFT_APP_ID,
    appPassword: process.env.MICROSOFT_APP_PASSWORD
});
server.post('/api/messages', connector.listen());

// Bot Storage: Here we register the state storage for your bot. 
// Default store: volatile in-memory store - Only for prototyping!
// We provide adapters for Azure Table, CosmosDb, SQL Azure, or you can implement your own!
// For samples and documentation, see: https://github.com/Microsoft/BotBuilder-Azure
var inMemoryStorage = new builder.MemoryBotStorage();

const LuisModelUrl = 'https://westeurope.api.cognitive.microsoft.com/luis/v2.0/apps/180a9aaa-9d67-4d40-b3d3-121917f4dbb8?subscription-key=39155bb750dc4b2abd84d410d80fce21&timezoneOffset=0&q=';
var recognizer = new builder.LuisRecognizer(LuisModelUrl);
// Bot with main dialog that triggers search and display its results
var bot = new builder.UniversalBot(connector, function (session, args) {
    session.send('You reached the default message handler. You said \'%s\'.', session.message.text);
}).set('storage', inMemoryStorage);

bot.recognizer(recognizer);

bot.dialog('GreetingDialog',
    (session) => {
        session.send('You reached the Greeting intent. You said \'%s\'.', session.message.text);
        session.endDialog();
    }
).triggerAction({
    matches: 'Greeting'
})

bot.dialog('HelpDialog',
    (session) => {
        session.send('You reached the Help intent. You said \'%s\'.', session.message.text);
        session.endDialog();
    }
).triggerAction({
    matches: 'Help'
})

bot.dialog('CancelDialog',
    (session) => {
        session.send('You reached the Cancel intent. You said \'%s\'.', session.message.text);
        session.endDialog();
    }
).triggerAction({
    matches: 'Cancel'
})

bot.dialog('SearchDialog', [
    function (session, args) {
        var intent = args.intent;
        var title = builder.EntityRecognizer.findEntity(intent.entities, 'businessterm');
        console.log(title.entity);
        session.send('You reached the Search.Aco intent. You enquire for the entitiy \'%s\'.', title.entity);
    },
    function (session, args, results) {
        // Trigger Search
        console.log("Inside the SearchLibrary.begin");
        SearchLibrary.begin(session);
        console.log("after  the SearchLibrary.begin");
    },
    function (session, args, results) {
        // Process selected search results
        session.send(
            'Done! For future reference, you selected these properties: %s',
            args.selection.map(function (i) { return i.key; }).join(', '));
    }
]).triggerAction({
    matches: 'Search.Aco'
});

var azureSearchClient = AzureSearch.create('aco-intel2', '4105C6676D0CDD9B2E7891952B9E9E00', 'azureblob-index');
var jobsResultsMapper = SearchLibrary.defaultResultsMapper(jobToSearchHit);

// Register Search Dialogs Library with bot
bot.library(SearchLibrary.create({
    multipleSelection: true,
    search: function (query) { return azureSearchClient.search(query).then(jobsResultsMapper); },
    refiners: ['people', 'content', 'location']
}));

// Maps the AzureSearch Job Document into a SearchHit that the Search Library can use
function jobToSearchHit(acosearch) {
    console.log("inside jobToSearchHit");
    console.log("inside acosearch.DocUrl" + acosearch.DocUrl + "-------" + acosearch.metadata_storage_name);
    return {
        key: acosearch.id,
        title: acosearch.metadata_storage_name,
        description: acosearch.content.substring(0, 100)+"...",
        documenturl:acosearch.DocUrl,
        imageUrl: acosearch.imageurl
    };
}

1 个答案:

答案 0 :(得分:0)

您的对话框没有前进,因为没有任何动作可以执行。因此,它到达第一个功能的末尾而无处可继续。您有几种选择。您可以:

  1. 您可以包含提示或按钮(即某种动作)来移动对话框。例如:

    bot.dialog('SearchDialog', [
        function (session, args) {
            var intent = args.intent;
            var title = builder.EntityRecognizer.findEntity(intent.entities, 'businessterm');
            console.log(title.entity);
            session.send('You reached the Search.Aco intent. You enquire for the entitiy \'%s\'.', title.entity);
            builder.Prompts.choice(session, "Start search?", "Yes|No", { listStyle: builder.ListStyle["button"] });
        },
        function (session, args, results) {
            // Trigger Search
            console.log("Inside the SearchLibrary.begin");
            SearchLibrary.begin(session);
            console.log("after  the SearchLibrary.begin");
        }
    
  2. 您可以将第一个和第二个功能结合在一起。但是,再次,您将需要一些将对话框从搜索功能移至搜索结果功能的方法。

    bot.dialog('SearchDialog', [
        function (session, args) {
            var intent = args.intent;
            var title = builder.EntityRecognizer.findEntity(intent.entities, 'businessterm');
            console.log(title.entity);
            session.send('You reached the Search.Aco intent. You enquire for the entitiy \'%s\'.', title.entity);
    
            // Trigger Search
            console.log("Inside the SearchLibrary.begin");
            SearchLibrary.begin(session);
            console.log("after  the SearchLibrary.begin");
        },
        function (session, args, results) {
            // Process selected search results
            session.send(
                'Done! For future reference, you selected these properties: %s',
                args.selection.map(function (i) { return i.key; }).join(', '));
        }
    ]).triggerAction({
    matches: 'Search.Aco'
    });
    
  3. 您可以将搜索功能设置为一个单独的对话框,该对话框在第一个对话框功能的末尾启动,并在完成后返回并继续原始对话框。

    bot.dialog('SearchDialog', [
        function (session, args) {
            var intent = args.intent;
            var title = builder.EntityRecognizer.findEntity(intent.entities, 'businessterm');
            console.log(title.entity);
            session.send('You reached the Search.Aco intent. You enquire for the entitiy \'%s\'.', title.entity);
            session.beginDialog('/searchAzure');
        },
        function (session, args, results) {
            // Process selected search results
            session.send(
                'Done! For future reference, you selected these properties: %s',
                args.selection.map(function (i) { return i.key; }).join(', '));
        }
    ]).triggerAction({
    matches: 'Search.Aco'
    });
    
    bot.dialog('/searchAzure', [
        function (session) {
            // Trigger Search
            console.log("Inside the SearchLibrary.begin");
            SearchLibrary.begin(session);
            console.log("after  the SearchLibrary.begin");
            session.endDialog();
        }
    ]);