-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathOpenAI.Files.pas
222 lines (194 loc) · 6.37 KB
/
OpenAI.Files.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
unit OpenAI.Files;
interface
uses
System.Classes, System.SysUtils, System.Net.Mime, OpenAI.API.Params,
OpenAI.API, OpenAI.Types;
{$SCOPEDENUMS ON}
type
TFileCreatePurpose = (FineTune, Answers, Search, Classifications);
TFileCreatePurposeHelper = record helper for TFileCreatePurpose
function ToString: string;
end;
TFileUploadParams = class(TMultipartFormData)
/// <summary>
/// Name of the JSON Lines file to be uploaded.
/// If the purpose is set to "fine-tune", the file will be used for fine-tuning.
/// </summary>
function &File(const FileName: TFileName): TFileUploadParams; overload;
/// <summary>
/// Name of the JSON Lines file to be uploaded.
/// If the purpose is set to "fine-tune", the file will be used for fine-tuning.
/// </summary>
function &File(const Stream: TStream; const FileName: TFileName): TFileUploadParams; overload;
/// <summary>
/// The intended purpose of the uploaded documents.
/// Use "fine-tune" for fine-tuning. This allows us to validate the format of the uploaded file.
/// Variants: ['fine-tune', 'answers', 'search', 'classifications']
/// </summary>
function Purpose(const Value: string): TFileUploadParams; overload;
/// <summary>
/// The intended purpose of the uploaded documents.
/// Use "fine-tune" for Fine-tuning. This allows us to validate the format of the uploaded file.
/// </summary>
function Purpose(const Value: TFileCreatePurpose): TFileUploadParams; overload;
constructor Create; reintroduce;
end;
/// <summary>
/// The File object represents a document that has been uploaded to OpenAI.
/// </summary>
TFile = class
private
FBytes: Int64;
FCreated_at: Int64;
FFilename: TFileName;
FId: string;
FObject: string;
FPurpose: string;
FStatus: string;
FStatus_details: string;
public
/// <summary>
/// The file identifier, which can be referenced in the API endpoints.
/// </summary>
property Id: string read FId write FId;
/// <summary>
/// The object type, which is always "file".
/// </summary>
property &Object: string read FObject write FObject;
/// <summary>
/// The size of the file in bytes.
/// </summary>
property Bytes: Int64 read FBytes write FBytes;
/// <summary>
/// The Unix timestamp (in seconds) for when the file was created.
/// </summary>
property CreatedAt: Int64 read FCreated_at write FCreated_at;
/// <summary>
/// The name of the file.
/// </summary>
property FileName: TFileName read FFilename write FFilename;
/// <summary>
/// The intended purpose of the file. Currently, only "fine-tune" is supported.
/// </summary>
property Purpose: string read FPurpose write FPurpose;
/// <summary>
/// DEPRECATED
/// The current status of the file, which can be either uploaded, processed, pending, error, deleting or deleted.
/// </summary>
property Status: string read FStatus write FStatus;
/// <summary>
/// DEPRECATED
/// Additional details about the status of the file. If the file is in the error state,
/// this will include a message describing the error.
/// </summary>
property StatusDetails: string read FStatus_details write FStatus_details;
end;
TFiles = class
private
FData: TArray<TFile>;
FObject: string;
public
property Data: TArray<TFile> read FData write FData;
property &Object: string read FObject write FObject;
destructor Destroy; override;
end;
TFilesRoute = class(TOpenAIAPIRoute)
public
/// <summary>
/// Returns a list of files that belong to the user's organization.
/// </summary>
function List: TFiles;
/// <summary>
/// Upload a file that contains document(s) to be used across various endpoints/features.
/// Currently, the size of all the files uploaded by one organization can be up to 1 GB.
/// Please contact us if you need to increase the storage limit.
/// </summary>
function Upload(ParamProc: TProc<TFileUploadParams>): TFile;
/// <summary>
/// Delete a file.
/// </summary>
function Delete(const FileId: string = ''): TDeletionStatus;
/// <summary>
/// Returns information about a specific file.
/// </summary>
function Retrieve(const FileId: string = ''): TFile;
/// <summary>
/// Returns the contents of the specified file
/// </summary>
procedure Download(const FileId: string; Stream: TStream);
end;
implementation
{ TFilesRoute }
function TFilesRoute.Upload(ParamProc: TProc<TFileUploadParams>): TFile;
begin
Result := API.PostForm<TFile, TFileUploadParams>('files', ParamProc);
end;
function TFilesRoute.Delete(const FileId: string): TDeletionStatus;
begin
Result := API.Delete<TDeletionStatus>('files/' + FileId);
end;
procedure TFilesRoute.Download(const FileId: string; Stream: TStream);
begin
API.GetFile('files/' + FileId + '/content', Stream);
end;
function TFilesRoute.List: TFiles;
begin
Result := API.Get<TFiles>('files');
end;
function TFilesRoute.Retrieve(const FileId: string): TFile;
begin
Result := API.Get<TFile>('files/' + FileId);
end;
{ TFiles }
destructor TFiles.Destroy;
var
Item: TFile;
begin
for Item in FData do
if Assigned(Item) then
Item.Free;
inherited;
end;
{ TFileUploadParams }
function TFileUploadParams.&File(const FileName: TFileName): TFileUploadParams;
begin
AddFile('file', FileName);
Result := Self;
end;
constructor TFileUploadParams.Create;
begin
inherited Create(True);
end;
function TFileUploadParams.&File(const Stream: TStream; const FileName: TFileName): TFileUploadParams;
begin
{$IF CompilerVersion <= 35}
AddStream('file', Stream, FileName);
{$ELSE}
AddStream('file', Stream, False, FileName);
{$ENDIF}
Result := Self;
end;
function TFileUploadParams.Purpose(const Value: TFileCreatePurpose): TFileUploadParams;
begin
Result := Purpose(Value.ToString);
end;
function TFileUploadParams.Purpose(const Value: string): TFileUploadParams;
begin
AddField('purpose', Value);
Result := Self;
end;
{ TFileCreatePurposeHelper }
function TFileCreatePurposeHelper.ToString: string;
begin
case Self of
TFileCreatePurpose.FineTune:
Result := 'fine-tune';
TFileCreatePurpose.Answers:
Result := 'answers';
TFileCreatePurpose.Search:
Result := 'search';
TFileCreatePurpose.Classifications:
Result := 'classifications';
end;
end;
end.