Skip to content

Commit bb6149f

Browse files
authored
fix: repair json decoding of moderation response (#670)
1 parent 4103778 commit bb6149f

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

moderation.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,17 @@ type ResultCategories struct {
5959

6060
// ResultCategoryScores represents CategoryScores of Result.
6161
type ResultCategoryScores struct {
62-
Hate bool `json:"hate"`
63-
HateThreatening bool `json:"hate/threatening"`
64-
Harassment bool `json:"harassment"`
65-
HarassmentThreatening bool `json:"harassment/threatening"`
66-
SelfHarm bool `json:"self-harm"`
67-
SelfHarmIntent bool `json:"self-harm/intent"`
68-
SelfHarmInstructions bool `json:"self-harm/instructions"`
69-
Sexual bool `json:"sexual"`
70-
SexualMinors bool `json:"sexual/minors"`
71-
Violence bool `json:"violence"`
72-
ViolenceGraphic bool `json:"violence/graphic"`
62+
Hate float32 `json:"hate"`
63+
HateThreatening float32 `json:"hate/threatening"`
64+
Harassment float32 `json:"harassment"`
65+
HarassmentThreatening float32 `json:"harassment/threatening"`
66+
SelfHarm float32 `json:"self-harm"`
67+
SelfHarmIntent float32 `json:"self-harm/intent"`
68+
SelfHarmInstructions float32 `json:"self-harm/instructions"`
69+
Sexual float32 `json:"sexual"`
70+
SexualMinors float32 `json:"sexual/minors"`
71+
Violence float32 `json:"violence"`
72+
ViolenceGraphic float32 `json:"violence/graphic"`
7373
}
7474

7575
// ModerationResponse represents a response structure for moderation API.

moderation_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,47 +82,47 @@ func handleModerationEndpoint(w http.ResponseWriter, r *http.Request) {
8282
switch {
8383
case strings.Contains(moderationReq.Input, "hate"):
8484
resCat = openai.ResultCategories{Hate: true}
85-
resCatScore = openai.ResultCategoryScores{Hate: true}
85+
resCatScore = openai.ResultCategoryScores{Hate: 1}
8686

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

9191
case strings.Contains(moderationReq.Input, "harass"):
9292
resCat = openai.ResultCategories{Harassment: true}
93-
resCatScore = openai.ResultCategoryScores{Harassment: true}
93+
resCatScore = openai.ResultCategoryScores{Harassment: 1}
9494

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

9999
case strings.Contains(moderationReq.Input, "suicide"):
100100
resCat = openai.ResultCategories{SelfHarm: true}
101-
resCatScore = openai.ResultCategoryScores{SelfHarm: true}
101+
resCatScore = openai.ResultCategoryScores{SelfHarm: 1}
102102

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

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

111111
case strings.Contains(moderationReq.Input, "porn"):
112112
resCat = openai.ResultCategories{Sexual: true}
113-
resCatScore = openai.ResultCategoryScores{Sexual: true}
113+
resCatScore = openai.ResultCategoryScores{Sexual: 1}
114114

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

119119
case strings.Contains(moderationReq.Input, "kill"):
120120
resCat = openai.ResultCategories{Violence: true}
121-
resCatScore = openai.ResultCategoryScores{Violence: true}
121+
resCatScore = openai.ResultCategoryScores{Violence: 1}
122122

123123
case strings.Contains(moderationReq.Input, "corpse"):
124124
resCat = openai.ResultCategories{ViolenceGraphic: true}
125-
resCatScore = openai.ResultCategoryScores{ViolenceGraphic: true}
125+
resCatScore = openai.ResultCategoryScores{ViolenceGraphic: 1}
126126
}
127127

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

0 commit comments

Comments
 (0)