openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Audio/AudioClient.cs
327lines · modecode
| 1 | using System; |
| 2 | using System.ClientModel; |
| 3 | using System.ClientModel.Primitives; |
| 4 | using System.IO; |
| 5 | using System.Threading; |
| 6 | using System.Threading.Tasks; |
| 7 | |
| 8 | namespace OpenAI.Audio; |
| 9 | |
| 10 | // CUSTOM: |
| 11 | // - Renamed. |
| 12 | // - Suppressed constructor that takes endpoint parameter; endpoint is now a property in the options class. |
| 13 | // - Suppressed methods that only take the options parameter. |
| 14 | /// <summary> The service client for OpenAI audio operations. </summary> |
| 15 | [CodeGenClient("Audio")] |
| 16 | [CodeGenSuppress("AudioClient", typeof(ClientPipeline), typeof(ApiKeyCredential), typeof(Uri))] |
| 17 | [CodeGenSuppress("CreateSpeechAsync", typeof(SpeechGenerationOptions), typeof(CancellationToken))] |
| 18 | [CodeGenSuppress("CreateSpeech", typeof(SpeechGenerationOptions), typeof(CancellationToken))] |
| 19 | public partial class AudioClient |
| 20 | { |
| 21 | private readonly string _model; |
| 22 | |
| 23 | // CUSTOM: Added as a convenience. |
| 24 | /// <summary> Initializes a new instance of <see cref="AudioClient"/>. </summary> |
| 25 | /// <param name="model"> The name of the model to use in requests sent to the service. To learn more about the available models, see <see href="https://platform.openai.com/docs/models"/>. </param> |
| 26 | /// <param name="apiKey"> The API key to authenticate with the service. </param> |
| 27 | /// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="apiKey"/> is null. </exception> |
| 28 | /// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception> |
| 29 | public AudioClient(string model, string apiKey) : this(model, new ApiKeyCredential(apiKey), new OpenAIClientOptions()) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | // CUSTOM: |
| 34 | // - Added `model` parameter. |
| 35 | // - Used a custom pipeline. |
| 36 | // - Demoted the endpoint parameter to be a property in the options class. |
| 37 | /// <summary> Initializes a new instance of <see cref="AudioClient"/>. </summary> |
| 38 | /// <param name="model"> The name of the model to use in requests sent to the service. To learn more about the available models, see <see href="https://platform.openai.com/docs/models"/>. </param> |
| 39 | /// <param name="credential"> The API key to authenticate with the service. </param> |
| 40 | /// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="credential"/> is null. </exception> |
| 41 | /// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception> |
| 42 | public AudioClient(string model, ApiKeyCredential credential) : this(model, credential, new OpenAIClientOptions()) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | // CUSTOM: |
| 47 | // - Added `model` parameter. |
| 48 | // - Used a custom pipeline. |
| 49 | // - Demoted the endpoint parameter to be a property in the options class. |
| 50 | /// <summary> Initializes a new instance of <see cref="AudioClient"/>. </summary> |
| 51 | /// <param name="model"> The name of the model to use in requests sent to the service. To learn more about the available models, see <see href="https://platform.openai.com/docs/models"/>. </param> |
| 52 | /// <param name="credential"> The API key to authenticate with the service. </param> |
| 53 | /// <param name="options"> The options to configure the client. </param> |
| 54 | /// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="credential"/> is null. </exception> |
| 55 | /// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception> |
| 56 | public AudioClient(string model, ApiKeyCredential credential, OpenAIClientOptions options) |
| 57 | { |
| 58 | Argument.AssertNotNullOrEmpty(model, nameof(model)); |
| 59 | Argument.AssertNotNull(credential, nameof(credential)); |
| 60 | options ??= new OpenAIClientOptions(); |
| 61 | |
| 62 | _model = model; |
| 63 | Pipeline = OpenAIClient.CreatePipeline(credential, options); |
| 64 | _endpoint = OpenAIClient.GetEndpoint(options); |
| 65 | } |
| 66 | |
| 67 | // CUSTOM: |
| 68 | // - Added `model` parameter. |
| 69 | // - Used a custom pipeline. |
| 70 | // - Demoted the endpoint parameter to be a property in the options class. |
| 71 | // - Made protected. |
| 72 | /// <summary> Initializes a new instance of <see cref="AudioClient"/>. </summary> |
| 73 | /// <param name="pipeline"> The HTTP pipeline to send and receive REST requests and responses. </param> |
| 74 | /// <param name="model"> The name of the model to use in requests sent to the service. To learn more about the available models, see <see href="https://platform.openai.com/docs/models"/>. </param> |
| 75 | /// <param name="options"> The options to configure the client. </param> |
| 76 | /// <exception cref="ArgumentNullException"> <paramref name="pipeline"/> or <paramref name="model"/> is null. </exception> |
| 77 | /// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception> |
| 78 | protected internal AudioClient(ClientPipeline pipeline, string model, OpenAIClientOptions options) |
| 79 | { |
| 80 | Argument.AssertNotNull(pipeline, nameof(pipeline)); |
| 81 | Argument.AssertNotNullOrEmpty(model, nameof(model)); |
| 82 | options ??= new OpenAIClientOptions(); |
| 83 | |
| 84 | _model = model; |
| 85 | Pipeline = pipeline; |
| 86 | _endpoint = OpenAIClient.GetEndpoint(options); |
| 87 | } |
| 88 | |
| 89 | #region GenerateSpeech |
| 90 | |
| 91 | /// <summary> Generates a life-like, spoken audio recording of the input text. </summary> |
| 92 | /// <remarks> |
| 93 | /// The default format of the generated audio is <see cref="GeneratedSpeechFormat.Mp3"/> unless otherwise specified |
| 94 | /// via <see cref="SpeechGenerationOptions.ResponseFormat"/>. |
| 95 | /// </remarks> |
| 96 | /// <param name="text"> The text to generate audio for. </param> |
| 97 | /// <param name="voice"> The voice to use in the generated audio. </param> |
| 98 | /// <param name="options"> The options to configure the audio generation. </param> |
| 99 | /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param> |
| 100 | /// <exception cref="ArgumentNullException"> <paramref name="text"/> is null. </exception> |
| 101 | /// <returns> The generated audio in the specified output format. </returns> |
| 102 | public virtual async Task<ClientResult<BinaryData>> GenerateSpeechAsync(string text, GeneratedSpeechVoice voice, SpeechGenerationOptions options = null, CancellationToken cancellationToken = default) |
| 103 | { |
| 104 | Argument.AssertNotNull(text, nameof(text)); |
| 105 | |
| 106 | options ??= new(); |
| 107 | CreateSpeechGenerationOptions(text, voice, ref options); |
| 108 | |
| 109 | using BinaryContent content = options; |
| 110 | ClientResult result = await GenerateSpeechAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); |
| 111 | return ClientResult.FromValue(result.GetRawResponse().Content, result.GetRawResponse()); |
| 112 | } |
| 113 | |
| 114 | /// <summary> Generates a life-like, spoken audio recording of the input text. </summary> |
| 115 | /// <remarks> |
| 116 | /// The default format of the generated audio is <see cref="GeneratedSpeechFormat.Mp3"/> unless otherwise specified |
| 117 | /// via <see cref="SpeechGenerationOptions.ResponseFormat"/>. |
| 118 | /// </remarks> |
| 119 | /// <param name="text"> The text to generate audio for. </param> |
| 120 | /// <param name="voice"> The voice to use in the generated audio. </param> |
| 121 | /// <param name="options"> The options to configure the audio generation. </param> |
| 122 | /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param> |
| 123 | /// <exception cref="ArgumentNullException"> <paramref name="text"/> is null. </exception> |
| 124 | /// <returns> The generated audio in the specified output format. </returns> |
| 125 | public virtual ClientResult<BinaryData> GenerateSpeech(string text, GeneratedSpeechVoice voice, SpeechGenerationOptions options = null, CancellationToken cancellationToken = default) |
| 126 | { |
| 127 | Argument.AssertNotNull(text, nameof(text)); |
| 128 | |
| 129 | options ??= new(); |
| 130 | CreateSpeechGenerationOptions(text, voice, ref options); |
| 131 | |
| 132 | using BinaryContent content = options; |
| 133 | ClientResult result = GenerateSpeech(content, cancellationToken.ToRequestOptions()); ; |
| 134 | return ClientResult.FromValue(result.GetRawResponse().Content, result.GetRawResponse()); |
| 135 | } |
| 136 | |
| 137 | #endregion |
| 138 | |
| 139 | #region TranscribeAudio |
| 140 | |
| 141 | /// <summary> Transcribes the input audio. </summary> |
| 142 | /// <param name="audio"> The audio stream to transcribe. </param> |
| 143 | /// <param name="audioFilename"> |
| 144 | /// The filename associated with the audio stream. The filename's extension (for example: .mp3) will be used to |
| 145 | /// validate the format of the input audio. The request may fail if the filename's extension and the actual |
| 146 | /// format of the input audio do not match. |
| 147 | /// </param> |
| 148 | /// <param name="options"> The options to configure the audio transcription. </param> |
| 149 | /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param> |
| 150 | /// <exception cref="ArgumentNullException"> <paramref name="audio"/> or <paramref name="audioFilename"/> is null. </exception> |
| 151 | /// <exception cref="ArgumentException"> <paramref name="audioFilename"/> is an empty string, and was expected to be non-empty. </exception> |
| 152 | public virtual async Task<ClientResult<AudioTranscription>> TranscribeAudioAsync(Stream audio, string audioFilename, AudioTranscriptionOptions options = null, CancellationToken cancellationToken = default) |
| 153 | { |
| 154 | Argument.AssertNotNull(audio, nameof(audio)); |
| 155 | Argument.AssertNotNullOrEmpty(audioFilename, nameof(audioFilename)); |
| 156 | |
| 157 | options ??= new(); |
| 158 | CreateAudioTranscriptionOptions(audio, audioFilename, ref options); |
| 159 | |
| 160 | using MultipartFormDataBinaryContent content = options.ToMultipartContent(audio, audioFilename); |
| 161 | ClientResult result = await TranscribeAudioAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false); |
| 162 | return ClientResult.FromValue(AudioTranscription.FromResponse(result.GetRawResponse()), result.GetRawResponse()); |
| 163 | } |
| 164 | |
| 165 | /// <summary> Transcribes the input audio. </summary> |
| 166 | /// <param name="audio"> The audio stream to transcribe. </param> |
| 167 | /// <param name="audioFilename"> |
| 168 | /// The filename associated with the audio stream. The filename's extension (for example: .mp3) will be used to |
| 169 | /// validate the format of the input audio. The request may fail if the filename's extension and the actual |
| 170 | /// format of the input audio do not match. |
| 171 | /// </param> |
| 172 | /// <param name="options"> The options to configure the audio transcription. </param> |
| 173 | /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param> |
| 174 | /// <exception cref="ArgumentNullException"> <paramref name="audio"/> or <paramref name="audioFilename"/> is null. </exception> |
| 175 | /// <exception cref="ArgumentException"> <paramref name="audioFilename"/> is an empty string, and was expected to be non-empty. </exception> |
| 176 | public virtual ClientResult<AudioTranscription> TranscribeAudio(Stream audio, string audioFilename, AudioTranscriptionOptions options = null, CancellationToken cancellationToken = default) |
| 177 | { |
| 178 | Argument.AssertNotNull(audio, nameof(audio)); |
| 179 | Argument.AssertNotNullOrEmpty(audioFilename, nameof(audioFilename)); |
| 180 | |
| 181 | options ??= new(); |
| 182 | CreateAudioTranscriptionOptions(audio, audioFilename, ref options); |
| 183 | |
| 184 | using MultipartFormDataBinaryContent content = options.ToMultipartContent(audio, audioFilename); |
| 185 | ClientResult result = TranscribeAudio(content, content.ContentType, cancellationToken.ToRequestOptions()); |
| 186 | return ClientResult.FromValue(AudioTranscription.FromResponse(result.GetRawResponse()), result.GetRawResponse()); |
| 187 | } |
| 188 | |
| 189 | /// <summary> Transcribes the input audio. </summary> |
| 190 | /// <param name="audioFilePath"> |
| 191 | /// The path of the audio file to transcribe. The provided file path's extension (for example: .mp3) will be |
| 192 | /// used to validate the format of the input audio. The request may fail if the file path's extension and the |
| 193 | /// actual format of the input audio do not match. |
| 194 | /// </param> |
| 195 | /// <param name="options"> The options to configure the audio transcription. </param> |
| 196 | /// <exception cref="ArgumentNullException"> <paramref name="audioFilePath"/> is null. </exception> |
| 197 | /// <exception cref="ArgumentException"> <paramref name="audioFilePath"/> is an empty string, and was expected to be non-empty. </exception> |
| 198 | public virtual async Task<ClientResult<AudioTranscription>> TranscribeAudioAsync(string audioFilePath, AudioTranscriptionOptions options = null) |
| 199 | { |
| 200 | Argument.AssertNotNullOrEmpty(audioFilePath, nameof(audioFilePath)); |
| 201 | |
| 202 | using FileStream audioStream = File.OpenRead(audioFilePath); |
| 203 | return await TranscribeAudioAsync(audioStream, audioFilePath, options).ConfigureAwait(false); |
| 204 | } |
| 205 | |
| 206 | /// <summary> Transcribes the input audio. </summary> |
| 207 | /// <param name="audioFilePath"> |
| 208 | /// The path of the audio file to transcribe. The provided file path's extension (for example: .mp3) will be |
| 209 | /// used to validate the format of the input audio. The request may fail if the file path's extension and the |
| 210 | /// actual format of the input audio do not match. |
| 211 | /// </param> |
| 212 | /// <param name="options"> The options to configure the audio transcription. </param> |
| 213 | /// <exception cref="ArgumentNullException"> <paramref name="audioFilePath"/> is null. </exception> |
| 214 | /// <exception cref="ArgumentException"> <paramref name="audioFilePath"/> is an empty string, and was expected to be non-empty. </exception> |
| 215 | public virtual ClientResult<AudioTranscription> TranscribeAudio(string audioFilePath, AudioTranscriptionOptions options = null) |
| 216 | { |
| 217 | Argument.AssertNotNullOrEmpty(audioFilePath, nameof(audioFilePath)); |
| 218 | |
| 219 | using FileStream audioStream = File.OpenRead(audioFilePath); |
| 220 | return TranscribeAudio(audioStream, audioFilePath, options); |
| 221 | } |
| 222 | |
| 223 | #endregion |
| 224 | |
| 225 | #region TranslateAudio |
| 226 | |
| 227 | /// <summary> Translates the input audio into English. </summary> |
| 228 | /// <param name="audio"> The audio stream to translate. </param> |
| 229 | /// <param name="audioFilename"> |
| 230 | /// The filename associated with the audio stream. The filename's extension (for example: .mp3) will be used to |
| 231 | /// validate the format of the input audio. The request may fail if the filename's extension and the actual |
| 232 | /// format of the input audio do not match. |
| 233 | /// </param> |
| 234 | /// <param name="options"> The options to configure the audio translation. </param> |
| 235 | /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param> |
| 236 | /// <exception cref="ArgumentNullException"> <paramref name="audio"/> or <paramref name="audioFilename"/> is null. </exception> |
| 237 | /// <exception cref="ArgumentException"> <paramref name="audioFilename"/> is an empty string, and was expected to be non-empty. </exception> |
| 238 | public virtual async Task<ClientResult<AudioTranslation>> TranslateAudioAsync(Stream audio, string audioFilename, AudioTranslationOptions options = null, CancellationToken cancellationToken = default) |
| 239 | { |
| 240 | Argument.AssertNotNull(audio, nameof(audio)); |
| 241 | Argument.AssertNotNullOrEmpty(audioFilename, nameof(audioFilename)); |
| 242 | |
| 243 | options ??= new(); |
| 244 | CreateAudioTranslationOptions(audio, audioFilename, ref options); |
| 245 | |
| 246 | using MultipartFormDataBinaryContent content = options.ToMultipartContent(audio, audioFilename); |
| 247 | ClientResult result = await TranslateAudioAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false); |
| 248 | return ClientResult.FromValue(AudioTranslation.FromResponse(result.GetRawResponse()), result.GetRawResponse()); |
| 249 | } |
| 250 | |
| 251 | /// <summary> Translates the input audio into English. </summary> |
| 252 | /// <param name="audio"> The audio stream to translate. </param> |
| 253 | /// <param name="audioFilename"> |
| 254 | /// The filename associated with the audio stream. The filename's extension (for example: .mp3) will be used to |
| 255 | /// validate the format of the input audio. The request may fail if the filename's extension and the actual |
| 256 | /// format of the input audio do not match. |
| 257 | /// </param> |
| 258 | /// <param name="options"> The options to configure the audio translation. </param> |
| 259 | /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param> |
| 260 | /// <exception cref="ArgumentNullException"> <paramref name="audio"/> or <paramref name="audioFilename"/> is null. </exception> |
| 261 | /// <exception cref="ArgumentException"> <paramref name="audioFilename"/> is an empty string, and was expected to be non-empty. </exception> |
| 262 | public virtual ClientResult<AudioTranslation> TranslateAudio(Stream audio, string audioFilename, AudioTranslationOptions options = null, CancellationToken cancellationToken = default) |
| 263 | { |
| 264 | Argument.AssertNotNull(audio, nameof(audio)); |
| 265 | Argument.AssertNotNullOrEmpty(audioFilename, nameof(audioFilename)); |
| 266 | |
| 267 | options ??= new(); |
| 268 | CreateAudioTranslationOptions(audio, audioFilename, ref options); |
| 269 | |
| 270 | using MultipartFormDataBinaryContent content = options.ToMultipartContent(audio, audioFilename); |
| 271 | ClientResult result = TranslateAudio(content, content.ContentType, cancellationToken.ToRequestOptions()); |
| 272 | return ClientResult.FromValue(AudioTranslation.FromResponse(result.GetRawResponse()), result.GetRawResponse()); |
| 273 | } |
| 274 | |
| 275 | /// <summary> Translates the input audio into English. </summary> |
| 276 | /// <param name="audioFilePath"> |
| 277 | /// The path of the audio file to translate. The provided file path's extension (for example: .mp3) will be |
| 278 | /// used to validate the format of the input audio. The request may fail if the file path's extension and the |
| 279 | /// actual format of the input audio do not match. |
| 280 | /// </param> |
| 281 | /// <param name="options"> The options to configure the audio translation. </param> |
| 282 | /// <exception cref="ArgumentNullException"> <paramref name="audioFilePath"/> was null. </exception> |
| 283 | /// <exception cref="ArgumentException"> <paramref name="audioFilePath"/> is an empty string, and was expected to be non-empty. </exception> |
| 284 | public virtual ClientResult<AudioTranslation> TranslateAudio(string audioFilePath, AudioTranslationOptions options = null) |
| 285 | { |
| 286 | Argument.AssertNotNullOrEmpty(audioFilePath, nameof(audioFilePath)); |
| 287 | |
| 288 | using FileStream audioStream = File.OpenRead(audioFilePath); |
| 289 | return TranslateAudio(audioStream, audioFilePath, options); |
| 290 | } |
| 291 | |
| 292 | /// <summary> Translates the input audio into English. </summary> |
| 293 | /// <param name="audioFilePath"> |
| 294 | /// The path of the audio file to translate. The provided file path's extension (for example: .mp3) will be |
| 295 | /// used to validate the format of the input audio. The request may fail if the file path's extension and the |
| 296 | /// actual format of the input audio do not match. |
| 297 | /// </param> |
| 298 | /// <param name="options"> The options to configure the audio translation. </param> |
| 299 | /// <exception cref="ArgumentNullException"> <paramref name="audioFilePath"/> was null. </exception> |
| 300 | /// <exception cref="ArgumentException"> <paramref name="audioFilePath"/> is an empty string, and was expected to be non-empty. </exception> |
| 301 | public virtual async Task<ClientResult<AudioTranslation>> TranslateAudioAsync(string audioFilePath, AudioTranslationOptions options = null) |
| 302 | { |
| 303 | Argument.AssertNotNull(audioFilePath, nameof(audioFilePath)); |
| 304 | |
| 305 | using FileStream audioStream = File.OpenRead(audioFilePath); |
| 306 | return await TranslateAudioAsync(audioStream, audioFilePath, options); |
| 307 | } |
| 308 | |
| 309 | #endregion |
| 310 | |
| 311 | private void CreateSpeechGenerationOptions(string text, GeneratedSpeechVoice voice, ref SpeechGenerationOptions options) |
| 312 | { |
| 313 | options.Input = text; |
| 314 | options.Voice = voice; |
| 315 | options.Model = _model; |
| 316 | } |
| 317 | |
| 318 | private void CreateAudioTranscriptionOptions(Stream audio, string audioFilename, ref AudioTranscriptionOptions options) |
| 319 | { |
| 320 | options.Model = _model; |
| 321 | } |
| 322 | |
| 323 | private void CreateAudioTranslationOptions(Stream audio, string audioFilename, ref AudioTranslationOptions options) |
| 324 | { |
| 325 | options.Model = _model; |
| 326 | } |
| 327 | } |
| 328 | |