From f69f821999f55c39ec29879d5c58bdfba2771ca3 Mon Sep 17 00:00:00 2001 From: Arihant Jain Date: Wed, 5 Apr 2023 09:22:19 +0530 Subject: [PATCH] Add moderation example (#23) --- examples/moderation/main.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 examples/moderation/main.go diff --git a/examples/moderation/main.go b/examples/moderation/main.go new file mode 100644 index 0000000..97ab1e4 --- /dev/null +++ b/examples/moderation/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "context" + "log" + "os" + + "github.com/rakyll/openai-go" + "github.com/rakyll/openai-go/moderation" +) + +func main() { + ctx := context.Background() + s := openai.NewSession(os.Getenv("OPENAI_API_KEY")) + + client := moderation.NewClient(s, "text-moderation-latest") + resp, err := client.Create(ctx, &moderation.CreateParams{ + Input: []string{"I will kill you"}, + }) + if err != nil { + log.Fatalf("Failed to complete: %v", err) + } + + for _, result := range resp.Results { + log.Println("Content moderation is flagged as", result.Flagged) + if result.Flagged { + for key, value := range result.Categories { + if value { + log.Println("Content category is", key) + } + } + } + } +}