Skip to content

Commit

Permalink
Add moderation example (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
ariain authored Apr 5, 2023
1 parent c5b7976 commit f69f821
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions examples/moderation/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}
}

0 comments on commit f69f821

Please sign in to comment.