openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.3.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/Custom/Audio/AudioClient.cs

462lines · modecode

1using System;
2using System.ClientModel;
3using System.ClientModel.Primitives;
4using System.Diagnostics.CodeAnalysis;
5using System.IO;
6using System.Threading;
7using System.Threading.Tasks;
8
9namespace OpenAI.Audio;
10
11// CUSTOM:
12// - Renamed.
13// - Suppressed constructor that takes endpoint parameter; endpoint is now a property in the options class.
14// - Suppressed methods that only take the options parameter.
15/// <summary> The service client for OpenAI audio operations. </summary>
16[CodeGenType("Audio")]
17[CodeGenSuppress("AudioClient", typeof(ClientPipeline), typeof(Uri))]
18[CodeGenSuppress("GenerateSpeechAsync", typeof(SpeechGenerationOptions), typeof(CancellationToken))]
19[CodeGenSuppress("GenerateSpeech", typeof(SpeechGenerationOptions), typeof(CancellationToken))]
20public partial class AudioClient
21{
22 private readonly string _model;
23
24 // CUSTOM: Added as a convenience.
25 /// <summary> Initializes a new instance of <see cref="AudioClient"/>. </summary>
26 /// <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>
27 /// <param name="apiKey"> The API key to authenticate with the service. </param>
28 /// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="apiKey"/> is null. </exception>
29 /// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
30 public AudioClient(string model, string apiKey) : this(model, new ApiKeyCredential(apiKey), new OpenAIClientOptions())
31 {
32 }
33
34 // CUSTOM:
35 // - Added `model` parameter.
36 // - Used a custom pipeline.
37 // - Demoted the endpoint parameter to be a property in the options class.
38 /// <summary> Initializes a new instance of <see cref="AudioClient"/>. </summary>
39 /// <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>
40 /// <param name="credential"> The API key to authenticate with the service. </param>
41 /// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="credential"/> is null. </exception>
42 /// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
43 public AudioClient(string model, ApiKeyCredential credential) : this(model, credential, new OpenAIClientOptions())
44 {
45 }
46
47 // CUSTOM:
48 // - Added `model` parameter.
49 // - Used a custom pipeline.
50 // - Demoted the endpoint parameter to be a property in the options class.
51 /// <summary> Initializes a new instance of <see cref="AudioClient"/>. </summary>
52 /// <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>
53 /// <param name="credential"> The API key to authenticate with the service. </param>
54 /// <param name="options"> The options to configure the client. </param>
55 /// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="credential"/> is null. </exception>
56 /// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
57 public AudioClient(string model, ApiKeyCredential credential, OpenAIClientOptions options) : this(model, OpenAIClient.CreateApiKeyAuthenticationPolicy(credential), options)
58 {
59 }
60
61 // CUSTOM: Added as a convenience.
62 /// <summary> Initializes a new instance of <see cref="AudioClient"/>. </summary>
63 /// <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>
64 /// <param name="authenticationPolicy"> The authentication policy used to authenticate with the service. </param>
65 /// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="authenticationPolicy"/> is null. </exception>
66 /// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
67 [Experimental("OPENAI001")]
68 public AudioClient(string model, AuthenticationPolicy authenticationPolicy) : this(model, authenticationPolicy, new OpenAIClientOptions())
69 {
70 }
71
72 // CUSTOM: Added as a convenience.
73 /// <summary> Initializes a new instance of <see cref="AudioClient"/>. </summary>
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="authenticationPolicy"> The authentication policy used to authenticate with the service. </param>
76 /// <param name="options"> The options to configure the client. </param>
77 /// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="authenticationPolicy"/> is null. </exception>
78 /// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
79 [Experimental("OPENAI001")]
80 public AudioClient(string model, AuthenticationPolicy authenticationPolicy, OpenAIClientOptions options)
81 {
82 Argument.AssertNotNullOrEmpty(model, nameof(model));
83 Argument.AssertNotNull(authenticationPolicy, nameof(authenticationPolicy));
84 options ??= new OpenAIClientOptions();
85
86 _model = model;
87 Pipeline = OpenAIClient.CreatePipeline(authenticationPolicy, options);
88 _endpoint = OpenAIClient.GetEndpoint(options);
89 }
90
91 // CUSTOM:
92 // - Added `model` parameter.
93 // - Used a custom pipeline.
94 // - Demoted the endpoint parameter to be a property in the options class.
95 // - Made protected.
96 /// <summary> Initializes a new instance of <see cref="AudioClient"/>. </summary>
97 /// <param name="pipeline"> The HTTP pipeline to send and receive REST requests and responses. </param>
98 /// <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>
99 /// <param name="options"> The options to configure the client. </param>
100 /// <exception cref="ArgumentNullException"> <paramref name="pipeline"/> or <paramref name="model"/> is null. </exception>
101 /// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
102 protected internal AudioClient(ClientPipeline pipeline, string model, OpenAIClientOptions options)
103 {
104 Argument.AssertNotNull(pipeline, nameof(pipeline));
105 Argument.AssertNotNullOrEmpty(model, nameof(model));
106 options ??= new OpenAIClientOptions();
107
108 _model = model;
109 Pipeline = pipeline;
110 _endpoint = OpenAIClient.GetEndpoint(options);
111 }
112
113 /// <summary>
114 /// Gets the name of the model used in requests sent to the service.
115 /// </summary>
116 [Experimental("OPENAI001")]
117 public string Model => _model;
118
119 #region GenerateSpeech
120
121 /// <summary> Generates a life-like, spoken audio recording of the input text. </summary>
122 /// <remarks>
123 /// The default format of the generated audio is <see cref="GeneratedSpeechFormat.Mp3"/> unless otherwise specified
124 /// via <see cref="SpeechGenerationOptions.ResponseFormat"/>.
125 /// </remarks>
126 /// <param name="text"> The text to generate audio for. </param>
127 /// <param name="voice"> The voice to use in the generated audio. </param>
128 /// <param name="options"> The options to configure the audio generation. </param>
129 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
130 /// <exception cref="ArgumentNullException"> <paramref name="text"/> is null. </exception>
131 /// <returns> The generated audio in the specified output format. </returns>
132 public virtual async Task<ClientResult<BinaryData>> GenerateSpeechAsync(string text, GeneratedSpeechVoice voice, SpeechGenerationOptions options = null, CancellationToken cancellationToken = default)
133 {
134 Argument.AssertNotNull(text, nameof(text));
135
136 options ??= new();
137 CreateSpeechGenerationOptions(text, voice, ref options);
138
139 using BinaryContent content = options.ToBinaryContent();
140 ClientResult result = await GenerateSpeechAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
141 return ClientResult.FromValue(result.GetRawResponse().Content, result.GetRawResponse());
142 }
143
144 /// <summary> Generates a life-like, spoken audio recording of the input text. </summary>
145 /// <remarks>
146 /// The default format of the generated audio is <see cref="GeneratedSpeechFormat.Mp3"/> unless otherwise specified
147 /// via <see cref="SpeechGenerationOptions.ResponseFormat"/>.
148 /// </remarks>
149 /// <param name="text"> The text to generate audio for. </param>
150 /// <param name="voice"> The voice to use in the generated audio. </param>
151 /// <param name="options"> The options to configure the audio generation. </param>
152 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
153 /// <exception cref="ArgumentNullException"> <paramref name="text"/> is null. </exception>
154 /// <returns> The generated audio in the specified output format. </returns>
155 public virtual ClientResult<BinaryData> GenerateSpeech(string text, GeneratedSpeechVoice voice, SpeechGenerationOptions options = null, CancellationToken cancellationToken = default)
156 {
157 Argument.AssertNotNull(text, nameof(text));
158
159 options ??= new();
160 CreateSpeechGenerationOptions(text, voice, ref options);
161
162 using BinaryContent content = options.ToBinaryContent();
163 ClientResult result = GenerateSpeech(content, cancellationToken.ToRequestOptions()); ;
164 return ClientResult.FromValue(result.GetRawResponse().Content, result.GetRawResponse());
165 }
166
167 #endregion
168
169 #region TranscribeAudio
170
171 /// <summary> Transcribes the input audio. </summary>
172 /// <param name="audio"> The audio stream to transcribe. </param>
173 /// <param name="audioFilename">
174 /// The filename associated with the audio stream. The filename's extension (for example: .mp3) will be used to
175 /// validate the format of the input audio. The request may fail if the filename's extension and the actual
176 /// format of the input audio do not match.
177 /// </param>
178 /// <param name="options"> The options to configure the audio transcription. </param>
179 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
180 /// <exception cref="ArgumentNullException"> <paramref name="audio"/> or <paramref name="audioFilename"/> is null. </exception>
181 /// <exception cref="ArgumentException"> <paramref name="audioFilename"/> is an empty string, and was expected to be non-empty. </exception>
182 public virtual async Task<ClientResult<AudioTranscription>> TranscribeAudioAsync(Stream audio, string audioFilename, AudioTranscriptionOptions options = null, CancellationToken cancellationToken = default)
183 {
184 Argument.AssertNotNull(audio, nameof(audio));
185 Argument.AssertNotNullOrEmpty(audioFilename, nameof(audioFilename));
186
187 using MultiPartFormDataBinaryContent content
188 = CreatePerCallTranscriptionOptions(options)
189 .ToMultipartContent(audio, audioFilename);
190
191 ClientResult result = await TranscribeAudioAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
192 return ClientResult.FromValue(AudioTranscription.FromResponse(result.GetRawResponse()), result.GetRawResponse());
193 }
194
195 /// <summary> Transcribes the input audio. </summary>
196 /// <param name="audio"> The audio stream to transcribe. </param>
197 /// <param name="audioFilename">
198 /// The filename associated with the audio stream. The filename's extension (for example: .mp3) will be used to
199 /// validate the format of the input audio. The request may fail if the filename's extension and the actual
200 /// format of the input audio do not match.
201 /// </param>
202 /// <param name="options"> The options to configure the audio transcription. </param>
203 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
204 /// <exception cref="ArgumentNullException"> <paramref name="audio"/> or <paramref name="audioFilename"/> is null. </exception>
205 /// <exception cref="ArgumentException"> <paramref name="audioFilename"/> is an empty string, and was expected to be non-empty. </exception>
206 public virtual ClientResult<AudioTranscription> TranscribeAudio(Stream audio, string audioFilename, AudioTranscriptionOptions options = null, CancellationToken cancellationToken = default)
207 {
208 Argument.AssertNotNull(audio, nameof(audio));
209 Argument.AssertNotNullOrEmpty(audioFilename, nameof(audioFilename));
210
211 using MultiPartFormDataBinaryContent content
212 = CreatePerCallTranscriptionOptions(options)
213 .ToMultipartContent(audio, audioFilename);
214
215 ClientResult result = TranscribeAudio(content, content.ContentType, cancellationToken.ToRequestOptions());
216 return ClientResult.FromValue(AudioTranscription.FromResponse(result.GetRawResponse()), result.GetRawResponse());
217 }
218
219 /// <summary> Transcribes the input audio. </summary>
220 /// <param name="audioFilePath">
221 /// The path of the audio file to transcribe. The provided file path's extension (for example: .mp3) will be
222 /// used to validate the format of the input audio. The request may fail if the file path's extension and the
223 /// actual format of the input audio do not match.
224 /// </param>
225 /// <param name="options"> The options to configure the audio transcription. </param>
226 /// <exception cref="ArgumentNullException"> <paramref name="audioFilePath"/> is null. </exception>
227 /// <exception cref="ArgumentException"> <paramref name="audioFilePath"/> is an empty string, and was expected to be non-empty. </exception>
228 public virtual async Task<ClientResult<AudioTranscription>> TranscribeAudioAsync(string audioFilePath, AudioTranscriptionOptions options = null)
229 {
230 Argument.AssertNotNullOrEmpty(audioFilePath, nameof(audioFilePath));
231
232 using FileStream audioStream = File.OpenRead(audioFilePath);
233 return await TranscribeAudioAsync(audioStream, audioFilePath, options).ConfigureAwait(false);
234 }
235
236 /// <summary> Transcribes the input audio. </summary>
237 /// <param name="audioFilePath">
238 /// The path of the audio file to transcribe. The provided file path's extension (for example: .mp3) will be
239 /// used to validate the format of the input audio. The request may fail if the file path's extension and the
240 /// actual format of the input audio do not match.
241 /// </param>
242 /// <param name="options"> The options to configure the audio transcription. </param>
243 /// <exception cref="ArgumentNullException"> <paramref name="audioFilePath"/> is null. </exception>
244 /// <exception cref="ArgumentException"> <paramref name="audioFilePath"/> is an empty string, and was expected to be non-empty. </exception>
245 public virtual ClientResult<AudioTranscription> TranscribeAudio(string audioFilePath, AudioTranscriptionOptions options = null)
246 {
247 Argument.AssertNotNullOrEmpty(audioFilePath, nameof(audioFilePath));
248
249 using FileStream audioStream = File.OpenRead(audioFilePath);
250 return TranscribeAudio(audioStream, audioFilePath, options);
251 }
252
253 // CUSTOM: Added Experimental attribute.
254 [Experimental("OPENAI001")]
255 public virtual AsyncCollectionResult<StreamingAudioTranscriptionUpdate> TranscribeAudioStreamingAsync(Stream audio, string audioFilename, AudioTranscriptionOptions options = null, CancellationToken cancellationToken = default)
256 {
257 Argument.AssertNotNull(audio, nameof(audio));
258 Argument.AssertNotNullOrEmpty(audioFilename, nameof(audioFilename));
259
260 EnsureModelSupportsStreaming();
261
262 MultiPartFormDataBinaryContent content
263 = CreatePerCallTranscriptionOptions(options, stream: true)
264 .ToMultipartContent(audio, audioFilename);
265
266 return new AsyncSseUpdateCollection<StreamingAudioTranscriptionUpdate>(
267 async () => await TranscribeAudioAsync(content, content.ContentType, cancellationToken.ToRequestOptions(streaming: true)).ConfigureAwait(false),
268 StreamingAudioTranscriptionUpdate.DeserializeStreamingAudioTranscriptionUpdate,
269 cancellationToken);
270 }
271
272 // CUSTOM: Added Experimental attribute.
273 [Experimental("OPENAI001")]
274 public virtual AsyncCollectionResult<StreamingAudioTranscriptionUpdate> TranscribeAudioStreamingAsync(string audioFilePath, AudioTranscriptionOptions options = null, CancellationToken cancellationToken = default)
275 {
276 Argument.AssertNotNullOrEmpty(audioFilePath, nameof(audioFilePath));
277
278 EnsureModelSupportsStreaming();
279
280 FileStream inputStream = File.OpenRead(audioFilePath);
281
282 MultiPartFormDataBinaryContent content
283 = CreatePerCallTranscriptionOptions(options, stream: true)
284 .ToMultipartContent(inputStream, audioFilePath);
285
286 AsyncSseUpdateCollection<StreamingAudioTranscriptionUpdate> result = new(
287 async () => await TranscribeAudioAsync(content, content.ContentType, cancellationToken.ToRequestOptions(streaming: true)).ConfigureAwait(false),
288 StreamingAudioTranscriptionUpdate.DeserializeStreamingAudioTranscriptionUpdate,
289 cancellationToken);
290 result.AdditionalDisposalActions.Add(() => inputStream?.Dispose());
291 return result;
292 }
293
294 // CUSTOM: Added Experimental attribute.
295 [Experimental("OPENAI001")]
296 public virtual CollectionResult<StreamingAudioTranscriptionUpdate> TranscribeAudioStreaming(Stream audio, string audioFilename, AudioTranscriptionOptions options = null, CancellationToken cancellationToken = default)
297 {
298 Argument.AssertNotNull(audio, nameof(audio));
299 Argument.AssertNotNullOrEmpty(audioFilename, nameof(audioFilename));
300
301 EnsureModelSupportsStreaming();
302
303 MultiPartFormDataBinaryContent content
304 = CreatePerCallTranscriptionOptions(options, stream: true)
305 .ToMultipartContent(audio, audioFilename);
306
307 return new SseUpdateCollection<StreamingAudioTranscriptionUpdate>(
308 () => TranscribeAudio(content, content.ContentType, cancellationToken.ToRequestOptions(streaming: true)),
309 StreamingAudioTranscriptionUpdate.DeserializeStreamingAudioTranscriptionUpdate,
310 cancellationToken);
311 }
312
313 // CUSTOM: Added Experimental attribute.
314 [Experimental("OPENAI001")]
315 public virtual CollectionResult<StreamingAudioTranscriptionUpdate> TranscribeAudioStreaming(string audioFilePath, AudioTranscriptionOptions options = null, CancellationToken cancellationToken = default)
316 {
317 Argument.AssertNotNullOrEmpty(audioFilePath, nameof(audioFilePath));
318
319 EnsureModelSupportsStreaming();
320
321 FileStream inputStream = File.OpenRead(audioFilePath);
322
323 MultiPartFormDataBinaryContent content
324 = CreatePerCallTranscriptionOptions(options, stream: true)
325 .ToMultipartContent(inputStream, audioFilePath);
326
327 SseUpdateCollection<StreamingAudioTranscriptionUpdate> result = new(
328 () => TranscribeAudio(content, content.ContentType, cancellationToken.ToRequestOptions(streaming: true)),
329 StreamingAudioTranscriptionUpdate.DeserializeStreamingAudioTranscriptionUpdate,
330 cancellationToken);
331 result.AdditionalDisposalActions.Add(() => inputStream?.Dispose());
332 return result;
333 }
334
335 private void EnsureModelSupportsStreaming()
336 {
337 if (string.Equals(_model, "whisper-1", StringComparison.OrdinalIgnoreCase))
338 {
339 string isEnabled = Environment.GetEnvironmentVariable("OPENAI_ENABLE_WHISPER_1_STREAMING");
340 if (!string.Equals(isEnabled, "true", StringComparison.OrdinalIgnoreCase))
341 {
342 throw new NotSupportedException(
343 "The selected model 'whisper-1' does not support streaming transcription. " +
344 "Please use a compatible model or set the environment variable 'OPENAI_ENABLE_WHISPER_1_STREAMING=true' to bypass this check.");
345 }
346 }
347 }
348
349 #endregion
350
351 #region TranslateAudio
352
353 /// <summary> Translates the input audio into English. </summary>
354 /// <param name="audio"> The audio stream to translate. </param>
355 /// <param name="audioFilename">
356 /// The filename associated with the audio stream. The filename's extension (for example: .mp3) will be used to
357 /// validate the format of the input audio. The request may fail if the filename's extension and the actual
358 /// format of the input audio do not match.
359 /// </param>
360 /// <param name="options"> The options to configure the audio translation. </param>
361 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
362 /// <exception cref="ArgumentNullException"> <paramref name="audio"/> or <paramref name="audioFilename"/> is null. </exception>
363 /// <exception cref="ArgumentException"> <paramref name="audioFilename"/> is an empty string, and was expected to be non-empty. </exception>
364 public virtual async Task<ClientResult<AudioTranslation>> TranslateAudioAsync(Stream audio, string audioFilename, AudioTranslationOptions options = null, CancellationToken cancellationToken = default)
365 {
366 Argument.AssertNotNull(audio, nameof(audio));
367 Argument.AssertNotNullOrEmpty(audioFilename, nameof(audioFilename));
368
369 options ??= new();
370 CreateAudioTranslationOptions(audio, audioFilename, ref options);
371
372 using MultiPartFormDataBinaryContent content = options.ToMultipartContent(audio, audioFilename);
373 ClientResult result = await TranslateAudioAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
374 return ClientResult.FromValue(AudioTranslation.FromResponse(result.GetRawResponse()), result.GetRawResponse());
375 }
376
377 /// <summary> Translates the input audio into English. </summary>
378 /// <param name="audio"> The audio stream to translate. </param>
379 /// <param name="audioFilename">
380 /// The filename associated with the audio stream. The filename's extension (for example: .mp3) will be used to
381 /// validate the format of the input audio. The request may fail if the filename's extension and the actual
382 /// format of the input audio do not match.
383 /// </param>
384 /// <param name="options"> The options to configure the audio translation. </param>
385 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
386 /// <exception cref="ArgumentNullException"> <paramref name="audio"/> or <paramref name="audioFilename"/> is null. </exception>
387 /// <exception cref="ArgumentException"> <paramref name="audioFilename"/> is an empty string, and was expected to be non-empty. </exception>
388 public virtual ClientResult<AudioTranslation> TranslateAudio(Stream audio, string audioFilename, AudioTranslationOptions options = null, CancellationToken cancellationToken = default)
389 {
390 Argument.AssertNotNull(audio, nameof(audio));
391 Argument.AssertNotNullOrEmpty(audioFilename, nameof(audioFilename));
392
393 options ??= new();
394 CreateAudioTranslationOptions(audio, audioFilename, ref options);
395
396 using MultiPartFormDataBinaryContent content = options.ToMultipartContent(audio, audioFilename);
397 ClientResult result = TranslateAudio(content, content.ContentType, cancellationToken.ToRequestOptions());
398 return ClientResult.FromValue(AudioTranslation.FromResponse(result.GetRawResponse()), result.GetRawResponse());
399 }
400
401 /// <summary> Translates the input audio into English. </summary>
402 /// <param name="audioFilePath">
403 /// The path of the audio file to translate. The provided file path's extension (for example: .mp3) will be
404 /// used to validate the format of the input audio. The request may fail if the file path's extension and the
405 /// actual format of the input audio do not match.
406 /// </param>
407 /// <param name="options"> The options to configure the audio translation. </param>
408 /// <exception cref="ArgumentNullException"> <paramref name="audioFilePath"/> was null. </exception>
409 /// <exception cref="ArgumentException"> <paramref name="audioFilePath"/> is an empty string, and was expected to be non-empty. </exception>
410 public virtual ClientResult<AudioTranslation> TranslateAudio(string audioFilePath, AudioTranslationOptions options = null)
411 {
412 Argument.AssertNotNullOrEmpty(audioFilePath, nameof(audioFilePath));
413
414 using FileStream audioStream = File.OpenRead(audioFilePath);
415 return TranslateAudio(audioStream, audioFilePath, options);
416 }
417
418 /// <summary> Translates the input audio into English. </summary>
419 /// <param name="audioFilePath">
420 /// The path of the audio file to translate. The provided file path's extension (for example: .mp3) will be
421 /// used to validate the format of the input audio. The request may fail if the file path's extension and the
422 /// actual format of the input audio do not match.
423 /// </param>
424 /// <param name="options"> The options to configure the audio translation. </param>
425 /// <exception cref="ArgumentNullException"> <paramref name="audioFilePath"/> was null. </exception>
426 /// <exception cref="ArgumentException"> <paramref name="audioFilePath"/> is an empty string, and was expected to be non-empty. </exception>
427 public virtual async Task<ClientResult<AudioTranslation>> TranslateAudioAsync(string audioFilePath, AudioTranslationOptions options = null)
428 {
429 Argument.AssertNotNull(audioFilePath, nameof(audioFilePath));
430
431 using FileStream audioStream = File.OpenRead(audioFilePath);
432 return await TranslateAudioAsync(audioStream, audioFilePath, options);
433 }
434
435 #endregion
436
437 private void CreateSpeechGenerationOptions(string text, GeneratedSpeechVoice voice, ref SpeechGenerationOptions options)
438 {
439 options.Input = text;
440 options.Voice = voice;
441 options.Model = _model;
442 }
443
444 internal virtual AudioTranscriptionOptions CreatePerCallTranscriptionOptions(AudioTranscriptionOptions userOptions, bool stream = false)
445 {
446 AudioTranscriptionOptions copiedOptions = userOptions is null ? new() : userOptions.GetClone();
447
448 copiedOptions.Model = _model;
449
450 if (stream)
451 {
452 copiedOptions.Stream = true;
453 }
454
455 return copiedOptions;
456 }
457
458 private void CreateAudioTranslationOptions(Stream audio, string audioFilename, ref AudioTranslationOptions options)
459 {
460 options.Model = _model;
461 }
462}