Exemplo n.º 1
0
        protected override async Task MessageReceived(IDialogContext context, IAwaitable <IMessageActivity> item)
        {
            var message     = await item;
            var messageText = await GetLuisQueryTextAsync(context, message);

            var tasks   = this.services.Select(s => s.QueryAsync(messageText, context.CancellationToken)).ToArray();
            var results = await Task.WhenAll(tasks);

            var winners = from result in results.Select((value, index) => new { value, index })
                          let resultWinner = this.BestIntentFrom(result.value)
                                             where resultWinner != null
                                             select new LuisServiceResult(result.value, resultWinner, this.services[result.index]);

            var winner = this.BestResultFrom(winners);

            if (winner == null)
            {
                throw new InvalidOperationException("No winning intent selected from Luis results.");
            }

            var intentName = default(string);
            var luisAction = this.actionResolver.ResolveActionFromLuisIntent(winner.Result, out intentName);

            if (luisAction != null)
            {
                var executionContextChain = new List <ActionExecutionContext> {
                    new ActionExecutionContext(intentName, luisAction)
                };
                while (LuisActionResolver.IsContextualAction(luisAction))
                {
                    var luisActionDefinition = default(LuisActionBindingAttribute);
                    if (!LuisActionResolver.CanStartWithNoContextAction(luisAction, out luisActionDefinition))
                    {
                        await context.PostAsync($"Cannot start contextual action '{luisActionDefinition.FriendlyName}' without a valid context.");

                        return;
                    }

                    luisAction = LuisActionResolver.BuildContextForContextualAction(luisAction, out intentName);
                    if (luisAction != null)
                    {
                        this.onContextCreation?.Invoke(luisAction, context);

                        executionContextChain.Insert(0, new ActionExecutionContext(intentName, luisAction));
                    }
                }

                var validationResults = default(ICollection <ValidationResult>); //check the validation result
                if (!luisAction.IsValid(out validationResults))
                {
                    var childDialog = new LuisActionMissingEntitiesDialog(winner.LuisService, executionContextChain);

                    context.Call(childDialog, this.LuisActionMissingDialogFinished);
                }
                else
                {
                    await this.DispatchToLuisActionActivityHandler(context, item, intentName, luisAction);
                }
            }
        }
Exemplo n.º 2
0
        protected override async Task MessageReceived(IDialogContext context, IAwaitable <IMessageActivity> item)
        {
            LuisServiceResult winner = await GetWinner(context, item);

            if (winner == null)
            {
                throw new InvalidOperationException("No winning intent selected from Luis results.");
            }

            ILuisAction luisAction = this.actionResolver.ResolveActionFromLuisIntent(winner.Result, out string intentName);

            if (luisAction != null)
            {
                var executionContextChain = new ActionExecutionContext(intentName, luisAction).ToSingleList();
                while (LuisActionResolver.IsContextualAction(luisAction))
                {
                    if (!LuisActionResolver.CanStartWithNoContextAction(luisAction, out LuisActionBindingAttribute luisActionDefinition))
                    {
                        await context.PostAsync($"Cannot start contextual action '{luisActionDefinition.FriendlyName}' without a valid context.");

                        return;
                    }

                    luisAction = LuisActionResolver.BuildContextForContextualAction(luisAction, out intentName);
                    if (luisAction != null)
                    {
                        this.onContextCreation?.Invoke(luisAction, context);
                        executionContextChain.Insert(0, new ActionExecutionContext(intentName, luisAction));
                    }
                }

                if (!luisAction.IsValid(out ICollection <ValidationResult> validationResults))
                {
                    var childDialog = new LuisActionMissingEntitiesDialog(winner.LuisService, executionContextChain);
                    context.Call(childDialog, this.LuisActionMissingDialogFinished);
                }
                else
                {
                    await this.DispatchToLuisActionActivityHandler(context, item, intentName, luisAction);
                }
            }
        }
Exemplo n.º 3
0
        private static async Task RunQuery(string query)
        {
            // Process message
            var luisService = new LuisService(new LuisModelAttribute(ConfigurationManager.AppSettings["LUIS_ModelId"], ConfigurationManager.AppSettings["LUIS_SubscriptionKey"]));
            var luisResult  = await luisService.QueryAsync(query, CancellationToken.None);

            // Try to resolve intent to action
            var intentName     = default(string);
            var intentEntities = default(IList <EntityRecommendation>);
            var intentAction   = new LuisActionResolver(typeof(GetTimeInPlaceAction).Assembly)
                                 .ResolveActionFromLuisIntent(luisResult, out intentName, out intentEntities);

            if (intentAction != null)
            {
                var executionContextChain = new List <ActionExecutionContext> {
                    new ActionExecutionContext(intentName, intentAction)
                };
                while (LuisActionResolver.IsContextualAction(intentAction))
                {
                    var luisActionDefinition = default(LuisActionBindingAttribute);
                    if (!LuisActionResolver.CanStartWithNoContextAction(intentAction, out luisActionDefinition))
                    {
                        Console.WriteLine($"Cannot start contextual action '{luisActionDefinition.FriendlyName}' without a valid context.");

                        return;
                    }

                    intentAction = LuisActionResolver.BuildContextForContextualAction(intentAction, out intentName);
                    if (intentAction != null)
                    {
                        executionContextChain.Insert(0, new ActionExecutionContext(intentName, intentAction));
                    }
                }

                await RunActions(luisService, executionContextChain);
            }
            else
            {
                Console.WriteLine("Could not understand the input.");
            }
        }