public ConversationResponse HandleMessage(Activity activity, ItemContextParameters parameters) { IConversation conversation = (ConversationHistory.Conversations.Any()) ? ConversationHistory.Conversations.Last() : null; var inConversation = conversation != null && !conversation.IsEnded; // determine which intent user wants and context var result = LuisService.Query(AppId, activity.Text); var requestedQuit = result.TopScoringIntent.Intent.ToLower().Equals("quit") && result.TopScoringIntent.Score > 0.4; var intent = IntentProvider.GetIntent(AppId, result.TopScoringIntent.Intent); // if the user is trying to end or finish a conversation if (inConversation && requestedQuit) { conversation.IsEnded = true; return(intent.Respond(null, null, null)); } // start a new conversation if not in one if (!inConversation && intent != null && result.TopScoringIntent.Score > 0.4) { conversation = ConversationFactory.Create(result, intent); ConversationHistory.Conversations.Add(conversation); inConversation = true; } if (inConversation) { // check and request all required parameters of a conversation foreach (ConversationParameter p in conversation.Intent.RequiredParameters) { if (!TryGetParam(p.ParamName, result, conversation, parameters, p.ParamGetter)) { return(RequestParam(p, conversation, parameters)); } } conversation.IsEnded = true; return(conversation.Intent.Respond(result, parameters, conversation)); } // determine mood of comment var sentiment = GetSentiment(activity.Text); var sentimentScore = (sentiment?.Documents != null && sentiment.Documents.Any()) ? sentiment.Documents.First().Score : 1; // is a user frustrated or is their intention unclear return((sentimentScore <= 0.4) ? IntentProvider.GetIntent(AppId, "frustrated user").Respond(null, null, null) : IntentProvider.GetDefaultResponse(AppId)); }
public virtual ConversationResponse ProcessUserInput(IConversationContext context) { if (string.IsNullOrWhiteSpace(context.Result.Query) || context.Result == null) { return(IntentProvider.GetDefaultResponse(context.AppId)); } // gather data var intent = IntentProvider.GetTopScoringIntent(context); var conversation = context.GetCurrentConversation(); var isConfident = context.Result.TopScoringIntent.Score > ApiKeys.LuisIntentConfidenceThreshold; var hasValidIntent = intent != null && isConfident; var inConversation = conversation != null && !conversation.IsEnded; var requestedQuit = hasValidIntent && intent.KeyName.Equals(context.QuitIntentName); // if the user is trying to end or finish a conversation if (inConversation && requestedQuit) { return(EndCurrentConversation(context)); } // continue conversation if (inConversation) { return(HandleCurrentConversation(context)); } // is a user frustrated or is their intention unclear var sentimentScore = context.Result.SentimentAnalysis?.score ?? 1; return((sentimentScore <= 0.4) ? IntentProvider.GetIntent(context.AppId, context.FrustratedIntentName)?.Respond(null, null, null) ?? IntentProvider.GetDefaultResponse(context.AppId) : IntentProvider.GetDefaultResponse(context.AppId)); }
public ActionResult Post([FromBody] Activity activity) { var s = JsonConvert.SerializeObject(activity.ChannelData); var d = JsonConvert.DeserializeObject <List <string> >(s); ItemContextParameters parameters = (d.Any()) ? JsonConvert.DeserializeObject <ItemContextParameters>(d[0]) : new ItemContextParameters(); if (activity.Type != ActivityTypes.Message) { return(null); } var result = !string.IsNullOrWhiteSpace(activity.Text) ? LuisService.Query(ChatSettings.OleApplicationId, activity.Text, true) : null; var conversationContext = ConversationContextFactory.Create( ChatSettings.OleApplicationId, Translator.Text("Chat.Clear"), Translator.Text("Chat.ConfirmMessage"), "decision - yes", "decision - no", "frustrated", "profile user - quit", parameters, result); var conversation = conversationContext.GetCurrentConversation(); var inConversation = conversation?.IsEnded ?? false; var isQuestion = result.Query.Split(new char[] { ' ' }).Take(2).Any(a => QuestionWords.Contains(a)); var response = (!inConversation && isQuestion) ? IntentProvider.GetIntent(ChatSettings.OleApplicationId, "self - websearch").Respond(result, parameters, conversation); : LuisConversationService.ProcessUserInput(conversationContext); var newMessage = Regex.Replace(response.Message, "<.*?>", " "); var relativePath = $"temp\\ole-{CreateMD5Hash(newMessage)}.mp3"; var filePath = $"{Request.PhysicalApplicationPath}{relativePath}"; var locale = SpeechLocaleOptions.enUS; var voice = VoiceName.EnUsGuyNeural; var gender = GenderOptions.Male; var audioFormat = AudioOutputFormatOptions.Audio24Khz160KBitRateMonoMp3; SpeechService.TextToFile(newMessage, filePath, locale, voice, gender, audioFormat); var reply = activity.CreateReply(response.Message, "en-US"); reply.ChannelData = new ChannelData { Input = response.Input, Selections = response.Selections?.ToDictionary(a => a.Key, b => b.Value.DisplayName) ?? null, Action = response.Action, AudioFile = $"\\{relativePath}" }; return(Json(reply)); }