public override async Task <TextProcessingServiceRespone> Invork(TextRequest textRequest) { using (RabbitMqClient client = new RabbitMqClient("localhost")) { var time = DateTime.Now; string inputQueue = "RnnTalkService", outputQueue = "RnnTalkService"; TextProcessingServiceRespone respone = new TextProcessingServiceRespone { Id = textRequest.Id.Value, IntentName = "none", TextData = "" }; if (_language == Enums.Language.English) { inputQueue += "_input_en"; outputQueue += "_output_en"; } else if (_language == Enums.Language.Russian) { inputQueue += "_input_ru"; outputQueue += "_output_ru"; } DbClient db = new DbClient(); var context = await db.GetContext(textRequest.Id.Value); RnnTalkSystemRequest talkSystemRequest = new RnnTalkSystemRequest(); talkSystemRequest.Id = textRequest.Id; talkSystemRequest.TextData = textRequest.TextData; talkSystemRequest.TalkContext = context.TalkContext; talkSystemRequest.TalkReplicCount = context.TalkReplicCount; string responeRnn; responeRnn = await client.PostAsync(JsonConvert.SerializeObject(talkSystemRequest), inputQueue, outputQueue); var talkSystemRespone = JsonConvert.DeserializeObject <RnnTalkSystemRespone>(responeRnn); context.TalkContext = talkSystemRespone.TalkContext; context.TalkReplicCount = talkSystemRespone.TalkReplicCount; await db.AddOrUpdateContext(context); respone.TextData = talkSystemRespone.TextData; db.Dispose(); Log.LogInformation(textRequest.Id.Value, 0, this.GetType().ToString(), $"service end work in {(DateTime.Now - time).Milliseconds} ms"); return(respone); } }
public override async Task <TextProcessingServiceRespone> Invork(TextRequest textRequest) { var time = DateTime.Now; TextProcessingServiceRespone textProcessingServiceRespone = new TextProcessingServiceRespone() { Id = textRequest.Id.Value }; ApiAiSDK.Model.AIResponse aiResponse; ApiAiSDK.RequestExtras requestExtras; DbClient db = new DbClient(); var context = await db.GetContext(textRequest.Id.Value); if (context != null && !string.IsNullOrWhiteSpace(context.IntentContext)) { requestExtras = JsonConvert.DeserializeObject <ApiAiSDK.RequestExtras>(context.IntentContext); } else { requestExtras = new ApiAiSDK.RequestExtras(); } aiResponse = apiAi.TextRequest(textRequest.TextData, requestExtras); //TODO: Update Exceptions if (aiResponse == null) { throw new Exception("Invalid output message"); } requestExtras.Contexts = new List <ApiAiSDK.Model.AIContext>(); foreach (var outContext in aiResponse.Result.Contexts) { ApiAiSDK.Model.AIContext aIContext = new ApiAiSDK.Model.AIContext { Parameters = new Dictionary <string, string>(), Lifespan = outContext.Lifespan, Name = outContext.Name }; foreach (var param in outContext.Parameters) { string key = param.Key; string value; if (param.Value != null) { value = param.Value.ToString(); aIContext.Parameters.Add(key, value); } } requestExtras.Contexts.Add(aIContext); } if (requestExtras == null) { requestExtras = new ApiAiSDK.RequestExtras(); } if (context == null) { context = new DbModels.UserContext() { Id = textRequest.Id.Value, IntentContext = "", TalkContext = "", TalkReplicCount = 0 } } ; context.IntentContext = JsonConvert.SerializeObject(requestExtras); await db.AddOrUpdateContext(context); requestExtras = null; db.Dispose(); textProcessingServiceRespone.TextData = aiResponse.Result.Fulfillment.Speech; if (!string.IsNullOrWhiteSpace(aiResponse.Result.Action)) { foreach (var parametr in aiResponse.Result.Parameters) { if (!string.IsNullOrWhiteSpace(parametr.Value.ToString())) { textProcessingServiceRespone.Entities.Add(parametr.Key, parametr.Value.ToString()); if (parametr.Value.ToString().Contains("dress")) { textProcessingServiceRespone.WayPoint = "adidas"; } else if (parametr.Value.ToString().Contains("shop")) { textProcessingServiceRespone.WayPoint = "adidas"; } else if (parametr.Value.ToString().Contains("furniture")) { textProcessingServiceRespone.WayPoint = "adidas"; } else if (parametr.Value.ToString().ToLower().Contains("ikea")) { textProcessingServiceRespone.WayPoint = "adidas"; } else if (parametr.Value.ToString().ToLower().Contains("adidas")) { textProcessingServiceRespone.WayPoint = "adidas"; } } } if (aiResponse.Result.Action == "navigation_shops") { textProcessingServiceRespone.WayPoint = "adidas"; } if (aiResponse.Result.Action == "Cheese") { textProcessingServiceRespone.WayPoint = "cheese"; } textProcessingServiceRespone.IntentName = aiResponse.Result.Action; } else { textProcessingServiceRespone.IntentName = "none"; } //DEBUG INFO #region DebugInfo if (SHOW_DEBUG_INFO) { Console.WriteLine("Id: {0} | BOT PARAMS:", textRequest.Id.Value); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("Intent {0}", textProcessingServiceRespone.IntentName); Console.WriteLine("\tName\t\t\t|Value"); Console.ForegroundColor = ConsoleColor.DarkGreen; foreach (var parametr in textProcessingServiceRespone.Entities) { Console.WriteLine("\t{0,-23} |{1}", parametr.Key, parametr.Value); } Console.WriteLine(textProcessingServiceRespone.WayPoint); Console.ForegroundColor = ConsoleColor.Gray; /* * Console.WriteLine("User: "******"Masha: " + outputText); * Console.ForegroundColor = ConsoleColor.DarkMagenta; * Console.WriteLine("BOT CONTEXTS:"); * foreach (var context in aiResponse.Result.Contexts) * { * Console.ForegroundColor = ConsoleColor.DarkCyan; * Console.WriteLine("## {0}", context.Name); * Console.ForegroundColor = ConsoleColor.Gray; * Console.WriteLine("\tName\t\t\t|Value"); * Console.ForegroundColor = ConsoleColor.DarkGreen; * foreach (var parameter in context.Parameters) * Console.WriteLine("\t{0,-23} |{1}", parameter.Key, parameter.Value); * } * Console.ForegroundColor = ConsoleColor.Gray; * * Console.ForegroundColor = ConsoleColor.DarkMagenta; * Console.WriteLine("BOT PARAMS:"); * Console.ForegroundColor = ConsoleColor.Gray; * Console.WriteLine("\tName\t\t\t|Value"); * Console.ForegroundColor = ConsoleColor.DarkGreen; * foreach (var parametr in aiResponse.Result.Parameters) * { * Console.WriteLine("\t{0,-23} |{1}", parametr.Key, parametr.Value); * } * Console.ForegroundColor = ConsoleColor.Gray; */ } #endregion Log.LogInformation(textRequest.Id.Value, 0, this.GetType().ToString(), $"service end work in {(DateTime.Now - time).Milliseconds} ms"); //Console.WriteLine("WayPoint: " + textProcessingServiceRespone.WayPoint); return(textProcessingServiceRespone); } }