openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.0.0-beta.12

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/Audio/AudioClient.cs

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