Skip to content

Commit

Permalink
fix: repair json decoding of moderation response (#670)
Browse files Browse the repository at this point in the history
  • Loading branch information
mheck136 committed Feb 28, 2024
1 parent 4103778 commit bb6149f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
22 changes: 11 additions & 11 deletions moderation.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ type ResultCategories struct {

// ResultCategoryScores represents CategoryScores of Result.
type ResultCategoryScores struct {
Hate bool `json:"hate"`
HateThreatening bool `json:"hate/threatening"`
Harassment bool `json:"harassment"`
HarassmentThreatening bool `json:"harassment/threatening"`
SelfHarm bool `json:"self-harm"`
SelfHarmIntent bool `json:"self-harm/intent"`
SelfHarmInstructions bool `json:"self-harm/instructions"`
Sexual bool `json:"sexual"`
SexualMinors bool `json:"sexual/minors"`
Violence bool `json:"violence"`
ViolenceGraphic bool `json:"violence/graphic"`
Hate float32 `json:"hate"`
HateThreatening float32 `json:"hate/threatening"`
Harassment float32 `json:"harassment"`
HarassmentThreatening float32 `json:"harassment/threatening"`
SelfHarm float32 `json:"self-harm"`
SelfHarmIntent float32 `json:"self-harm/intent"`
SelfHarmInstructions float32 `json:"self-harm/instructions"`
Sexual float32 `json:"sexual"`
SexualMinors float32 `json:"sexual/minors"`
Violence float32 `json:"violence"`
ViolenceGraphic float32 `json:"violence/graphic"`
}

// ModerationResponse represents a response structure for moderation API.
Expand Down
22 changes: 11 additions & 11 deletions moderation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,47 +82,47 @@ func handleModerationEndpoint(w http.ResponseWriter, r *http.Request) {
switch {
case strings.Contains(moderationReq.Input, "hate"):
resCat = openai.ResultCategories{Hate: true}
resCatScore = openai.ResultCategoryScores{Hate: true}
resCatScore = openai.ResultCategoryScores{Hate: 1}

case strings.Contains(moderationReq.Input, "hate more"):
resCat = openai.ResultCategories{HateThreatening: true}
resCatScore = openai.ResultCategoryScores{HateThreatening: true}
resCatScore = openai.ResultCategoryScores{HateThreatening: 1}

case strings.Contains(moderationReq.Input, "harass"):
resCat = openai.ResultCategories{Harassment: true}
resCatScore = openai.ResultCategoryScores{Harassment: true}
resCatScore = openai.ResultCategoryScores{Harassment: 1}

case strings.Contains(moderationReq.Input, "harass hard"):
resCat = openai.ResultCategories{Harassment: true}
resCatScore = openai.ResultCategoryScores{HarassmentThreatening: true}
resCatScore = openai.ResultCategoryScores{HarassmentThreatening: 1}

case strings.Contains(moderationReq.Input, "suicide"):
resCat = openai.ResultCategories{SelfHarm: true}
resCatScore = openai.ResultCategoryScores{SelfHarm: true}
resCatScore = openai.ResultCategoryScores{SelfHarm: 1}

case strings.Contains(moderationReq.Input, "wanna suicide"):
resCat = openai.ResultCategories{SelfHarmIntent: true}
resCatScore = openai.ResultCategoryScores{SelfHarm: true}
resCatScore = openai.ResultCategoryScores{SelfHarm: 1}

case strings.Contains(moderationReq.Input, "drink bleach"):
resCat = openai.ResultCategories{SelfHarmInstructions: true}
resCatScore = openai.ResultCategoryScores{SelfHarmInstructions: true}
resCatScore = openai.ResultCategoryScores{SelfHarmInstructions: 1}

case strings.Contains(moderationReq.Input, "porn"):
resCat = openai.ResultCategories{Sexual: true}
resCatScore = openai.ResultCategoryScores{Sexual: true}
resCatScore = openai.ResultCategoryScores{Sexual: 1}

case strings.Contains(moderationReq.Input, "child porn"):
resCat = openai.ResultCategories{SexualMinors: true}
resCatScore = openai.ResultCategoryScores{SexualMinors: true}
resCatScore = openai.ResultCategoryScores{SexualMinors: 1}

case strings.Contains(moderationReq.Input, "kill"):
resCat = openai.ResultCategories{Violence: true}
resCatScore = openai.ResultCategoryScores{Violence: true}
resCatScore = openai.ResultCategoryScores{Violence: 1}

case strings.Contains(moderationReq.Input, "corpse"):
resCat = openai.ResultCategories{ViolenceGraphic: true}
resCatScore = openai.ResultCategoryScores{ViolenceGraphic: true}
resCatScore = openai.ResultCategoryScores{ViolenceGraphic: 1}
}

result := openai.Result{Categories: resCat, CategoryScores: resCatScore, Flagged: true}
Expand Down

0 comments on commit bb6149f

Please sign in to comment.