From b77fb87a5edda6a4e340768898c927c9fe428566 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=93=E7=A3=8A?= Date: Fri, 18 Oct 2024 11:08:55 +0800 Subject: [PATCH] Optimize TwilioMessageQueueService.cs Optimize TwilioMessageQueueService.cs --- .../Services/TwilioMessageQueueService.cs | 72 ++++++++++--------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs b/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs index e3476ea2..33cf800d 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs @@ -28,22 +28,19 @@ namespace BotSharp.Plugin.Twilio.Services await foreach (var message in _queue.Reader.ReadAllAsync(stoppingToken)) { await _throttler.WaitAsync(stoppingToken); - _ = Task.Run(async () => + try { - try - { - Console.WriteLine($"Start processing {message}."); - await ProcessUserMessageAsync(message); - } - catch (Exception ex) - { - Console.WriteLine($"Processing {message} failed due to {ex.Message}."); - } - finally - { - _throttler.Release(); - } - }); + Console.WriteLine($"Start processing {message}."); + await ProcessUserMessageAsync(message); + } + catch (Exception ex) + { + Console.WriteLine($"Processing {message} failed due to {ex.Message}."); + } + finally + { + _throttler.Release(); + } } } @@ -66,19 +63,7 @@ namespace BotSharp.Plugin.Twilio.Services var sessionManager = sp.GetRequiredService(); var progressService = sp.GetRequiredService(); InitProgressService(message, sessionManager, progressService); - - routing.Context.SetMessageId(message.ConversationId, inputMsg.MessageId); - var states = new List - { - new MessageState("channel", ConversationChannel.Phone), - new MessageState("calling_phone", message.From) - }; - - foreach (var kvp in message.States) - { - states.Add(new MessageState(kvp.Key, kvp.Value)); - } - conv.SetConversationId(message.ConversationId, states); + InitConversation(message, inputMsg, conv, routing); var result = await conv.SendMessage(config.AgentId, inputMsg, @@ -94,13 +79,36 @@ namespace BotSharp.Plugin.Twilio.Services }; } ); + reply.SpeechFileName = await GetReplySpeechFileName(message.ConversationId, reply, sp); + reply.Hints = GetHints(reply); ; + reply.Content = null; + await sessionManager.SetAssistantReplyAsync(message.ConversationId, message.SeqNumber, reply); + } + private static void InitConversation(CallerMessage message, RoleDialogModel inputMsg, IConversationService conv, IRoutingService routing) + { + routing.Context.SetMessageId(message.ConversationId, inputMsg.MessageId); + var states = new List + { + new("channel", ConversationChannel.Phone), + new("calling_phone", message.From) + }; + states.AddRange(message.States.Select(kvp => new MessageState(kvp.Key, kvp.Value))); + conv.SetConversationId(message.ConversationId, states); + } + + private static async Task GetReplySpeechFileName(string conversationId, AssistantMessage reply, IServiceProvider sp) + { var completion = CompletionProvider.GetAudioCompletion(sp, "openai", "tts-1"); var fileStorage = sp.GetRequiredService(); var data = await completion.GenerateAudioFromTextAsync(reply.Content); var fileName = $"reply_{reply.MessageId}.mp3"; - fileStorage.SaveSpeechFile(message.ConversationId, fileName, data); - reply.SpeechFileName = fileName; + fileStorage.SaveSpeechFile(conversationId, fileName, data); + return fileName; + } + + private static string GetHints(AssistantMessage reply) + { var phrases = reply.Content.Split(',', StringSplitOptions.RemoveEmptyEntries); int capcity = 100; var hints = new List(capcity); @@ -122,9 +130,7 @@ namespace BotSharp.Plugin.Twilio.Services } // add frequency short words hints.AddRange(["yes", "no", "correct", "right"]); - reply.Hints = string.Join(", ", hints.Select(x => x.ToLower()).Distinct().Reverse()); - reply.Content = null; - await sessionManager.SetAssistantReplyAsync(message.ConversationId, message.SeqNumber, reply); + return string.Join(", ", hints.Select(x => x.ToLower()).Distinct().Reverse()); } private static void InitProgressService(CallerMessage message, ITwilioSessionManager sessionManager, IConversationProgressService progressService)