-
Notifications
You must be signed in to change notification settings - Fork 58
/
OpenAI.Moderations.pas
282 lines (260 loc) · 10.4 KB
/
OpenAI.Moderations.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
unit OpenAI.Moderations;
interface
uses
System.SysUtils, OpenAI.API.Params, OpenAI.API, Rest.Json.Types;
type
TModerationsParams = class(TJSONParam)
/// <summary>
/// The input text to classify
/// </summary>
function Input(const Value: string): TModerationsParams; overload;
/// <summary>
/// The input text to classify
/// </summary>
function Input(const Value: TArray<string>): TModerationsParams; overload;
/// <summary>
/// Two content moderations models are available: "text-moderation-stable" and "text-moderation-latest".
/// The default is text-moderation-latest which will be automatically upgraded over time.
/// This ensures you are always using our most accurate model. If you use text-moderation-stable,
/// we will provide advanced notice before updating the model. Accuracy of text-moderation-stable may be
/// slightly lower than for text-moderation-latest.
/// </summary>
function Model(const Value: string = 'text-moderation-latest'): TModerationsParams;
constructor Create; reintroduce;
end;
TCategoryScores = class
private
FHate: Extended;
[JSONName('hate/threatening')]
FHatethreatening: Extended;
[JSONName('self-harm')]
FSelfharm: Extended;
FSexual: Extended;
[JSONName('sexual/minors')]
FSexualminors: Extended;
FViolence: Extended;
[JSONName('violence/graphic')]
FViolencegraphic: Extended;
FHarassment: Extended;
[JSONName('harassment/threatening')]
FHarassmentthreatening: Extended;
[JSONName('self-harm/intent')]
FSelfharmintent: Extended;
[JSONName('self-harm/instructions')]
FSelfharminstructions: Extended;
public
/// <summary>
/// Content that expresses, incites, or promotes hate based on race, gender, ethnicity, religion, nationality,
/// sexual orientation, disability status, or caste. Hateful content aimed at non-protected groups (e.g., chess players)
/// is harrassment.
/// </summary>
property Hate: Extended read FHate write FHate;
/// <summary>
/// Hateful content that also includes violence or serious harm towards the targeted group based on race,
/// gender, ethnicity, religion, nationality, sexual orientation, disability status, or caste.
/// </summary>
property HateOrThreatening: Extended read FHatethreatening write FHatethreatening;
/// <summary>
/// Content that expresses, incites, or promotes harassing language towards any target.
/// </summary>
property Harassment: Extended read FHarassment write FHarassment;
/// <summary>
/// Harassment content that also includes violence or serious harm towards any target.
/// </summary>
property HarassmentOrThreatening: Extended read FHarassmentthreatening write FHarassmentthreatening;
/// <summary>
/// Content that promotes, encourages, or depicts acts of self-harm, such as suicide, cutting, and eating disorders.
/// </summary>
property SelfHarm: Extended read FSelfharm write FSelfharm;
/// <summary>
/// Content where the speaker expresses that they are engaging or intend to engage in acts of self-harm,
/// such as suicide, cutting, and eating disorders.
/// </summary>
property SelfHarmOrIntent: Extended read FSelfharmintent write FSelfharmintent;
/// <summary>
/// Content that encourages performing acts of self-harm, such as suicide, cutting, and eating disorders,
/// or that gives instructions or advice on how to commit such acts.
/// </summary>
property SelfHarmOrInstructions: Extended read FSelfharminstructions write FSelfharminstructions;
/// <summary>
/// Content meant to arouse sexual excitement, such as the description of sexual activity,
/// or that promotes sexual services (excluding sex education and wellness).
/// </summary>
property Sexual: Extended read FSexual write FSexual;
/// <summary>
/// Sexual content that includes an individual who is under 18 years old.
/// </summary>
property SexualOrMinors: Extended read FSexualminors write FSexualminors;
/// <summary>
/// Content that depicts death, violence, or physical injury.
/// </summary>
property Violence: Extended read FViolence write FViolence;
/// <summary>
/// Content that depicts death, violence, or physical injury in graphic detail.
/// </summary>
property ViolenceOrGraphic: Extended read FViolencegraphic write FViolencegraphic;
end;
TCategories = class
private
FHate: Boolean;
[JSONName('hate/threatening')]
FHatethreatening: Boolean;
[JSONName('self-harm')]
FSelfharm: Boolean;
FSexual: Boolean;
[JSONName('sexual/minors')]
FSexualminors: Boolean;
FViolence: Boolean;
[JSONName('violence/graphic')]
FViolencegraphic: Boolean;
FHarassment: Boolean;
[JSONName('harassment/threatening')]
FHarassmentthreatening: Boolean;
[JSONName('self-harm/intent')]
FSelfharmintent: Boolean;
[JSONName('self-harm/instructions')]
FSelfharminstructions: Boolean;
public
/// <summary>
/// Content that expresses, incites, or promotes hate based on race, gender, ethnicity, religion, nationality,
/// sexual orientation, disability status, or caste. Hateful content aimed at non-protected groups (e.g., chess players)
/// is harrassment.
/// </summary>
property Hate: Boolean read FHate write FHate;
/// <summary>
/// Hateful content that also includes violence or serious harm towards the targeted group based on race,
/// gender, ethnicity, religion, nationality, sexual orientation, disability status, or caste.
/// </summary>
property HateOrThreatening: Boolean read FHatethreatening write FHatethreatening;
/// <summary>
/// Content that expresses, incites, or promotes harassing language towards any target.
/// </summary>
property Harassment: Boolean read FHarassment write FHarassment;
/// <summary>
/// Harassment content that also includes violence or serious harm towards any target.
/// </summary>
property HarassmentOrThreatening: Boolean read FHarassmentthreatening write FHarassmentthreatening;
/// <summary>
/// Content that promotes, encourages, or depicts acts of self-harm, such as suicide, cutting, and eating disorders.
/// </summary>
property SelfHarm: Boolean read FSelfharm write FSelfharm;
/// <summary>
/// Content where the speaker expresses that they are engaging or intend to engage in acts of self-harm,
/// such as suicide, cutting, and eating disorders.
/// </summary>
property SelfHarmOrIntent: Boolean read FSelfharmintent write FSelfharmintent;
/// <summary>
/// Content that encourages performing acts of self-harm, such as suicide, cutting, and eating disorders,
/// or that gives instructions or advice on how to commit such acts.
/// </summary>
property SelfHarmOrInstructions: Boolean read FSelfharminstructions write FSelfharminstructions;
/// <summary>
/// Content meant to arouse sexual excitement, such as the description of sexual activity,
/// or that promotes sexual services (excluding sex education and wellness).
/// </summary>
property Sexual: Boolean read FSexual write FSexual;
/// <summary>
/// Sexual content that includes an individual who is under 18 years old.
/// </summary>
property SexualOrMinors: Boolean read FSexualminors write FSexualminors;
/// <summary>
/// Content that depicts death, violence, or physical injury.
/// </summary>
property Violence: Boolean read FViolence write FViolence;
/// <summary>
/// Content that depicts death, violence, or physical injury in graphic detail.
/// </summary>
property ViolenceOrGraphic: Boolean read FViolencegraphic write FViolencegraphic;
end;
TResult = class
private
FCategories: TCategories;
FCategory_scores: TCategoryScores;
FFlagged: Boolean;
public
/// <summary>
/// A list of the categories, and whether they are flagged or not.
/// </summary>
property Categories: TCategories read FCategories write FCategories;
/// <summary>
/// A list of the categories along with their scores as predicted by model.
/// </summary>
property CategoryScores: TCategoryScores read FCategory_scores write FCategory_scores;
/// <summary>
/// Whether the content violates OpenAI's usage policies.
/// </summary>
/// <seealso>https://platform.openai.com/policies/usage-policies</seealso>
property Flagged: Boolean read FFlagged write FFlagged;
destructor Destroy; override;
end;
/// <summary>
/// Represents policy compliance report by OpenAI's content moderation model against a given input.
/// </summary>
TModerations = class
private
FId: string;
FModel: string;
FResults: TArray<TResult>;
public
/// <summary>
/// The unique identifier for the moderation request.
/// </summary>
property Id: string read FId write FId;
/// <summary>
/// The model used to generate the moderation results.
/// </summary>
property Model: string read FModel write FModel;
/// <summary>
/// A list of moderation objects.
/// </summary>
property Results: TArray<TResult> read FResults write FResults;
destructor Destroy; override;
end;
TModerationsRoute = class(TOpenAIAPIRoute)
public
/// <summary>
/// Classifies if text violates OpenAI's Content Policy
/// </summary>
function Create(ParamProc: TProc<TModerationsParams>): TModerations;
end;
implementation
{ TModerationsRoute }
function TModerationsRoute.Create(ParamProc: TProc<TModerationsParams>): TModerations;
begin
Result := API.Post<TModerations, TModerationsParams>('moderations', ParamProc);
end;
{ TModerationsParams }
constructor TModerationsParams.Create;
begin
inherited;
Model();
end;
function TModerationsParams.Input(const Value: TArray<string>): TModerationsParams;
begin
Result := TModerationsParams(Add('input', Value));
end;
function TModerationsParams.Model(const Value: string): TModerationsParams;
begin
Result := TModerationsParams(Add('model', Value));
end;
function TModerationsParams.Input(const Value: string): TModerationsParams;
begin
Result := TModerationsParams(Add('input', Value));
end;
{ TResult }
destructor TResult.Destroy;
begin
FCategories.Free;
FCategory_scores.Free;
inherited;
end;
{ TModerations }
destructor TModerations.Destroy;
var
Item: TResult;
begin
for Item in FResults do
Item.Free;
inherited;
end;
end.