forked from HemulGM/DelphiOpenAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenAI.Files.pas
192 lines (164 loc) · 5.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
unit OpenAI.Files;
interface
uses
System.Classes, System.SysUtils, System.Net.Mime, OpenAI.API.Params,
OpenAI.API;
{$SCOPEDENUMS ON}
type
TFileCreatePurpose = (FineTune, Answer, Search, Classifications);
TFileCreatePurposeHelper = record helper for TFileCreatePurpose
function ToString: string;
end;
TFileCreateParams = class(TMultipartFormData)
/// <summary>
/// Name of the JSON Lines file to be uploaded.
/// If the purpose is set to "fine-tune", each line is a JSON record with "prompt" and "completion"
/// fields representing your training examples.
/// </summary>
function &File(const FileName: string): TFileCreateParams; overload;
/// <summary>
/// Name of the JSON Lines file to be uploaded.
/// If the purpose is set to "fine-tune", each line is a JSON record with "prompt" and "completion"
/// fields representing your training examples.
/// </summary>
function &File(const Stream: TStream; const FileName: string): TFileCreateParams; 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): TFileCreateParams; 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: TFileCreatePurpose): TFileCreateParams; overload;
end;
TFile = class
private
FBytes: Int64;
FCreated_at: Int64;
FFilename: string;
FId: string;
FObject: string;
FPurpose: string;
public
property Bytes: Int64 read FBytes write FBytes;
property CreatedAt: Int64 read FCreated_at write FCreated_at;
property FileName: string read FFilename write FFilename;
property Id: string read FId write FId;
property &Object: string read FObject write FObject;
property Purpose: string read FPurpose write FPurpose;
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;
TDeletedInfo = class
private
FDeleted: Boolean;
FId: string;
FObject: string;
public
property Deleted: Boolean read FDeleted write FDeleted;
property Id: string read FId write FId;
property &Object: string read FObject write FObject;
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 Create(ParamProc: TProc<TFileCreateParams>): TFile;
/// <summary>
/// Delete a file.
/// </summary>
function Delete(const FileId: string): TDeletedInfo;
/// <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.Create(ParamProc: TProc<TFileCreateParams>): TFile;
begin
Result := API.PostForm<TFile, TFileCreateParams>('files', ParamProc);
end;
function TFilesRoute.Delete(const FileId: string): TDeletedInfo;
begin
Result := API.Delete<TDeletedInfo>('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;
{ TFileCreateParams }
function TFileCreateParams.&File(const FileName: string): TFileCreateParams;
begin
AddFile('file', FileName);
Result := Self;
end;
function TFileCreateParams.&File(const Stream: TStream; const FileName: string): TFileCreateParams;
begin
AddStream('file', Stream, FileName);
Result := Self;
end;
function TFileCreateParams.Purpose(const Value: TFileCreatePurpose): TFileCreateParams;
begin
Result := Purpose(Value.ToString);
end;
function TFileCreateParams.Purpose(const Value: string): TFileCreateParams;
begin
AddField('purpose', Value);
Result := Self;
end;
{ TFileCreatePurposeHelper }
function TFileCreatePurposeHelper.ToString: string;
begin
case Self of
TFileCreatePurpose.FineTune:
Result := 'fine-tune';
TFileCreatePurpose.Answer:
Result := 'answer';
TFileCreatePurpose.Search:
Result := 'search';
TFileCreatePurpose.Classifications:
Result := 'classifications';
end;
end;
end.