|
| 1 | +package openai |
| 2 | + |
| 3 | +import ( |
| 4 | + // Packages |
| 5 | + "context" |
| 6 | + |
| 7 | + client "github.com/mutablelogic/go-client" |
| 8 | +) |
| 9 | + |
| 10 | +/////////////////////////////////////////////////////////////////////////////// |
| 11 | +// TYPES |
| 12 | + |
| 13 | +type reqModerations struct { |
| 14 | + options |
| 15 | + Input []string `json:"input"` |
| 16 | +} |
| 17 | + |
| 18 | +type responseModerations struct { |
| 19 | + Id string `json:"id"` |
| 20 | + Model string `json:"model"` |
| 21 | + Results []Moderation `json:"results"` |
| 22 | +} |
| 23 | + |
| 24 | +type Moderation struct { |
| 25 | + Flagged bool `json:"flagged"` |
| 26 | + Categories struct { |
| 27 | + Sexual bool `json:"sexual,omitempty"` |
| 28 | + Hate bool `json:"hate,omitempty"` |
| 29 | + Harassment bool `json:"harassment,omitempty"` |
| 30 | + SelfHarm bool `json:"self-harm,omitempty"` |
| 31 | + SexualMinors bool `json:"sexual/minors,omitempty"` |
| 32 | + HateThreatening bool `json:"hate/threatening,omitempty"` |
| 33 | + ViolenceGraphic bool `json:"violence/graphic,omitempty"` |
| 34 | + SelfHarmIntent bool `json:"self-harm/intent,omitempty"` |
| 35 | + HarasssmentThreatening bool `json:"harassment/threatening,omitempty"` |
| 36 | + Violence bool `json:"violence,omitempty"` |
| 37 | + } `json:"categories,omitempty" writer:",wrap"` |
| 38 | + CategoryScores struct { |
| 39 | + Sexual float32 `json:"sexual,omitempty"` |
| 40 | + Hate float32 `json:"hate,omitempty"` |
| 41 | + Harassment float32 `json:"harassment,omitempty"` |
| 42 | + SelfHarm float32 `json:"self-harm,omitempty"` |
| 43 | + SexualMinors float32 `json:"sexual/minors,omitempty"` |
| 44 | + HateThreatening float32 `json:"hate/threatening,omitempty"` |
| 45 | + ViolenceGraphic float32 `json:"violence/graphic,omitempty"` |
| 46 | + SelfHarmIntent float32 `json:"self-harm/intent,omitempty"` |
| 47 | + HarasssmentThreatening float32 `json:"harassment/threatening,omitempty"` |
| 48 | + Violence float32 `json:"violence,omitempty"` |
| 49 | + } `json:"category_scores,omitempty" writer:",wrap"` |
| 50 | +} |
| 51 | + |
| 52 | +/////////////////////////////////////////////////////////////////////////////// |
| 53 | +// GLOBALS |
| 54 | + |
| 55 | +const ( |
| 56 | + defaultModerationModel = "text-moderation-latest" |
| 57 | +) |
| 58 | + |
| 59 | +/////////////////////////////////////////////////////////////////////////////// |
| 60 | +// API CALLS |
| 61 | + |
| 62 | +// Classifies if text is potentially harmful |
| 63 | +func (c *Client) Moderations(ctx context.Context, text []string, opts ...Opt) ([]Moderation, error) { |
| 64 | + var request reqModerations |
| 65 | + var response responseModerations |
| 66 | + |
| 67 | + // Set options |
| 68 | + request.Model = defaultModerationModel |
| 69 | + request.Input = text |
| 70 | + for _, opt := range opts { |
| 71 | + if err := opt(&request.options); err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Request->Response |
| 77 | + if payload, err := client.NewJSONRequest(request); err != nil { |
| 78 | + return nil, err |
| 79 | + } else if err := c.DoWithContext(ctx, payload, &response, client.OptPath("moderations")); err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + |
| 83 | + // Return success |
| 84 | + return response.Results, nil |
| 85 | +} |
0 commit comments