|
| 1 | +namespace BotSharp.Plugin.AudioHandler.Functions; |
| 2 | + |
| 3 | +public class ReadAudioFn : IFunctionCallback |
| 4 | +{ |
| 5 | + public string Name => "util-audio-handle_audio_request"; |
| 6 | + public string Indication => "Reading audio"; |
| 7 | + |
| 8 | + private readonly IServiceProvider _services; |
| 9 | + private readonly IFileStorageService _fileStorage; |
| 10 | + private readonly ILogger<ReadAudioFn> _logger; |
| 11 | + private readonly BotSharpOptions _options; |
| 12 | + private readonly AudioHandlerSettings _settings; |
| 13 | + |
| 14 | + private readonly IEnumerable<string> _audioContentTypes = new List<string> |
| 15 | + { |
| 16 | + AudioType.mp3.ToFileType(), |
| 17 | + AudioType.wav.ToFileType(), |
| 18 | + }; |
| 19 | + |
| 20 | + public ReadAudioFn( |
| 21 | + IServiceProvider services, |
| 22 | + ILogger<ReadAudioFn> logger, |
| 23 | + BotSharpOptions options, |
| 24 | + AudioHandlerSettings settings, |
| 25 | + IFileStorageService fileStorage) |
| 26 | + { |
| 27 | + _services = services; |
| 28 | + _logger = logger; |
| 29 | + _options = options; |
| 30 | + _settings = settings; |
| 31 | + _fileStorage = fileStorage; |
| 32 | + } |
| 33 | + |
| 34 | + public async Task<bool> Execute(RoleDialogModel message) |
| 35 | + { |
| 36 | + var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs, _options.JsonSerializerOptions); |
| 37 | + var conv = _services.GetRequiredService<IConversationService>(); |
| 38 | + var routingCtx = _services.GetRequiredService<IRoutingContext>(); |
| 39 | + |
| 40 | + var wholeDialogs = routingCtx.GetDialogs(); |
| 41 | + if (wholeDialogs.IsNullOrEmpty()) |
| 42 | + { |
| 43 | + wholeDialogs = conv.GetDialogHistory(); |
| 44 | + } |
| 45 | + |
| 46 | + var dialogs = AssembleFiles(conv.ConversationId, wholeDialogs); |
| 47 | + var response = await GetAudioTranscription(dialogs); |
| 48 | + message.Content = response; |
| 49 | + dialogs.ForEach(x => x.Files = null); |
| 50 | + return true; |
| 51 | + } |
| 52 | + |
| 53 | + private List<RoleDialogModel> AssembleFiles(string convId, List<RoleDialogModel> dialogs) |
| 54 | + { |
| 55 | + if (dialogs.IsNullOrEmpty()) |
| 56 | + { |
| 57 | + return new List<RoleDialogModel>(); |
| 58 | + } |
| 59 | + |
| 60 | + var messageId = dialogs.Select(x => x.MessageId).Distinct().ToList(); |
| 61 | + var audioFiles = _fileStorage.GetMessageFiles(convId, messageId, options: new() |
| 62 | + { |
| 63 | + Sources = [FileSource.User], |
| 64 | + ContentTypes = _audioContentTypes |
| 65 | + }); |
| 66 | + |
| 67 | + foreach (var dialog in dialogs) |
| 68 | + { |
| 69 | + var found = audioFiles.Where(x => x.MessageId == dialog.MessageId |
| 70 | + && x.FileSource.IsEqualTo(FileSource.User)).ToList(); |
| 71 | + |
| 72 | + if (found.IsNullOrEmpty() || !dialog.IsFromUser) |
| 73 | + { |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + dialog.Files = found.Select(x => new BotSharpFile |
| 78 | + { |
| 79 | + ContentType = x.ContentType, |
| 80 | + FileUrl = x.FileUrl, |
| 81 | + FileStorageUrl = x.FileStorageUrl |
| 82 | + }).ToList(); |
| 83 | + } |
| 84 | + |
| 85 | + return dialogs; |
| 86 | + } |
| 87 | + |
| 88 | + private async Task<string> GetAudioTranscription(List<RoleDialogModel> dialogs) |
| 89 | + { |
| 90 | + var audioCompletion = PrepareModel(); |
| 91 | + var dialog = dialogs.Where(x => !x.Files.IsNullOrEmpty()).LastOrDefault(); |
| 92 | + var transcripts = new List<string>(); |
| 93 | + |
| 94 | + if (dialog != null) |
| 95 | + { |
| 96 | + foreach (var file in dialog.Files) |
| 97 | + { |
| 98 | + if (string.IsNullOrWhiteSpace(file?.FileStorageUrl)) |
| 99 | + { |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + var extension = Path.GetExtension(file.FileStorageUrl); |
| 104 | + var fileName = Path.GetFileName(file.FileStorageUrl); |
| 105 | + if (!VerifyAudioFileType(fileName)) |
| 106 | + { |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + var binary = _fileStorage.GetFileBytes(file.FileStorageUrl); |
| 111 | + using var stream = binary.ToStream(); |
| 112 | + stream.Position = 0; |
| 113 | + |
| 114 | + var result = await audioCompletion.TranscriptTextAsync(stream, fileName); |
| 115 | + transcripts.Add(result); |
| 116 | + stream.Close(); |
| 117 | + await Task.Delay(100); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + if (transcripts.IsNullOrEmpty()) |
| 123 | + { |
| 124 | + var msg = "No audio is found in the chat."; |
| 125 | + _logger.LogWarning(msg); |
| 126 | + transcripts.Add(msg); |
| 127 | + } |
| 128 | + |
| 129 | + return string.Join("\r\n\r\n", transcripts); |
| 130 | + } |
| 131 | + |
| 132 | + private IAudioTranscription PrepareModel() |
| 133 | + { |
| 134 | + var (provider, model) = GetLlmProviderModel(); |
| 135 | + return CompletionProvider.GetAudioTranscriber(_services, provider: provider, model: model); |
| 136 | + } |
| 137 | + |
| 138 | + private bool VerifyAudioFileType(string fileName) |
| 139 | + { |
| 140 | + var extension = Path.GetExtension(fileName).TrimStart('.').ToLower(); |
| 141 | + return Enum.TryParse<AudioType>(extension, out _) |
| 142 | + || !string.IsNullOrEmpty(FileUtility.GetFileContentType(fileName)); |
| 143 | + } |
| 144 | + |
| 145 | + private (string, string) GetLlmProviderModel() |
| 146 | + { |
| 147 | + var state = _services.GetRequiredService<IConversationStateService>(); |
| 148 | + var llmProviderService = _services.GetRequiredService<ILlmProviderService>(); |
| 149 | + |
| 150 | + var provider = state.GetState("audio_read_llm_provider"); |
| 151 | + var model = state.GetState("audio_read_llm_provider"); |
| 152 | + |
| 153 | + if (!string.IsNullOrEmpty(provider) && !string.IsNullOrEmpty(model)) |
| 154 | + { |
| 155 | + return (provider, model); |
| 156 | + } |
| 157 | + |
| 158 | + provider = _settings?.Audio?.Reading?.LlmProvider; |
| 159 | + model = _settings?.Audio?.Reading?.LlmModel; |
| 160 | + |
| 161 | + if (!string.IsNullOrEmpty(provider) && !string.IsNullOrEmpty(model)) |
| 162 | + { |
| 163 | + return (provider, model); |
| 164 | + } |
| 165 | + |
| 166 | + provider = "openai"; |
| 167 | + model = "gpt-4o-mini-transcribe"; |
| 168 | + |
| 169 | + return (provider, model); |
| 170 | + } |
| 171 | +} |
0 commit comments