Skip to content

Commit acd6faf

Browse files
committed
add kotlin dsl for moderations + kotlin example
1 parent 97196f2 commit acd6faf

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

examples/src/main/java/moderations/CreateModerations.java renamed to examples/src/main/java/moderations/ModerationsExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.util.Comparator;
99
import java.util.Scanner;
1010

11-
public class CreateModerations {
11+
public class ModerationsExample {
1212

1313
// To use dotenv, you need to add the "io.github.cdimascio:dotenv-kotlin:version"
1414
// dependency. Then you can add a .env file in your project directory.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package moderations
2+
3+
import com.cjcrafter.openai.moderations.create
4+
import com.cjcrafter.openai.openAI
5+
import io.github.cdimascio.dotenv.dotenv
6+
7+
8+
fun main() {
9+
10+
// To use dotenv, you need to add the "io.github.cdimascio:dotenv-kotlin:version"
11+
// dependency. Then you can add a .env file in your project directory.
12+
val key = dotenv()["OPENAI_TOKEN"]
13+
val openai = openAI { apiKey(key) }
14+
15+
while (true) {
16+
print("Input: ")
17+
val input = readln()
18+
val moderation = openai.moderations.create {
19+
input(input)
20+
}
21+
22+
val max = moderation.results[0].categoryScores.entries.maxBy { it.value }
23+
println("Highest category: ${max.key} with a score of ${max.value}")
24+
}
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.cjcrafter.openai.moderations
2+
3+
fun createModerationRequest(block: CreateModerationRequest.Builder.() -> Unit): CreateModerationRequest {
4+
return CreateModerationRequest.builder().apply(block).build()
5+
}
6+
7+
fun ModerationHandler.create(block: CreateModerationRequest.Builder.() -> Unit): Moderation {
8+
val request = createModerationRequest(block)
9+
return create(request)
10+
}

0 commit comments

Comments
 (0)