diff options
| author | Xe Iaso <me@xeiaso.net> | 2023-07-13 07:48:30 -0400 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2023-07-13 07:48:41 -0400 |
| commit | 4574a25067f0b5692f4083e205cd0a093d86dc2c (patch) | |
| tree | 8c85d7faaa6e0f93d581f397f4d09891d4994d9c /web | |
| parent | d7ef6058cf4f9acc2675557f03379a703d55460f (diff) | |
| download | x-4574a25067f0b5692f4083e205cd0a093d86dc2c.tar.xz x-4574a25067f0b5692f4083e205cd0a093d86dc2c.zip | |
web/openai/moderation: helper methods
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'web')
| -rw-r--r-- | web/openai/moderation/moderation.go | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/web/openai/moderation/moderation.go b/web/openai/moderation/moderation.go index 6127d9c..7f73bf2 100644 --- a/web/openai/moderation/moderation.go +++ b/web/openai/moderation/moderation.go @@ -4,7 +4,9 @@ import ( "bytes" "context" "encoding/json" + "fmt" "net/http" + "strings" ) type Request struct { @@ -16,6 +18,56 @@ type Response struct { Model string `json:"model"` Results []Results `json:"results"` } + +func (r Response) Flagged() bool { + var result bool + + for _, set := range r.Results { + if set.Flagged { + result = true + } + } + + return result +} + +func (r Response) Reasons() string { + if !r.Flagged() { + return "" + } + + var sb strings.Builder + + fmt.Fprintln(&sb, "Your request failed for the following reasons:") + fmt.Fprintln(&sb) + + for _, set := range r.Results { + if set.Categories.Hate { + fmt.Fprintln(&sb, "- hate") + } + if set.Categories.HateThreatening { + fmt.Fprintln(&sb, "- hate (threatening)") + } + if set.Categories.SelfHarm { + fmt.Fprintln(&sb, "- self harm") + } + if set.Categories.Sexual { + fmt.Fprintln(&sb, "- sexual") + } + if set.Categories.SexualMinors { + fmt.Fprintln(&sb, "- sexual (minors)") + } + if set.Categories.Violence { + fmt.Fprintln(&sb, "- violence") + } + if set.Categories.ViolenceGraphic { + fmt.Fprintln(&sb, "- violence (graphic)") + } + } + + return sb.String() +} + type Categories struct { Hate bool `json:"hate"` HateThreatening bool `json:"hate/threatening"` |
