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