- Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathmoderation.go
107 lines (94 loc) · 3.82 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
package openai
import (
"context"
"errors"
"net/http"
)
// The moderation endpoint is a tool you can use to check whether content complies with OpenAI's usage policies.
// Developers can thus identify content that our usage policies prohibits and take action, for instance by filtering it.
// 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.
const (
ModerationOmniLatest="omni-moderation-latest"
ModerationOmni20240926="omni-moderation-2024-09-26"
ModerationTextStable="text-moderation-stable"
ModerationTextLatest="text-moderation-latest"
// Deprecated: use ModerationTextStable and ModerationTextLatest instead.
ModerationText001="text-moderation-001"
)
var (
ErrModerationInvalidModel=errors.New("this model is not supported with moderation, please use text-moderation-stable or text-moderation-latest instead") //nolint:lll
)
varvalidModerationModel=map[string]struct{}{
ModerationOmniLatest: {},
ModerationOmni20240926: {},
ModerationTextStable: {},
ModerationTextLatest: {},
}
// ModerationRequest represents a request structure for moderation API.
typeModerationRequeststruct {
Inputstring`json:"input,omitempty"`
Modelstring`json:"model,omitempty"`
}
// Result represents one of possible moderation results.
typeResultstruct {
CategoriesResultCategories`json:"categories"`
CategoryScoresResultCategoryScores`json:"category_scores"`
Flaggedbool`json:"flagged"`
}
// ResultCategories represents Categories of Result.
typeResultCategoriesstruct {
Hatebool`json:"hate"`
HateThreateningbool`json:"hate/threatening"`
Harassmentbool`json:"harassment"`
HarassmentThreateningbool`json:"harassment/threatening"`
SelfHarmbool`json:"self-harm"`
SelfHarmIntentbool`json:"self-harm/intent"`
SelfHarmInstructionsbool`json:"self-harm/instructions"`
Sexualbool`json:"sexual"`
SexualMinorsbool`json:"sexual/minors"`
Violencebool`json:"violence"`
ViolenceGraphicbool`json:"violence/graphic"`
}
// ResultCategoryScores represents CategoryScores of Result.
typeResultCategoryScoresstruct {
Hatefloat32`json:"hate"`
HateThreateningfloat32`json:"hate/threatening"`
Harassmentfloat32`json:"harassment"`
HarassmentThreateningfloat32`json:"harassment/threatening"`
SelfHarmfloat32`json:"self-harm"`
SelfHarmIntentfloat32`json:"self-harm/intent"`
SelfHarmInstructionsfloat32`json:"self-harm/instructions"`
Sexualfloat32`json:"sexual"`
SexualMinorsfloat32`json:"sexual/minors"`
Violencefloat32`json:"violence"`
ViolenceGraphicfloat32`json:"violence/graphic"`
}
// ModerationResponse represents a response structure for moderation API.
typeModerationResponsestruct {
IDstring`json:"id"`
Modelstring`json:"model"`
Results []Result`json:"results"`
httpHeader
}
// Moderations — perform a moderation api call over a string.
// Input can be an array or slice but a string will reduce the complexity.
func (c*Client) Moderations(ctx context.Context, requestModerationRequest) (responseModerationResponse, errerror) {
if_, ok:=validModerationModel[request.Model]; len(request.Model) >0&&!ok {
err=ErrModerationInvalidModel
return
}
req, err:=c.newRequest(
ctx,
http.MethodPost,
c.fullURL("/moderations", withModel(request.Model)),
withBody(&request),
)
iferr!=nil {
return
}
err=c.sendRequest(req, &response)
return
}