1// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
3package openai
4
5import (
6 "context"
7 "net/http"
8
9 "github.com/openai/openai-go/internal/apijson"
10 "github.com/openai/openai-go/internal/requestconfig"
11 "github.com/openai/openai-go/option"
12 "github.com/openai/openai-go/packages/param"
13 "github.com/openai/openai-go/packages/respjson"
14 "github.com/openai/openai-go/shared/constant"
15)
16
17// ModerationService contains methods and other services that help with interacting
18// with the openai API.
19//
20// Note, unlike clients, this service does not read variables from the environment
21// automatically. You should not instantiate this service directly, and instead use
22// the [NewModerationService] method instead.
23type ModerationService struct {
24 Options []option.RequestOption
25}
26
27// NewModerationService generates a new service that applies the given options to
28// each request. These options are applied after the parent client's options (if
29// there is one), and before any request-specific options.
30func NewModerationService(opts ...option.RequestOption) (r ModerationService) {
31 r = ModerationService{}
32 r.Options = opts
33 return
34}
35
36// Classifies if text and/or image inputs are potentially harmful. Learn more in
37// the [moderation guide](https://platform.openai.com/docs/guides/moderation).
38func (r *ModerationService) New(ctx context.Context, body ModerationNewParams, opts ...option.RequestOption) (res *ModerationNewResponse, err error) {
39 opts = append(r.Options[:], opts...)
40 path := "moderations"
41 err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
42 return
43}
44
45type Moderation struct {
46 // A list of the categories, and whether they are flagged or not.
47 Categories ModerationCategories `json:"categories,required"`
48 // A list of the categories along with the input type(s) that the score applies to.
49 CategoryAppliedInputTypes ModerationCategoryAppliedInputTypes `json:"category_applied_input_types,required"`
50 // A list of the categories along with their scores as predicted by model.
51 CategoryScores ModerationCategoryScores `json:"category_scores,required"`
52 // Whether any of the below categories are flagged.
53 Flagged bool `json:"flagged,required"`
54 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
55 JSON struct {
56 Categories respjson.Field
57 CategoryAppliedInputTypes respjson.Field
58 CategoryScores respjson.Field
59 Flagged respjson.Field
60 ExtraFields map[string]respjson.Field
61 raw string
62 } `json:"-"`
63}
64
65// Returns the unmodified JSON received from the API
66func (r Moderation) RawJSON() string { return r.JSON.raw }
67func (r *Moderation) UnmarshalJSON(data []byte) error {
68 return apijson.UnmarshalRoot(data, r)
69}
70
71// A list of the categories, and whether they are flagged or not.
72type ModerationCategories struct {
73 // Content that expresses, incites, or promotes harassing language towards any
74 // target.
75 Harassment bool `json:"harassment,required"`
76 // Harassment content that also includes violence or serious harm towards any
77 // target.
78 HarassmentThreatening bool `json:"harassment/threatening,required"`
79 // Content that expresses, incites, or promotes hate based on race, gender,
80 // ethnicity, religion, nationality, sexual orientation, disability status, or
81 // caste. Hateful content aimed at non-protected groups (e.g., chess players) is
82 // harassment.
83 Hate bool `json:"hate,required"`
84 // Hateful content that also includes violence or serious harm towards the targeted
85 // group based on race, gender, ethnicity, religion, nationality, sexual
86 // orientation, disability status, or caste.
87 HateThreatening bool `json:"hate/threatening,required"`
88 // Content that includes instructions or advice that facilitate the planning or
89 // execution of wrongdoing, or that gives advice or instruction on how to commit
90 // illicit acts. For example, "how to shoplift" would fit this category.
91 Illicit bool `json:"illicit,required"`
92 // Content that includes instructions or advice that facilitate the planning or
93 // execution of wrongdoing that also includes violence, or that gives advice or
94 // instruction on the procurement of any weapon.
95 IllicitViolent bool `json:"illicit/violent,required"`
96 // Content that promotes, encourages, or depicts acts of self-harm, such as
97 // suicide, cutting, and eating disorders.
98 SelfHarm bool `json:"self-harm,required"`
99 // Content that encourages performing acts of self-harm, such as suicide, cutting,
100 // and eating disorders, or that gives instructions or advice on how to commit such
101 // acts.
102 SelfHarmInstructions bool `json:"self-harm/instructions,required"`
103 // Content where the speaker expresses that they are engaging or intend to engage
104 // in acts of self-harm, such as suicide, cutting, and eating disorders.
105 SelfHarmIntent bool `json:"self-harm/intent,required"`
106 // Content meant to arouse sexual excitement, such as the description of sexual
107 // activity, or that promotes sexual services (excluding sex education and
108 // wellness).
109 Sexual bool `json:"sexual,required"`
110 // Sexual content that includes an individual who is under 18 years old.
111 SexualMinors bool `json:"sexual/minors,required"`
112 // Content that depicts death, violence, or physical injury.
113 Violence bool `json:"violence,required"`
114 // Content that depicts death, violence, or physical injury in graphic detail.
115 ViolenceGraphic bool `json:"violence/graphic,required"`
116 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
117 JSON struct {
118 Harassment respjson.Field
119 HarassmentThreatening respjson.Field
120 Hate respjson.Field
121 HateThreatening respjson.Field
122 Illicit respjson.Field
123 IllicitViolent respjson.Field
124 SelfHarm respjson.Field
125 SelfHarmInstructions respjson.Field
126 SelfHarmIntent respjson.Field
127 Sexual respjson.Field
128 SexualMinors respjson.Field
129 Violence respjson.Field
130 ViolenceGraphic respjson.Field
131 ExtraFields map[string]respjson.Field
132 raw string
133 } `json:"-"`
134}
135
136// Returns the unmodified JSON received from the API
137func (r ModerationCategories) RawJSON() string { return r.JSON.raw }
138func (r *ModerationCategories) UnmarshalJSON(data []byte) error {
139 return apijson.UnmarshalRoot(data, r)
140}
141
142// A list of the categories along with the input type(s) that the score applies to.
143type ModerationCategoryAppliedInputTypes struct {
144 // The applied input type(s) for the category 'harassment'.
145 //
146 // Any of "text".
147 Harassment []string `json:"harassment,required"`
148 // The applied input type(s) for the category 'harassment/threatening'.
149 //
150 // Any of "text".
151 HarassmentThreatening []string `json:"harassment/threatening,required"`
152 // The applied input type(s) for the category 'hate'.
153 //
154 // Any of "text".
155 Hate []string `json:"hate,required"`
156 // The applied input type(s) for the category 'hate/threatening'.
157 //
158 // Any of "text".
159 HateThreatening []string `json:"hate/threatening,required"`
160 // The applied input type(s) for the category 'illicit'.
161 //
162 // Any of "text".
163 Illicit []string `json:"illicit,required"`
164 // The applied input type(s) for the category 'illicit/violent'.
165 //
166 // Any of "text".
167 IllicitViolent []string `json:"illicit/violent,required"`
168 // The applied input type(s) for the category 'self-harm'.
169 //
170 // Any of "text", "image".
171 SelfHarm []string `json:"self-harm,required"`
172 // The applied input type(s) for the category 'self-harm/instructions'.
173 //
174 // Any of "text", "image".
175 SelfHarmInstructions []string `json:"self-harm/instructions,required"`
176 // The applied input type(s) for the category 'self-harm/intent'.
177 //
178 // Any of "text", "image".
179 SelfHarmIntent []string `json:"self-harm/intent,required"`
180 // The applied input type(s) for the category 'sexual'.
181 //
182 // Any of "text", "image".
183 Sexual []string `json:"sexual,required"`
184 // The applied input type(s) for the category 'sexual/minors'.
185 //
186 // Any of "text".
187 SexualMinors []string `json:"sexual/minors,required"`
188 // The applied input type(s) for the category 'violence'.
189 //
190 // Any of "text", "image".
191 Violence []string `json:"violence,required"`
192 // The applied input type(s) for the category 'violence/graphic'.
193 //
194 // Any of "text", "image".
195 ViolenceGraphic []string `json:"violence/graphic,required"`
196 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
197 JSON struct {
198 Harassment respjson.Field
199 HarassmentThreatening respjson.Field
200 Hate respjson.Field
201 HateThreatening respjson.Field
202 Illicit respjson.Field
203 IllicitViolent respjson.Field
204 SelfHarm respjson.Field
205 SelfHarmInstructions respjson.Field
206 SelfHarmIntent respjson.Field
207 Sexual respjson.Field
208 SexualMinors respjson.Field
209 Violence respjson.Field
210 ViolenceGraphic respjson.Field
211 ExtraFields map[string]respjson.Field
212 raw string
213 } `json:"-"`
214}
215
216// Returns the unmodified JSON received from the API
217func (r ModerationCategoryAppliedInputTypes) RawJSON() string { return r.JSON.raw }
218func (r *ModerationCategoryAppliedInputTypes) UnmarshalJSON(data []byte) error {
219 return apijson.UnmarshalRoot(data, r)
220}
221
222// A list of the categories along with their scores as predicted by model.
223type ModerationCategoryScores struct {
224 // The score for the category 'harassment'.
225 Harassment float64 `json:"harassment,required"`
226 // The score for the category 'harassment/threatening'.
227 HarassmentThreatening float64 `json:"harassment/threatening,required"`
228 // The score for the category 'hate'.
229 Hate float64 `json:"hate,required"`
230 // The score for the category 'hate/threatening'.
231 HateThreatening float64 `json:"hate/threatening,required"`
232 // The score for the category 'illicit'.
233 Illicit float64 `json:"illicit,required"`
234 // The score for the category 'illicit/violent'.
235 IllicitViolent float64 `json:"illicit/violent,required"`
236 // The score for the category 'self-harm'.
237 SelfHarm float64 `json:"self-harm,required"`
238 // The score for the category 'self-harm/instructions'.
239 SelfHarmInstructions float64 `json:"self-harm/instructions,required"`
240 // The score for the category 'self-harm/intent'.
241 SelfHarmIntent float64 `json:"self-harm/intent,required"`
242 // The score for the category 'sexual'.
243 Sexual float64 `json:"sexual,required"`
244 // The score for the category 'sexual/minors'.
245 SexualMinors float64 `json:"sexual/minors,required"`
246 // The score for the category 'violence'.
247 Violence float64 `json:"violence,required"`
248 // The score for the category 'violence/graphic'.
249 ViolenceGraphic float64 `json:"violence/graphic,required"`
250 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
251 JSON struct {
252 Harassment respjson.Field
253 HarassmentThreatening respjson.Field
254 Hate respjson.Field
255 HateThreatening respjson.Field
256 Illicit respjson.Field
257 IllicitViolent respjson.Field
258 SelfHarm respjson.Field
259 SelfHarmInstructions respjson.Field
260 SelfHarmIntent respjson.Field
261 Sexual respjson.Field
262 SexualMinors respjson.Field
263 Violence respjson.Field
264 ViolenceGraphic respjson.Field
265 ExtraFields map[string]respjson.Field
266 raw string
267 } `json:"-"`
268}
269
270// Returns the unmodified JSON received from the API
271func (r ModerationCategoryScores) RawJSON() string { return r.JSON.raw }
272func (r *ModerationCategoryScores) UnmarshalJSON(data []byte) error {
273 return apijson.UnmarshalRoot(data, r)
274}
275
276// An object describing an image to classify.
277//
278// The properties ImageURL, Type are required.
279type ModerationImageURLInputParam struct {
280 // Contains either an image URL or a data URL for a base64 encoded image.
281 ImageURL ModerationImageURLInputImageURLParam `json:"image_url,omitzero,required"`
282 // Always `image_url`.
283 //
284 // This field can be elided, and will marshal its zero value as "image_url".
285 Type constant.ImageURL `json:"type,required"`
286 paramObj
287}
288
289func (r ModerationImageURLInputParam) MarshalJSON() (data []byte, err error) {
290 type shadow ModerationImageURLInputParam
291 return param.MarshalObject(r, (*shadow)(&r))
292}
293func (r *ModerationImageURLInputParam) UnmarshalJSON(data []byte) error {
294 return apijson.UnmarshalRoot(data, r)
295}
296
297// Contains either an image URL or a data URL for a base64 encoded image.
298//
299// The property URL is required.
300type ModerationImageURLInputImageURLParam struct {
301 // Either a URL of the image or the base64 encoded image data.
302 URL string `json:"url,required" format:"uri"`
303 paramObj
304}
305
306func (r ModerationImageURLInputImageURLParam) MarshalJSON() (data []byte, err error) {
307 type shadow ModerationImageURLInputImageURLParam
308 return param.MarshalObject(r, (*shadow)(&r))
309}
310func (r *ModerationImageURLInputImageURLParam) UnmarshalJSON(data []byte) error {
311 return apijson.UnmarshalRoot(data, r)
312}
313
314type ModerationModel = string
315
316const (
317 ModerationModelOmniModerationLatest ModerationModel = "omni-moderation-latest"
318 ModerationModelOmniModeration2024_09_26 ModerationModel = "omni-moderation-2024-09-26"
319 ModerationModelTextModerationLatest ModerationModel = "text-moderation-latest"
320 ModerationModelTextModerationStable ModerationModel = "text-moderation-stable"
321)
322
323func ModerationMultiModalInputParamOfImageURL(imageURL ModerationImageURLInputImageURLParam) ModerationMultiModalInputUnionParam {
324 var variant ModerationImageURLInputParam
325 variant.ImageURL = imageURL
326 return ModerationMultiModalInputUnionParam{OfImageURL: &variant}
327}
328
329func ModerationMultiModalInputParamOfText(text string) ModerationMultiModalInputUnionParam {
330 var variant ModerationTextInputParam
331 variant.Text = text
332 return ModerationMultiModalInputUnionParam{OfText: &variant}
333}
334
335// Only one field can be non-zero.
336//
337// Use [param.IsOmitted] to confirm if a field is set.
338type ModerationMultiModalInputUnionParam struct {
339 OfImageURL *ModerationImageURLInputParam `json:",omitzero,inline"`
340 OfText *ModerationTextInputParam `json:",omitzero,inline"`
341 paramUnion
342}
343
344func (u ModerationMultiModalInputUnionParam) MarshalJSON() ([]byte, error) {
345 return param.MarshalUnion(u, u.OfImageURL, u.OfText)
346}
347func (u *ModerationMultiModalInputUnionParam) UnmarshalJSON(data []byte) error {
348 return apijson.UnmarshalRoot(data, u)
349}
350
351func (u *ModerationMultiModalInputUnionParam) asAny() any {
352 if !param.IsOmitted(u.OfImageURL) {
353 return u.OfImageURL
354 } else if !param.IsOmitted(u.OfText) {
355 return u.OfText
356 }
357 return nil
358}
359
360// Returns a pointer to the underlying variant's property, if present.
361func (u ModerationMultiModalInputUnionParam) GetImageURL() *ModerationImageURLInputImageURLParam {
362 if vt := u.OfImageURL; vt != nil {
363 return &vt.ImageURL
364 }
365 return nil
366}
367
368// Returns a pointer to the underlying variant's property, if present.
369func (u ModerationMultiModalInputUnionParam) GetText() *string {
370 if vt := u.OfText; vt != nil {
371 return &vt.Text
372 }
373 return nil
374}
375
376// Returns a pointer to the underlying variant's property, if present.
377func (u ModerationMultiModalInputUnionParam) GetType() *string {
378 if vt := u.OfImageURL; vt != nil {
379 return (*string)(&vt.Type)
380 } else if vt := u.OfText; vt != nil {
381 return (*string)(&vt.Type)
382 }
383 return nil
384}
385
386func init() {
387 apijson.RegisterUnion[ModerationMultiModalInputUnionParam](
388 "type",
389 apijson.Discriminator[ModerationImageURLInputParam]("image_url"),
390 apijson.Discriminator[ModerationTextInputParam]("text"),
391 )
392}
393
394// An object describing text to classify.
395//
396// The properties Text, Type are required.
397type ModerationTextInputParam struct {
398 // A string of text to classify.
399 Text string `json:"text,required"`
400 // Always `text`.
401 //
402 // This field can be elided, and will marshal its zero value as "text".
403 Type constant.Text `json:"type,required"`
404 paramObj
405}
406
407func (r ModerationTextInputParam) MarshalJSON() (data []byte, err error) {
408 type shadow ModerationTextInputParam
409 return param.MarshalObject(r, (*shadow)(&r))
410}
411func (r *ModerationTextInputParam) UnmarshalJSON(data []byte) error {
412 return apijson.UnmarshalRoot(data, r)
413}
414
415// Represents if a given text input is potentially harmful.
416type ModerationNewResponse struct {
417 // The unique identifier for the moderation request.
418 ID string `json:"id,required"`
419 // The model used to generate the moderation results.
420 Model string `json:"model,required"`
421 // A list of moderation objects.
422 Results []Moderation `json:"results,required"`
423 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
424 JSON struct {
425 ID respjson.Field
426 Model respjson.Field
427 Results respjson.Field
428 ExtraFields map[string]respjson.Field
429 raw string
430 } `json:"-"`
431}
432
433// Returns the unmodified JSON received from the API
434func (r ModerationNewResponse) RawJSON() string { return r.JSON.raw }
435func (r *ModerationNewResponse) UnmarshalJSON(data []byte) error {
436 return apijson.UnmarshalRoot(data, r)
437}
438
439type ModerationNewParams struct {
440 // Input (or inputs) to classify. Can be a single string, an array of strings, or
441 // an array of multi-modal input objects similar to other models.
442 Input ModerationNewParamsInputUnion `json:"input,omitzero,required"`
443 // The content moderation model you would like to use. Learn more in
444 // [the moderation guide](https://platform.openai.com/docs/guides/moderation), and
445 // learn about available models
446 // [here](https://platform.openai.com/docs/models#moderation).
447 Model ModerationModel `json:"model,omitzero"`
448 paramObj
449}
450
451func (r ModerationNewParams) MarshalJSON() (data []byte, err error) {
452 type shadow ModerationNewParams
453 return param.MarshalObject(r, (*shadow)(&r))
454}
455func (r *ModerationNewParams) UnmarshalJSON(data []byte) error {
456 return apijson.UnmarshalRoot(data, r)
457}
458
459// Only one field can be non-zero.
460//
461// Use [param.IsOmitted] to confirm if a field is set.
462type ModerationNewParamsInputUnion struct {
463 OfString param.Opt[string] `json:",omitzero,inline"`
464 OfStringArray []string `json:",omitzero,inline"`
465 OfModerationMultiModalArray []ModerationMultiModalInputUnionParam `json:",omitzero,inline"`
466 paramUnion
467}
468
469func (u ModerationNewParamsInputUnion) MarshalJSON() ([]byte, error) {
470 return param.MarshalUnion(u, u.OfString, u.OfStringArray, u.OfModerationMultiModalArray)
471}
472func (u *ModerationNewParamsInputUnion) UnmarshalJSON(data []byte) error {
473 return apijson.UnmarshalRoot(data, u)
474}
475
476func (u *ModerationNewParamsInputUnion) asAny() any {
477 if !param.IsOmitted(u.OfString) {
478 return &u.OfString.Value
479 } else if !param.IsOmitted(u.OfStringArray) {
480 return &u.OfStringArray
481 } else if !param.IsOmitted(u.OfModerationMultiModalArray) {
482 return &u.OfModerationMultiModalArray
483 }
484 return nil
485}