forked from TheoKanning/openai-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add moderation support (TheoKanning#24)
Adding support for the new Moderations api https://beta.openai.com/docs/guides/moderation Fixes TheoKanning#20
1 parent
77219d4
commit d1f2748
Showing
10 changed files
with
195 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
api/src/main/java/com/theokanning/openai/moderation/Moderation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.theokanning.openai.moderation; | ||
|
||
import lombok.Data; | ||
|
||
/** | ||
* An object containing the moderation data for a single input string | ||
* | ||
* https://beta.openai.com/docs/api-reference/moderations/create | ||
*/ | ||
@Data | ||
public class Moderation { | ||
/** | ||
* Set to true if the model classifies the content as violating OpenAI's content policy, false otherwise | ||
*/ | ||
boolean flagged; | ||
|
||
/** | ||
* Object containing per-category binary content policy violation flags. | ||
* For each category, the value is true if the model flags the corresponding category as violated, false otherwise. | ||
*/ | ||
ModerationCategories categories; | ||
|
||
/** | ||
* Object containing per-category raw scores output by the model, denoting the model's confidence that the | ||
* input violates the OpenAI's policy for the category. | ||
* The value is between 0 and 1, where higher values denote higher confidence. | ||
* The scores should not be interpreted as probabilities. | ||
*/ | ||
ModerationCategoryScores categoryScores; | ||
} |
34 changes: 34 additions & 0 deletions
34
api/src/main/java/com/theokanning/openai/moderation/ModerationCategories.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.theokanning.openai.moderation; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.theokanning.openai.completion.CompletionChoice; | ||
import lombok.Data; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* An object containing the flags for each moderation category | ||
* | ||
* https://beta.openai.com/docs/api-reference/moderations/create | ||
*/ | ||
@Data | ||
public class ModerationCategories { | ||
|
||
boolean hate; | ||
|
||
@JsonProperty("hate/threatening") | ||
boolean hateThreatening; | ||
|
||
@JsonProperty("self-harm") | ||
boolean selfHarm; | ||
|
||
boolean sexual; | ||
|
||
@JsonProperty("sexual/minors") | ||
boolean sexualMinors; | ||
|
||
boolean violence; | ||
|
||
@JsonProperty("violence/graphic") | ||
boolean violenceGraphic; | ||
} |
31 changes: 31 additions & 0 deletions
31
api/src/main/java/com/theokanning/openai/moderation/ModerationCategoryScores.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.theokanning.openai.moderation; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Data; | ||
|
||
/** | ||
* An object containing the scores for each moderation category | ||
* | ||
* https://beta.openai.com/docs/api-reference/moderations/create | ||
*/ | ||
@Data | ||
public class ModerationCategoryScores { | ||
|
||
double hate; | ||
|
||
@JsonProperty("hate/threatening") | ||
double hateThreatening; | ||
|
||
@JsonProperty("self-harm") | ||
double selfHarm; | ||
|
||
double sexual; | ||
|
||
@JsonProperty("sexual/minors") | ||
double sexualMinors; | ||
|
||
double violence; | ||
|
||
@JsonProperty("violence/graphic") | ||
double violenceGraphic; | ||
} |
28 changes: 28 additions & 0 deletions
28
api/src/main/java/com/theokanning/openai/moderation/ModerationRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.theokanning.openai.moderation; | ||
|
||
import lombok.*; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* A request for OpenAi to detect if text violates OpenAi's content policy. | ||
* | ||
* https://beta.openai.com/docs/api-reference/moderations/create | ||
*/ | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Data | ||
public class ModerationRequest { | ||
|
||
/** | ||
* The input text to classify. | ||
*/ | ||
@NonNull | ||
String input; | ||
|
||
/** | ||
* The name of the model to use, defaults to text-moderation-stable. | ||
*/ | ||
String model; | ||
} |
28 changes: 28 additions & 0 deletions
28
api/src/main/java/com/theokanning/openai/moderation/ModerationResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.theokanning.openai.moderation; | ||
|
||
import lombok.Data; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* An object containing a response from the moderation api | ||
* | ||
* https://beta.openai.com/docs/api-reference/moderations/create | ||
*/ | ||
@Data | ||
public class ModerationResult { | ||
/** | ||
* A unique id assigned to this moderation. | ||
*/ | ||
String id; | ||
|
||
/** | ||
* The GPT-3 model used. | ||
*/ | ||
String model; | ||
|
||
/** | ||
* A list of moderation scores. | ||
*/ | ||
List<Moderation> results; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
client/src/test/java/com/theokanning/openai/ModerationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.theokanning.openai; | ||
|
||
import com.theokanning.openai.moderation.ModerationRequest; | ||
import com.theokanning.openai.moderation.Moderation; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
|
||
public class ModerationTest { | ||
|
||
String token = System.getenv("OPENAI_TOKEN"); | ||
OpenAiService service = new OpenAiService(token); | ||
|
||
@Test | ||
void createModeration() { | ||
ModerationRequest moderationRequest = ModerationRequest.builder() | ||
.input("I want to kill them") | ||
.model("text-moderation-latest") | ||
.build(); | ||
|
||
Moderation moderationScore = service.createModeration(moderationRequest).getResults().get(0); | ||
|
||
assertTrue(moderationScore.isFlagged()); | ||
} | ||
} |