openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.0.0-beta.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/Audio/AudioClient.cs

330lines · modecode

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