openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.0.0-beta.7

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/Audio/AudioClient.cs

337lines · modecode

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