Skip to content

Commit 97196f2

Browse files
committed
create java moderations example
1 parent c41eeeb commit 97196f2

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package moderations;
2+
3+
import com.cjcrafter.openai.OpenAI;
4+
import com.cjcrafter.openai.moderations.CreateModerationRequest;
5+
import com.cjcrafter.openai.moderations.Moderation;
6+
import io.github.cdimascio.dotenv.Dotenv;
7+
8+
import java.util.Comparator;
9+
import java.util.Scanner;
10+
11+
public class CreateModerations {
12+
13+
// To use dotenv, you need to add the "io.github.cdimascio:dotenv-kotlin:version"
14+
// dependency. Then you can add a .env file in your project directory.
15+
public static final OpenAI openai = OpenAI.builder()
16+
.apiKey(Dotenv.load().get("OPENAI_TOKEN"))
17+
.build();
18+
19+
public static final Scanner scan = new Scanner(System.in);
20+
21+
public static void main(String[] args) {
22+
while (true) {
23+
System.out.print("Input: ");
24+
String input = scan.nextLine();
25+
CreateModerationRequest request = CreateModerationRequest.builder()
26+
.input(input)
27+
.build();
28+
29+
Moderation moderation = openai.moderations().create(request);
30+
Moderation.Result result = moderation.getResults().get(0);
31+
32+
// Finds the category with the highest score
33+
String highest = result.getCategoryScores().keySet().stream()
34+
.max(Comparator.comparing(a -> result.getCategoryScores().get(a)))
35+
.orElseThrow(() -> new RuntimeException("No categories found!"));
36+
37+
System.out.println("Highest category: " + highest + ", with a score of " + result.getCategoryScores().get(highest));
38+
}
39+
}
40+
}

src/main/kotlin/com/cjcrafter/openai/moderations/Moderation.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,20 @@ import com.fasterxml.jackson.annotation.JsonProperty
88
* @property id The id of the moderation request. Always starts with "modr-".
99
* @property model The model which was used to moderate the content.
1010
* @property results The results of the moderation request.
11-
* @constructor Create empty Moderation
1211
*/
1312
data class Moderation(
1413
@JsonProperty(required = true) val id: String,
1514
@JsonProperty(required = true) val model: String,
16-
@JsonProperty(required = true) val results: Results,
15+
@JsonProperty(required = true) val results: List<Result>,
1716
) {
1817
/**
1918
* The results of the moderation request.
2019
*
2120
* @property flagged If any categories were flagged.
2221
* @property categories The categories that were flagged.
2322
* @property categoryScores The scores of each category.
24-
* @constructor Create empty Results
2523
*/
26-
data class Results(
24+
data class Result(
2725
@JsonProperty(required = true) val flagged: Boolean,
2826
@JsonProperty(required = true) val categories: Map<String, Boolean>,
2927
@JsonProperty("category_scores", required = true) val categoryScores: Map<String, Double>,

0 commit comments

Comments
 (0)