moderation.go

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