- Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathmoderation.go
505 lines (466 loc) · 19.7 KB
/
moderation.go
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package openai
import (
"context"
"net/http"
"reflect"
"github.com/openai/openai-go/internal/apijson"
"github.com/openai/openai-go/internal/requestconfig"
"github.com/openai/openai-go/option"
"github.com/openai/openai-go/packages/param"
"github.com/openai/openai-go/packages/resp"
"github.com/openai/openai-go/shared/constant"
"github.com/tidwall/gjson"
)
// ModerationService contains methods and other services that help with interacting
// with the openai API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewModerationService] method instead.
typeModerationServicestruct {
Options []option.RequestOption
}
// NewModerationService generates a new service that applies the given options to
// each request. These options are applied after the parent client's options (if
// there is one), and before any request-specific options.
funcNewModerationService(opts...option.RequestOption) (rModerationService) {
r=ModerationService{}
r.Options=opts
return
}
// Classifies if text and/or image inputs are potentially harmful. Learn more in
// the [moderation guide](https://platform.openai.com/docs/guides/moderation).
func (r*ModerationService) New(ctx context.Context, bodyModerationNewParams, opts...option.RequestOption) (res*ModerationNewResponse, errerror) {
opts=append(r.Options[:], opts...)
path:="moderations"
err=requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
typeModerationstruct {
// A list of the categories, and whether they are flagged or not.
CategoriesModerationCategories`json:"categories,required"`
// A list of the categories along with the input type(s) that the score applies to.
CategoryAppliedInputTypesModerationCategoryAppliedInputTypes`json:"category_applied_input_types,required"`
// A list of the categories along with their scores as predicted by model.
CategoryScoresModerationCategoryScores`json:"category_scores,required"`
// Whether any of the below categories are flagged.
Flaggedbool`json:"flagged,required"`
// Metadata for the response, check the presence of optional fields with the
// [resp.Field.IsPresent] method.
JSONstruct {
Categories resp.Field
CategoryAppliedInputTypes resp.Field
CategoryScores resp.Field
Flagged resp.Field
ExtraFieldsmap[string]resp.Field
rawstring
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (rModeration) RawJSON() string { returnr.JSON.raw }
func (r*Moderation) UnmarshalJSON(data []byte) error {
returnapijson.UnmarshalRoot(data, r)
}
// A list of the categories, and whether they are flagged or not.
typeModerationCategoriesstruct {
// Content that expresses, incites, or promotes harassing language towards any
// target.
Harassmentbool`json:"harassment,required"`
// Harassment content that also includes violence or serious harm towards any
// target.
HarassmentThreateningbool`json:"harassment/threatening,required"`
// 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
// harassment.
Hatebool`json:"hate,required"`
// 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.
HateThreateningbool`json:"hate/threatening,required"`
// Content that includes instructions or advice that facilitate the planning or
// execution of wrongdoing, or that gives advice or instruction on how to commit
// illicit acts. For example, "how to shoplift" would fit this category.
Illicitbool`json:"illicit,required"`
// Content that includes instructions or advice that facilitate the planning or
// execution of wrongdoing that also includes violence, or that gives advice or
// instruction on the procurement of any weapon.
IllicitViolentbool`json:"illicit/violent,required"`
// Content that promotes, encourages, or depicts acts of self-harm, such as
// suicide, cutting, and eating disorders.
SelfHarmbool`json:"self-harm,required"`
// 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.
SelfHarmInstructionsbool`json:"self-harm/instructions,required"`
// 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.
SelfHarmIntentbool`json:"self-harm/intent,required"`
// Content meant to arouse sexual excitement, such as the description of sexual
// activity, or that promotes sexual services (excluding sex education and
// wellness).
Sexualbool`json:"sexual,required"`
// Sexual content that includes an individual who is under 18 years old.
SexualMinorsbool`json:"sexual/minors,required"`
// Content that depicts death, violence, or physical injury.
Violencebool`json:"violence,required"`
// Content that depicts death, violence, or physical injury in graphic detail.
ViolenceGraphicbool`json:"violence/graphic,required"`
// Metadata for the response, check the presence of optional fields with the
// [resp.Field.IsPresent] method.
JSONstruct {
Harassment resp.Field
HarassmentThreatening resp.Field
Hate resp.Field
HateThreatening resp.Field
Illicit resp.Field
IllicitViolent resp.Field
SelfHarm resp.Field
SelfHarmInstructions resp.Field
SelfHarmIntent resp.Field
Sexual resp.Field
SexualMinors resp.Field
Violence resp.Field
ViolenceGraphic resp.Field
ExtraFieldsmap[string]resp.Field
rawstring
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (rModerationCategories) RawJSON() string { returnr.JSON.raw }
func (r*ModerationCategories) UnmarshalJSON(data []byte) error {
returnapijson.UnmarshalRoot(data, r)
}
// A list of the categories along with the input type(s) that the score applies to.
typeModerationCategoryAppliedInputTypesstruct {
// The applied input type(s) for the category 'harassment'.
//
// Any of "text".
Harassment []string`json:"harassment,required"`
// The applied input type(s) for the category 'harassment/threatening'.
//
// Any of "text".
HarassmentThreatening []string`json:"harassment/threatening,required"`
// The applied input type(s) for the category 'hate'.
//
// Any of "text".
Hate []string`json:"hate,required"`
// The applied input type(s) for the category 'hate/threatening'.
//
// Any of "text".
HateThreatening []string`json:"hate/threatening,required"`
// The applied input type(s) for the category 'illicit'.
//
// Any of "text".
Illicit []string`json:"illicit,required"`
// The applied input type(s) for the category 'illicit/violent'.
//
// Any of "text".
IllicitViolent []string`json:"illicit/violent,required"`
// The applied input type(s) for the category 'self-harm'.
//
// Any of "text", "image".
SelfHarm []string`json:"self-harm,required"`
// The applied input type(s) for the category 'self-harm/instructions'.
//
// Any of "text", "image".
SelfHarmInstructions []string`json:"self-harm/instructions,required"`
// The applied input type(s) for the category 'self-harm/intent'.
//
// Any of "text", "image".
SelfHarmIntent []string`json:"self-harm/intent,required"`
// The applied input type(s) for the category 'sexual'.
//
// Any of "text", "image".
Sexual []string`json:"sexual,required"`
// The applied input type(s) for the category 'sexual/minors'.
//
// Any of "text".
SexualMinors []string`json:"sexual/minors,required"`
// The applied input type(s) for the category 'violence'.
//
// Any of "text", "image".
Violence []string`json:"violence,required"`
// The applied input type(s) for the category 'violence/graphic'.
//
// Any of "text", "image".
ViolenceGraphic []string`json:"violence/graphic,required"`
// Metadata for the response, check the presence of optional fields with the
// [resp.Field.IsPresent] method.
JSONstruct {
Harassment resp.Field
HarassmentThreatening resp.Field
Hate resp.Field
HateThreatening resp.Field
Illicit resp.Field
IllicitViolent resp.Field
SelfHarm resp.Field
SelfHarmInstructions resp.Field
SelfHarmIntent resp.Field
Sexual resp.Field
SexualMinors resp.Field
Violence resp.Field
ViolenceGraphic resp.Field
ExtraFieldsmap[string]resp.Field
rawstring
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (rModerationCategoryAppliedInputTypes) RawJSON() string { returnr.JSON.raw }
func (r*ModerationCategoryAppliedInputTypes) UnmarshalJSON(data []byte) error {
returnapijson.UnmarshalRoot(data, r)
}
// A list of the categories along with their scores as predicted by model.
typeModerationCategoryScoresstruct {
// The score for the category 'harassment'.
Harassmentfloat64`json:"harassment,required"`
// The score for the category 'harassment/threatening'.
HarassmentThreateningfloat64`json:"harassment/threatening,required"`
// The score for the category 'hate'.
Hatefloat64`json:"hate,required"`
// The score for the category 'hate/threatening'.
HateThreateningfloat64`json:"hate/threatening,required"`
// The score for the category 'illicit'.
Illicitfloat64`json:"illicit,required"`
// The score for the category 'illicit/violent'.
IllicitViolentfloat64`json:"illicit/violent,required"`
// The score for the category 'self-harm'.
SelfHarmfloat64`json:"self-harm,required"`
// The score for the category 'self-harm/instructions'.
SelfHarmInstructionsfloat64`json:"self-harm/instructions,required"`
// The score for the category 'self-harm/intent'.
SelfHarmIntentfloat64`json:"self-harm/intent,required"`
// The score for the category 'sexual'.
Sexualfloat64`json:"sexual,required"`
// The score for the category 'sexual/minors'.
SexualMinorsfloat64`json:"sexual/minors,required"`
// The score for the category 'violence'.
Violencefloat64`json:"violence,required"`
// The score for the category 'violence/graphic'.
ViolenceGraphicfloat64`json:"violence/graphic,required"`
// Metadata for the response, check the presence of optional fields with the
// [resp.Field.IsPresent] method.
JSONstruct {
Harassment resp.Field
HarassmentThreatening resp.Field
Hate resp.Field
HateThreatening resp.Field
Illicit resp.Field
IllicitViolent resp.Field
SelfHarm resp.Field
SelfHarmInstructions resp.Field
SelfHarmIntent resp.Field
Sexual resp.Field
SexualMinors resp.Field
Violence resp.Field
ViolenceGraphic resp.Field
ExtraFieldsmap[string]resp.Field
rawstring
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (rModerationCategoryScores) RawJSON() string { returnr.JSON.raw }
func (r*ModerationCategoryScores) UnmarshalJSON(data []byte) error {
returnapijson.UnmarshalRoot(data, r)
}
// An object describing an image to classify.
//
// The properties ImageURL, Type are required.
typeModerationImageURLInputParamstruct {
// Contains either an image URL or a data URL for a base64 encoded image.
ImageURLModerationImageURLInputImageURLParam`json:"image_url,omitzero,required"`
// Always `image_url`.
//
// This field can be elided, and will marshal its zero value as "image_url".
Type constant.ImageURL`json:"type,required"`
paramObj
}
// IsPresent returns true if the field's value is not omitted and not the JSON
// "null". To check if this field is omitted, use [param.IsOmitted].
func (fModerationImageURLInputParam) IsPresent() bool { return!param.IsOmitted(f) &&!f.IsNull() }
func (rModerationImageURLInputParam) MarshalJSON() (data []byte, errerror) {
typeshadowModerationImageURLInputParam
returnparam.MarshalObject(r, (*shadow)(&r))
}
// Contains either an image URL or a data URL for a base64 encoded image.
//
// The property URL is required.
typeModerationImageURLInputImageURLParamstruct {
// Either a URL of the image or the base64 encoded image data.
URLstring`json:"url,required" format:"uri"`
paramObj
}
// IsPresent returns true if the field's value is not omitted and not the JSON
// "null". To check if this field is omitted, use [param.IsOmitted].
func (fModerationImageURLInputImageURLParam) IsPresent() bool {
return!param.IsOmitted(f) &&!f.IsNull()
}
func (rModerationImageURLInputImageURLParam) MarshalJSON() (data []byte, errerror) {
typeshadowModerationImageURLInputImageURLParam
returnparam.MarshalObject(r, (*shadow)(&r))
}
typeModerationModel=string
const (
ModerationModelOmniModerationLatestModerationModel="omni-moderation-latest"
ModerationModelOmniModeration2024_09_26ModerationModel="omni-moderation-2024-09-26"
ModerationModelTextModerationLatestModerationModel="text-moderation-latest"
ModerationModelTextModerationStableModerationModel="text-moderation-stable"
)
funcModerationMultiModalInputParamOfImageURL(imageURLModerationImageURLInputImageURLParam) ModerationMultiModalInputUnionParam {
varvariantModerationImageURLInputParam
variant.ImageURL=imageURL
returnModerationMultiModalInputUnionParam{OfImageURL: &variant}
}
funcModerationMultiModalInputParamOfText(textstring) ModerationMultiModalInputUnionParam {
varvariantModerationTextInputParam
variant.Text=text
returnModerationMultiModalInputUnionParam{OfText: &variant}
}
// Only one field can be non-zero.
//
// Use [param.IsOmitted] to confirm if a field is set.
typeModerationMultiModalInputUnionParamstruct {
OfImageURL*ModerationImageURLInputParam`json:",omitzero,inline"`
OfText*ModerationTextInputParam`json:",omitzero,inline"`
paramUnion
}
// IsPresent returns true if the field's value is not omitted and not the JSON
// "null". To check if this field is omitted, use [param.IsOmitted].
func (uModerationMultiModalInputUnionParam) IsPresent() bool {
return!param.IsOmitted(u) &&!u.IsNull()
}
func (uModerationMultiModalInputUnionParam) MarshalJSON() ([]byte, error) {
returnparam.MarshalUnion[ModerationMultiModalInputUnionParam](u.OfImageURL, u.OfText)
}
func (u*ModerationMultiModalInputUnionParam) asAny() any {
if!param.IsOmitted(u.OfImageURL) {
returnu.OfImageURL
} elseif!param.IsOmitted(u.OfText) {
returnu.OfText
}
returnnil
}
// Returns a pointer to the underlying variant's property, if present.
func (uModerationMultiModalInputUnionParam) GetImageURL() *ModerationImageURLInputImageURLParam {
ifvt:=u.OfImageURL; vt!=nil {
return&vt.ImageURL
}
returnnil
}
// Returns a pointer to the underlying variant's property, if present.
func (uModerationMultiModalInputUnionParam) GetText() *string {
ifvt:=u.OfText; vt!=nil {
return&vt.Text
}
returnnil
}
// Returns a pointer to the underlying variant's property, if present.
func (uModerationMultiModalInputUnionParam) GetType() *string {
ifvt:=u.OfImageURL; vt!=nil {
return (*string)(&vt.Type)
} elseifvt:=u.OfText; vt!=nil {
return (*string)(&vt.Type)
}
returnnil
}
funcinit() {
apijson.RegisterUnion[ModerationMultiModalInputUnionParam](
"type",
apijson.UnionVariant{
TypeFilter: gjson.JSON,
Type: reflect.TypeOf(ModerationImageURLInputParam{}),
DiscriminatorValue: "image_url",
},
apijson.UnionVariant{
TypeFilter: gjson.JSON,
Type: reflect.TypeOf(ModerationTextInputParam{}),
DiscriminatorValue: "text",
},
)
}
// An object describing text to classify.
//
// The properties Text, Type are required.
typeModerationTextInputParamstruct {
// A string of text to classify.
Textstring`json:"text,required"`
// Always `text`.
//
// This field can be elided, and will marshal its zero value as "text".
Type constant.Text`json:"type,required"`
paramObj
}
// IsPresent returns true if the field's value is not omitted and not the JSON
// "null". To check if this field is omitted, use [param.IsOmitted].
func (fModerationTextInputParam) IsPresent() bool { return!param.IsOmitted(f) &&!f.IsNull() }
func (rModerationTextInputParam) MarshalJSON() (data []byte, errerror) {
typeshadowModerationTextInputParam
returnparam.MarshalObject(r, (*shadow)(&r))
}
// Represents if a given text input is potentially harmful.
typeModerationNewResponsestruct {
// The unique identifier for the moderation request.
IDstring`json:"id,required"`
// The model used to generate the moderation results.
Modelstring`json:"model,required"`
// A list of moderation objects.
Results []Moderation`json:"results,required"`
// Metadata for the response, check the presence of optional fields with the
// [resp.Field.IsPresent] method.
JSONstruct {
ID resp.Field
Model resp.Field
Results resp.Field
ExtraFieldsmap[string]resp.Field
rawstring
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (rModerationNewResponse) RawJSON() string { returnr.JSON.raw }
func (r*ModerationNewResponse) UnmarshalJSON(data []byte) error {
returnapijson.UnmarshalRoot(data, r)
}
typeModerationNewParamsstruct {
// Input (or inputs) to classify. Can be a single string, an array of strings, or
// an array of multi-modal input objects similar to other models.
InputModerationNewParamsInputUnion`json:"input,omitzero,required"`
// The content moderation model you would like to use. Learn more in
// [the moderation guide](https://platform.openai.com/docs/guides/moderation), and
// learn about available models
// [here](https://platform.openai.com/docs/models#moderation).
ModelModerationModel`json:"model,omitzero"`
paramObj
}
// IsPresent returns true if the field's value is not omitted and not the JSON
// "null". To check if this field is omitted, use [param.IsOmitted].
func (fModerationNewParams) IsPresent() bool { return!param.IsOmitted(f) &&!f.IsNull() }
func (rModerationNewParams) MarshalJSON() (data []byte, errerror) {
typeshadowModerationNewParams
returnparam.MarshalObject(r, (*shadow)(&r))
}
// Only one field can be non-zero.
//
// Use [param.IsOmitted] to confirm if a field is set.
typeModerationNewParamsInputUnionstruct {
OfString param.Opt[string] `json:",omitzero,inline"`
OfModerationNewsInputArray []string`json:",omitzero,inline"`
OfModerationMultiModalArray []ModerationMultiModalInputUnionParam`json:",omitzero,inline"`
paramUnion
}
// IsPresent returns true if the field's value is not omitted and not the JSON
// "null". To check if this field is omitted, use [param.IsOmitted].
func (uModerationNewParamsInputUnion) IsPresent() bool { return!param.IsOmitted(u) &&!u.IsNull() }
func (uModerationNewParamsInputUnion) MarshalJSON() ([]byte, error) {
returnparam.MarshalUnion[ModerationNewParamsInputUnion](u.OfString, u.OfModerationNewsInputArray, u.OfModerationMultiModalArray)
}
func (u*ModerationNewParamsInputUnion) asAny() any {
if!param.IsOmitted(u.OfString) {
return&u.OfString.Value
} elseif!param.IsOmitted(u.OfModerationNewsInputArray) {
return&u.OfModerationNewsInputArray
} elseif!param.IsOmitted(u.OfModerationMultiModalArray) {
return&u.OfModerationMultiModalArray
}
returnnil
}