private async Task <DialogTurnResult> ProcessQuestionDialog(WaterfallStepContext step, CancellationToken cancellationToken = default(CancellationToken)) { var question = (string)step.Result; step.ActiveDialog.State["question"] = question; if (question == "/start") { await step.EndDialogAsync(); return(Dialog.EndOfTurn); } using (LuisRouterHelper luisRouterHelper = new LuisRouterHelper(Startup.EnvironmentName, Startup.ContentRootPath)) { var apps = await luisRouterHelper.LuisDiscoveryAsync(step, luisRouterAccessor, step.Context.Activity.Text, Startup.ApplicationCode, Startup.EncryptionKey); if (apps.Count > 0) { LuisAppDetail app = apps.OrderByDescending(x => x.Score).FirstOrDefault(); var recognizerResult = await luisRouterAccessor.LuisServices[app.Name].RecognizeAsync(step.Context, cancellationToken); var topIntent = recognizerResult?.GetTopScoringIntent(); if (topIntent != null && topIntent.HasValue && topIntent.Value.score >= .90 && topIntent.Value.intent != "None") { step.Context.Activity.Text = topIntent.Value.intent; var qnaName = string.Empty; using (QnAMakerHelper qnaMakerHelper = new QnAMakerHelper(Startup.EnvironmentName, Startup.ContentRootPath)) { qnaName = qnaMakerHelper.GetConfiguration().Name; } var response = await qnaMakerAccessor.QnAMakerServices[qnaName].GetAnswersAsync(step.Context); if (response != null && response.Length > 0) { string responseType = string.Empty; responseType = FindResponseTypeMetadata(response[0].Metadata); await step.Context.SendCustomResponseAsync(response[0].Answer, responseType); if (!string.IsNullOrEmpty(responseType)) { if (!topIntent.Value.intent.EndsWith("_Sample")) { List <Choice> choices = new List <Choice>(); choices.Add(new Choice { Value = $"Yes" }); choices.Add(new Choice { Value = $"No" }); var message = $"Would you like to see an example?"; await step.Context.SendCustomResponseAsync(message); PromptOptions options = new PromptOptions { Choices = choices }; return(await step.PromptAsync("AskForExampleValidator", options, cancellationToken : cancellationToken)); } } } else { await accessors.AskForExamplePreference.SetAsync(step.Context, false); await accessors.ConversationState.SaveChangesAsync(step.Context, false, cancellationToken); var message = $"I did not find information to show you"; await step.Context.SendCustomResponseAsync(message); } } else { await accessors.AskForExamplePreference.SetAsync(step.Context, false); await accessors.ConversationState.SaveChangesAsync(step.Context, false, cancellationToken); var message = $"I did not find information to show you"; await step.Context.SendCustomResponseAsync(message); } } else { await accessors.AskForExamplePreference.SetAsync(step.Context, false); await accessors.ConversationState.SaveChangesAsync(step.Context, false, cancellationToken); var message = $"I did not find information to show you"; await step.Context.SendCustomResponseAsync(message); } } return(await step.NextAsync()); }
private async Task <DialogTurnResult> ProcessIfExampleIsRequiredDialog(WaterfallStepContext step, CancellationToken cancellationToken = default(CancellationToken)) { bool askForExample = await accessors.AskForExamplePreference.GetAsync(step.Context, () => { return(false); }); if (askForExample) { var message = $"i would like to see a sample about {step.ActiveDialog.State["question"]}"; //await step.Context.SendActivityAsync(message, cancellationToken: cancellationToken); step.Context.Activity.Text = message; using (LuisRouterHelper luisRouterHelper = new LuisRouterHelper(Startup.EnvironmentName, Startup.ContentRootPath)) { var apps = await luisRouterHelper.LuisDiscoveryAsync(step, luisRouterAccessor, step.Context.Activity.Text, Startup.ApplicationCode, Startup.EncryptionKey); if (apps.Count > 0) { LuisAppDetail app = apps.OrderByDescending(x => x.Score).FirstOrDefault(); var recognizerResult = await luisRouterAccessor.LuisServices[app.Name].RecognizeAsync(step.Context, cancellationToken); var topIntent = recognizerResult?.GetTopScoringIntent(); if (topIntent != null && topIntent.HasValue && topIntent.Value.score >= .90 && topIntent.Value.intent != "None") { step.Context.Activity.Text = topIntent.Value.intent; var qnaName = string.Empty; using (QnAMakerHelper qnaMakerHelper = new QnAMakerHelper(Startup.EnvironmentName, Startup.ContentRootPath)) { qnaName = qnaMakerHelper.GetConfiguration().Name; } var response = await qnaMakerAccessor.QnAMakerServices[qnaName].GetAnswersAsync(step.Context); if (response != null && response.Length > 0) { string responseType = string.Empty; responseType = FindResponseTypeMetadata(response[0].Metadata); await step.Context.SendCustomResponseAsync(response[0].Answer, responseType); } else { await accessors.AskForExamplePreference.SetAsync(step.Context, false); await accessors.ConversationState.SaveChangesAsync(step.Context, false, cancellationToken); message = $"I did not find information to show you"; await step.Context.SendCustomResponseAsync(message); } } else { await accessors.AskForExamplePreference.SetAsync(step.Context, false); await accessors.ConversationState.SaveChangesAsync(step.Context, false, cancellationToken); message = $"I did not find information to show you"; await step.Context.SendCustomResponseAsync(message); } } else { await accessors.AskForExamplePreference.SetAsync(step.Context, false); await accessors.ConversationState.SaveChangesAsync(step.Context, false, cancellationToken); message = $"I did not find information to show you"; await step.Context.SendCustomResponseAsync(message); } } } return(await step.NextAsync()); }
public async void LuisDiscoveryTest() { // arrage var expectedIntent = "Sample"; var expectedName = "Sample"; var expectedScore = 100; var luisAppDetail = new LuisAppDetail() { Intent = expectedIntent, Name = expectedName, Score = expectedScore }; var luisDiscoveryResponse = new LuisDiscoveryResponse() { IsSucceded = true, ResultId = 100, LuisAppDetails = new List <LuisAppDetail>() { luisAppDetail } }; var jsonLuisDiscoveryResponse = JsonConvert.SerializeObject(luisDiscoveryResponse); var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict); handlerMock .Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>() ) .ReturnsAsync(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent(jsonLuisDiscoveryResponse), }) .Verifiable(); var httpClient = new HttpClient(handlerMock.Object) { BaseAddress = new Uri("http://localhost/") }; var storage = new MemoryStorage(); var userState = new UserState(storage); var conversationState = new ConversationState(storage); var adapter = new TestAdapter().Use(new AutoSaveStateMiddleware(conversationState)); var dialogState = conversationState.CreateProperty <DialogState>("dialogState"); var dialogs = new DialogSet(dialogState); var steps = new WaterfallStep[] { async(step, cancellationToken) => { await step.Context.SendActivityAsync("response"); // act ILuisRouterService luisRouterService = new LuisRouterService(httpClient, EnvironmentName, ContentRootPath, userState); var result = await luisRouterService.LuisDiscoveryAsync(step, "TEXT", "APPLICATIONCODE", "ENCRYPTIONKEY"); var item = result.ToList().FirstOrDefault(); // assert Assert.Equal(expectedIntent, item.Intent); Assert.Equal(expectedName, item.Name); Assert.Equal(expectedScore, item.Score); return(Dialog.EndOfTurn); } }; dialogs.Add(new WaterfallDialog( "test", steps)); await new TestFlow(adapter, async(turnContext, cancellationToken) => { var dc = await dialogs.CreateContextAsync(turnContext, cancellationToken); await dc.ContinueDialogAsync(cancellationToken); if (!turnContext.Responded) { await dc.BeginDialogAsync("test", null, cancellationToken); } }) .Send("ask") .AssertReply("response") .StartTestAsync(); }