forked from HemulGM/DelphiOpenAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenAI.Audio.pas
236 lines (202 loc) · 7.69 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
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>
/// The audio file to transcribe, in one of these formats: mp3, mp4, mpeg, mpga, m4a, wav, or webm.
/// </summary>
function &File(const FileName: string): TAudioTranscription; overload;
/// <summary>
/// The audio file to transcribe, in one of these formats: mp3, mp4, mpeg, mpga, m4a, wav, or webm.
/// </summary>
function &File(const Stream: TStream; const FileName: string): TAudioTranscription; overload;
/// <summary>
/// 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>
/// The audio file to translate, in one of these formats: mp3, mp4, mpeg, mpga, m4a, wav, or webm.
/// </summary>
function &File(const FileName: string): TAudioTranslation; overload;
/// <summary>
/// The audio file to transcribe, in one of these formats: mp3, mp4, mpeg, mpga, m4a, wav, or webm.
/// </summary>
function &File(const Stream: TStream; const FileName: string): TAudioTranslation; overload;
/// <summary>
/// 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;
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;
end;
implementation
{ TAudioRoute }
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: string): 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: string): 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', Value.ToString);
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: string): TAudioTranslation;
begin
AddFile('file', FileName);
Result := Self;
end;
function TAudioTranslation.&File(const Stream: TStream; const FileName: string): TAudioTranslation;
begin
AddStream('file', Stream, FileName);
Result := Self;
end;
function TAudioTranslation.Temperature(const Value: Single): TAudioTranslation;
begin
AddField('temperature', Value.ToString);
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;
end.