Skip to content

Commit 97228f1

Browse files
committed
Added moderations
1 parent 2cd4a17 commit 97228f1

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

cmd/api/openai.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func openaiRegister(flags *Flags) {
7676
{Name: "transcribe", Call: openaiTranscribe, Description: "Transcribes audio into the input language", MinArgs: 1, MaxArgs: 1, Syntax: "<filename>"},
7777
{Name: "translate", Call: openaiTranslate, Description: "Translates audio into English", MinArgs: 1, MaxArgs: 1, Syntax: "<filename>"},
7878
{Name: "say", Call: openaiTextToSpeech, Description: "Text to speech", MinArgs: 2, Syntax: "<voice-id> <text>..."},
79+
{Name: "moderations", Call: openaiModerations, Description: "Classifies text across several categories", MinArgs: 1, Syntax: "<text>..."},
7980
},
8081
})
8182
}
@@ -342,3 +343,19 @@ func openaiTextToSpeech(ctx context.Context, w *tablewriter.Writer, args []strin
342343
// Return success
343344
return nil
344345
}
346+
347+
func openaiModerations(ctx context.Context, w *tablewriter.Writer, args []string) error {
348+
opts := []openai.Opt{}
349+
if openaiModel != "" {
350+
opts = append(opts, openai.OptModel(openaiModel))
351+
}
352+
353+
// Request -> Response
354+
response, err := openaiClient.Moderations(ctx, args, opts...)
355+
if err != nil {
356+
return err
357+
}
358+
359+
// Write output
360+
return w.Write(response)
361+
}

pkg/openai/moderations.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)