-
Notifications
You must be signed in to change notification settings - Fork 4
/
moderation.go
70 lines (59 loc) · 2.1 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
package openai
import (
"context"
"encoding/json"
"github.com/fabiustech/openai/models"
"github.com/fabiustech/openai/routes"
)
// ModerationRequest contains all relevant fields for requests to the moderations endpoint.
type ModerationRequest struct {
// Input is the input text to classify.
Input string `json:"input,omitempty"`
// Model specifies the model to use for moderation.
// Defaults to models.TextModerationLatest.
Model models.Moderation `json:"model,omitempty"`
}
// Result represents one of possible moderation results.
type Result struct {
Categories *ResultCategories `json:"categories"`
CategoryScores *ResultCategoryScores `json:"category_scores"`
Flagged bool `json:"flagged"`
}
// ResultCategories represents Categories of Result.
type ResultCategories struct {
Hate bool `json:"hate"`
HateThreatening bool `json:"hate/threatening"`
SelfHarm bool `json:"self-harm"`
Sexual bool `json:"sexual"`
SexualMinors bool `json:"sexual/minors"`
Violence bool `json:"violence"`
ViolenceGraphic bool `json:"violence/graphic"`
}
// ResultCategoryScores represents CategoryScores of Result.
type ResultCategoryScores struct {
Hate float32 `json:"hate"`
HateThreatening float32 `json:"hate/threatening"`
SelfHarm float32 `json:"self-harm"`
Sexual float32 `json:"sexual"`
SexualMinors float32 `json:"sexual/minors"`
Violence float32 `json:"violence"`
ViolenceGraphic float32 `json:"violence/graphic"`
}
// ModerationResponse represents a response structure for moderation API.
type ModerationResponse struct {
ID string `json:"id"`
Model string `json:"model"`
Results []Result `json:"results"`
}
// CreateModeration classifies if text violates OpenAI's Content Policy.
func (c *Client) CreateModeration(ctx context.Context, mr *ModerationRequest) (*ModerationResponse, error) {
var b, err = c.post(ctx, routes.Moderations, mr)
if err != nil {
return nil, err
}
var resp = &ModerationResponse{}
if err = json.Unmarshal(b, resp); err != nil {
return nil, err
}
return resp, nil
}