-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from CJCrafter/moderations
Moderations
- Loading branch information
Showing
11 changed files
with
237 additions
and
21 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
examples/src/main/java/moderations/ModerationsExample.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,40 @@ | ||
package moderations; | ||
|
||
import com.cjcrafter.openai.OpenAI; | ||
import com.cjcrafter.openai.moderations.CreateModerationRequest; | ||
import com.cjcrafter.openai.moderations.Moderation; | ||
import io.github.cdimascio.dotenv.Dotenv; | ||
|
||
import java.util.Comparator; | ||
import java.util.Scanner; | ||
|
||
public class ModerationsExample { | ||
|
||
// To use dotenv, you need to add the "io.github.cdimascio:dotenv-kotlin:version" | ||
// dependency. Then you can add a .env file in your project directory. | ||
public static final OpenAI openai = OpenAI.builder() | ||
.apiKey(Dotenv.load().get("OPENAI_TOKEN")) | ||
.build(); | ||
|
||
public static final Scanner scan = new Scanner(System.in); | ||
|
||
public static void main(String[] args) { | ||
while (true) { | ||
System.out.print("Input: "); | ||
String input = scan.nextLine(); | ||
CreateModerationRequest request = CreateModerationRequest.builder() | ||
.input(input) | ||
.build(); | ||
|
||
Moderation moderation = openai.moderations().create(request); | ||
Moderation.Result result = moderation.getResults().get(0); | ||
|
||
// Finds the category with the highest score | ||
String highest = result.getCategoryScores().keySet().stream() | ||
.max(Comparator.comparing(a -> result.getCategoryScores().get(a))) | ||
.orElseThrow(() -> new RuntimeException("No categories found!")); | ||
|
||
System.out.println("Highest category: " + highest + ", with a score of " + result.getCategoryScores().get(highest)); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
examples/src/main/kotlin/moderations/ModerationsExample.kt
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,25 @@ | ||
package moderations | ||
|
||
import com.cjcrafter.openai.moderations.create | ||
import com.cjcrafter.openai.openAI | ||
import io.github.cdimascio.dotenv.dotenv | ||
|
||
|
||
fun main() { | ||
|
||
// To use dotenv, you need to add the "io.github.cdimascio:dotenv-kotlin:version" | ||
// dependency. Then you can add a .env file in your project directory. | ||
val key = dotenv()["OPENAI_TOKEN"] | ||
val openai = openAI { apiKey(key) } | ||
|
||
while (true) { | ||
print("Input: ") | ||
val input = readln() | ||
val moderation = openai.moderations.create { | ||
input(input) | ||
} | ||
|
||
val max = moderation.results[0].categoryScores.entries.maxBy { it.value } | ||
println("Highest category: ${max.key} with a score of ${max.value}") | ||
} | ||
} |
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
60 changes: 60 additions & 0 deletions
60
src/main/kotlin/com/cjcrafter/openai/moderations/CreateModerationRequest.kt
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,60 @@ | ||
package com.cjcrafter.openai.moderations | ||
|
||
import com.cjcrafter.openai.util.OpenAIDslMarker | ||
|
||
/** | ||
* Represents a request to create a new moderation request. | ||
* | ||
* @property input The input to moderate | ||
* @property model The model to use for moderation | ||
*/ | ||
data class CreateModerationRequest internal constructor( | ||
var input: Any, | ||
var model: String? = null | ||
) { | ||
|
||
@OpenAIDslMarker | ||
class Builder internal constructor() { | ||
private var input: Any? = null | ||
private var model: String? = null | ||
|
||
/** | ||
* Sets the input to moderate. | ||
* | ||
* @param input The input to moderate | ||
*/ | ||
fun input(input: String) = apply { this.input = input } | ||
|
||
/** | ||
* Sets the input to moderate. | ||
* | ||
* @param input The input to moderate | ||
*/ | ||
fun input(input: List<String>) = apply { this.input = input } | ||
|
||
/** | ||
* Sets the model to use for moderation. | ||
* | ||
* @param model The model to use for moderation | ||
*/ | ||
fun model(model: String) = apply { this.model = model } | ||
|
||
/** | ||
* Builds the [CreateModerationRequest] instance. | ||
*/ | ||
fun build(): CreateModerationRequest { | ||
return CreateModerationRequest( | ||
input = input ?: throw IllegalStateException("input must be defined to use CreateModerationRequest"), | ||
model = model | ||
) | ||
} | ||
} | ||
|
||
companion object { | ||
/** | ||
* Returns a builder to construct a [CreateModerationRequest] instance. | ||
*/ | ||
@JvmStatic | ||
fun builder() = Builder() | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/kotlin/com/cjcrafter/openai/moderations/Moderation.kt
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,29 @@ | ||
package com.cjcrafter.openai.moderations | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
|
||
/** | ||
* A moderation object returned by the moderations api. | ||
* | ||
* @property id The id of the moderation request. Always starts with "modr-". | ||
* @property model The model which was used to moderate the content. | ||
* @property results The results of the moderation request. | ||
*/ | ||
data class Moderation( | ||
@JsonProperty(required = true) val id: String, | ||
@JsonProperty(required = true) val model: String, | ||
@JsonProperty(required = true) val results: List<Result>, | ||
) { | ||
/** | ||
* The results of the moderation request. | ||
* | ||
* @property flagged If any categories were flagged. | ||
* @property categories The categories that were flagged. | ||
* @property categoryScores The scores of each category. | ||
*/ | ||
data class Result( | ||
@JsonProperty(required = true) val flagged: Boolean, | ||
@JsonProperty(required = true) val categories: Map<String, Boolean>, | ||
@JsonProperty("category_scores", required = true) val categoryScores: Map<String, Double>, | ||
) | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/com/cjcrafter/openai/moderations/ModerationDsl.kt
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,10 @@ | ||
package com.cjcrafter.openai.moderations | ||
|
||
fun createModerationRequest(block: CreateModerationRequest.Builder.() -> Unit): CreateModerationRequest { | ||
return CreateModerationRequest.builder().apply(block).build() | ||
} | ||
|
||
fun ModerationHandler.create(block: CreateModerationRequest.Builder.() -> Unit): Moderation { | ||
val request = createModerationRequest(block) | ||
return create(request) | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/com/cjcrafter/openai/moderations/ModerationHandler.kt
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,15 @@ | ||
package com.cjcrafter.openai.moderations | ||
|
||
/** | ||
* Handler used to interact with [Moderation] objects. | ||
*/ | ||
interface ModerationHandler { | ||
|
||
/** | ||
* Creates a new moderation request with the given options. | ||
* | ||
* @param request The values of the moderation to create | ||
* @return The created moderation | ||
*/ | ||
fun create(request: CreateModerationRequest): Moderation | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/com/cjcrafter/openai/moderations/ModerationHandlerImpl.kt
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,13 @@ | ||
package com.cjcrafter.openai.moderations | ||
|
||
import com.cjcrafter.openai.RequestHelper | ||
|
||
class ModerationHandlerImpl( | ||
private val requestHelper: RequestHelper, | ||
private val endpoint: String, | ||
): ModerationHandler { | ||
override fun create(request: CreateModerationRequest): Moderation { | ||
val httpRequest = requestHelper.buildRequest(request, endpoint).build() | ||
return requestHelper.executeRequest(httpRequest, Moderation::class.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
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