|
| 1 | +package gogpt |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "net/http" |
| 8 | +) |
| 9 | + |
| 10 | +// ModerationRequest represents a request structure for moderation API |
| 11 | +type ModerationRequest struct { |
| 12 | + Input string `json:"input,omitempty"` |
| 13 | + Model *string `json:"model,omitempty"` |
| 14 | +} |
| 15 | + |
| 16 | +// Result represents one of possible moderation results |
| 17 | +type Result struct { |
| 18 | + Categories []ResultCategories `json:"categories"` |
| 19 | + CategoryScores []ResultCategoryScores `json:"category_scores"` |
| 20 | + Flagged int `json:"flagged"` |
| 21 | +} |
| 22 | + |
| 23 | +// ResultCategories represents Categories of Result |
| 24 | +type ResultCategories struct { |
| 25 | + Hate int `json:"hate"` |
| 26 | + HateThreatening int `json:"hate/threatening"` |
| 27 | + SelfHarm int `json:"self-harm"` |
| 28 | + Sexual int `json:"sexual"` |
| 29 | + SexualMinors int `json:"sexual/minors"` |
| 30 | + Violence int `json:"violence"` |
| 31 | + ViolenceGraphic int `json:"violence/graphic"` |
| 32 | +} |
| 33 | + |
| 34 | +// ResultCategoryScores represents CategoryScores of Result |
| 35 | +type ResultCategoryScores struct { |
| 36 | + Hate float32 `json:"hate"` |
| 37 | + HateThreatening float32 `json:"hate/threatening"` |
| 38 | + SelfHarm float32 `json:"self-harm"` |
| 39 | + Sexual float32 `json:"sexual"` |
| 40 | + SexualMinors float32 `json:"sexual/minors"` |
| 41 | + Violence float32 `json:"violence"` |
| 42 | + ViolenceGraphic float32 `json:"violence/graphic"` |
| 43 | +} |
| 44 | + |
| 45 | +// ModerationResponse represents a response structure for moderation API |
| 46 | +type ModerationResponse struct { |
| 47 | + ID string `json:"id"` |
| 48 | + Model string `json:"model"` |
| 49 | + Results []Result `json:"results"` |
| 50 | +} |
| 51 | + |
| 52 | +// Moderations — perform a moderation api call over a string. |
| 53 | +// Input can be an array or slice but a string will reduce the complexity. |
| 54 | +func (c *Client) Moderations(ctx context.Context, request ModerationRequest) (response ModerationResponse, err error) { |
| 55 | + var reqBytes []byte |
| 56 | + reqBytes, err = json.Marshal(request) |
| 57 | + if err != nil { |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + req, err := http.NewRequest("POST", c.fullURL("/moderations"), bytes.NewBuffer(reqBytes)) |
| 62 | + if err != nil { |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + req = req.WithContext(ctx) |
| 67 | + err = c.sendRequest(req, &response) |
| 68 | + return |
| 69 | +} |
0 commit comments