forked from HemulGM/DelphiOpenAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenAI.Audio.pas
316 lines (272 loc) · 10.3 KB
/
OpenAI.Audio.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
unit OpenAI.Audio;
interface
uses
System.Classes, System.SysUtils, System.Net.Mime, OpenAI.API.Params,
OpenAI.API;
{$SCOPEDENUMS ON}
type
TAudioResponseFormat = (Json, Text, Srt, VerboseJson, Vtt);
TAudioResponseFormatHelper = record helper for TAudioResponseFormat
function ToString: string;
end;
TAudioTranscription = class(TMultipartFormData)
/// <summary>
/// Required.
/// The audio file object (not file name) to transcribe, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm.
/// </summary>
function &File(const FileName: TFileName): TAudioTranscription; overload;
/// <summary>
/// Required.
/// The audio file object (not file name) to transcribe, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm.
/// </summary>
function &File(const Stream: TStream; const FileName: TFileName): TAudioTranscription; overload;
/// <summary>
/// Required.
/// ID of the model to use. Only whisper-1 is currently available.
/// </summary>
function Model(const Value: string): TAudioTranscription; overload;
/// <summary>
/// An optional text to guide the model's style or continue a previous audio segment.
/// The prompt should match the audio language.
/// </summary>
/// <seealso>https://platform.openai.com/docs/guides/speech-to-text/prompting</seealso>
function Prompt(const Value: string): TAudioTranscription; overload;
/// <summary>
/// The format of the transcript output, in one of these options: json, text, srt, verbose_json, or vtt.
/// </summary>
function ResponseFormat(const Value: string): TAudioTranscription; overload;
/// <summary>
/// The format of the transcript output, in one of these options: json, text, srt, verbose_json, or vtt.
/// </summary>
function ResponseFormat(const Value: TAudioResponseFormat = TAudioResponseFormat.Json): TAudioTranscription; overload;
/// <summary>
/// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random,
/// while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use
/// log probability to automatically increase the temperature until certain thresholds are hit.
/// </summary>
function Temperature(const Value: Single = 0): TAudioTranscription;
/// <summary>
/// The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency (like en, ru, uk).
/// </summary>
function Language(const Value: string): TAudioTranscription; overload;
constructor Create; reintroduce;
end;
TAudioTranslation = class(TMultipartFormData)
/// <summary>
/// Required.
/// The audio file object (not file name) translate, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm.
/// </summary>
function &File(const FileName: TFileName): TAudioTranslation; overload;
/// <summary>
/// Required.
/// The audio file object (not file name) translate, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm.
/// </summary>
function &File(const Stream: TStream; const FileName: TFileName): TAudioTranslation; overload;
/// <summary>
/// Required.
/// ID of the model to use. Only whisper-1 is currently available.
/// </summary>
function Model(const Value: string): TAudioTranslation; overload;
/// <summary>
/// An optional text to guide the model's style or continue a previous audio segment. The prompt should be in English.
/// </summary>
/// <seealso>https://platform.openai.com/docs/guides/speech-to-text/prompting</seealso>
function Prompt(const Value: string): TAudioTranslation; overload;
/// <summary>
/// The format of the transcript output, in one of these options: json, text, srt, verbose_json, or vtt.
/// </summary>
function ResponseFormat(const Value: string = 'json'): TAudioTranslation;
/// <summary>
/// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random,
/// while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use
/// log probability to automatically increase the temperature until certain thresholds are hit.
/// </summary>
function Temperature(const Value: Single = 0): TAudioTranslation;
constructor Create; reintroduce;
end;
TAudioSpeechParams = class(TJSONParam)
/// <summary>
/// One of the available TTS models: tts-1 or tts-1-hd
/// </summary>
function Model(const Value: string): TAudioSpeechParams;
/// <summary>
/// The text to generate audio for. The maximum length is 4096 characters.
/// </summary>
function Input(const Value: string): TAudioSpeechParams; overload;
/// <summary>
/// The voice to use when generating the audio.
/// Supported voices are alloy, echo, fable, onyx, nova, and shimmer.
/// </summary>
function Voice(const Value: string): TAudioSpeechParams; overload;
/// <summary>
/// The format to audio in. Supported formats are mp3, opus, aac, and flac.
/// </summary>
function ResponseFormat(const Value: string): TAudioSpeechParams;
/// <summary>
/// The speed of the generated audio. Select a value from 0.25 to 4.0. 1.0 is the default.
/// </summary>
function Speed(const Value: Single = 1): TAudioSpeechParams;
constructor Create; override;
end;
TAudioText = class
private
FText: string;
public
property Text: string read FText write FText;
end;
/// <summary>
/// Learn how to turn audio into text.
/// </summary>
TAudioRoute = class(TOpenAIAPIRoute)
public
/// <summary>
/// Transcribes audio into the input language.
/// </summary>
function CreateTranscription(ParamProc: TProc<TAudioTranscription>): TAudioText;
/// <summary>
/// Translates audio into into English.
/// </summary>
function CreateTranslation(ParamProc: TProc<TAudioTranslation>): TAudioText;
/// <summary>
/// Generates audio from the input text.
/// </summary>
procedure CreateSpeech(ParamProc: TProc<TAudioSpeechParams>; Stream: TStream);
end;
implementation
{ TAudioRoute }
procedure TAudioRoute.CreateSpeech(ParamProc: TProc<TAudioSpeechParams>; Stream: TStream);
begin
API.Post<TAudioSpeechParams>('audio/speech', ParamProc, Stream);
end;
function TAudioRoute.CreateTranscription(ParamProc: TProc<TAudioTranscription>): TAudioText;
begin
Result := API.PostForm<TAudioText, TAudioTranscription>('audio/transcriptions', ParamProc);
end;
function TAudioRoute.CreateTranslation(ParamProc: TProc<TAudioTranslation>): TAudioText;
begin
Result := API.PostForm<TAudioText, TAudioTranslation>('audio/translations', ParamProc);
end;
{ TAudioTranscription }
function TAudioTranscription.&File(const FileName: TFileName): TAudioTranscription;
begin
AddFile('file', FileName);
Result := Self;
end;
constructor TAudioTranscription.Create;
begin
inherited Create(True);
Model('whisper-1');
end;
function TAudioTranscription.&File(const Stream: TStream; const FileName: TFileName): TAudioTranscription;
begin
AddStream('file', Stream, FileName);
Result := Self;
end;
function TAudioTranscription.Language(const Value: string): TAudioTranscription;
begin
AddField('language', Value);
Result := Self;
end;
function TAudioTranscription.Temperature(const Value: Single): TAudioTranscription;
begin
AddField('temperature', FormatFloat('0,0', Value));
Result := Self;
end;
function TAudioTranscription.Prompt(const Value: string): TAudioTranscription;
begin
AddField('prompt', Value);
Result := Self;
end;
function TAudioTranscription.ResponseFormat(const Value: TAudioResponseFormat): TAudioTranscription;
begin
Result := ResponseFormat(Value.ToString);
end;
function TAudioTranscription.ResponseFormat(const Value: string): TAudioTranscription;
begin
AddField('response_format', Value);
Result := Self;
end;
function TAudioTranscription.Model(const Value: string): TAudioTranscription;
begin
AddField('model', Value);
Result := Self;
end;
{ TAudioTranslation }
function TAudioTranslation.&File(const FileName: TFileName): TAudioTranslation;
begin
AddFile('file', FileName);
Result := Self;
end;
constructor TAudioTranslation.Create;
begin
inherited Create(True);
end;
function TAudioTranslation.&File(const Stream: TStream; const FileName: TFileName): TAudioTranslation;
begin
AddStream('file', Stream, FileName);
Result := Self;
end;
function TAudioTranslation.Temperature(const Value: Single): TAudioTranslation;
begin
AddField('temperature', FormatFloat('0,0', Value));
Result := Self;
end;
function TAudioTranslation.Prompt(const Value: string): TAudioTranslation;
begin
AddField('prompt', Value);
Result := Self;
end;
function TAudioTranslation.ResponseFormat(const Value: string): TAudioTranslation;
begin
AddField('response_format', Value);
Result := Self;
end;
function TAudioTranslation.Model(const Value: string): TAudioTranslation;
begin
AddField('model', Value);
Result := Self;
end;
{ TAudioResponseFormatHelper }
function TAudioResponseFormatHelper.ToString: string;
begin
case Self of
TAudioResponseFormat.Json:
Result := 'json';
TAudioResponseFormat.Text:
Result := 'text';
TAudioResponseFormat.Srt:
Result := 'srt';
TAudioResponseFormat.VerboseJson:
Result := 'verbose_json';
TAudioResponseFormat.Vtt:
Result := 'vtt';
end;
end;
{ TAudioSpeechParams }
constructor TAudioSpeechParams.Create;
begin
inherited;
Model('tts-1');
Voice('alloy');
end;
function TAudioSpeechParams.Input(const Value: string): TAudioSpeechParams;
begin
Result := TAudioSpeechParams(Add('input', Value));
end;
function TAudioSpeechParams.Model(const Value: string): TAudioSpeechParams;
begin
Result := TAudioSpeechParams(Add('model', Value));
end;
function TAudioSpeechParams.ResponseFormat(const Value: string): TAudioSpeechParams;
begin
Result := TAudioSpeechParams(Add('response_format', Value));
end;
function TAudioSpeechParams.Speed(const Value: Single): TAudioSpeechParams;
begin
Result := TAudioSpeechParams(Add('speed', Value));
end;
function TAudioSpeechParams.Voice(const Value: string): TAudioSpeechParams;
begin
Result := TAudioSpeechParams(Add('voice', Value));
end;
end.