Moderation
Prerequisites
-
创建 OpenAI 帐户并获取 API 密钥。您可以在 OpenAI signup page 注册并在 API Keys page 生成 API 密钥。
-
Create an OpenAI account and obtain an API key. You can sign up at the OpenAI signup page and generate an API key on the API Keys page.
-
将
spring-ai-openai
依赖项添加到项目的构建文件中。有关更多信息,请参阅 Dependency Management 部分。 -
Add the
spring-ai-openai
dependency to your project’s build file. For more information, refer to the Dependency Management section.
Auto-configuration
Spring AI 自动配置、启动器模块的工件名称发生了重大变化。请参阅 upgrade notes 以获取更多信息。 There has been a significant change in the Spring AI auto-configuration, starter modules' artifact names. Please refer to the upgrade notes for more information. |
Spring AI 为 OpenAI 审核模型提供 Spring Boot 自动配置。要启用它,请将以下依赖项添加到您项目的 Maven pom.xml
文件中:
Spring AI provides Spring Boot auto-configuration for the OpenAI Moderation Model.
To enable it add the following dependency to your project’s Maven pom.xml
file:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
或添加到您的 Gradle build.gradle
构建文件中:
or to your Gradle build.gradle
build file:
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-openai'
}
|
Refer to the Dependency Management section to add the Spring AI BOM to your build file. |
Moderation Properties
Connection Properties
前缀 spring.ai.openai 用作属性前缀,可让您连接到 OpenAI。
The prefix spring.ai.openai is used as the property prefix that lets you connect to OpenAI.
Property |
Description |
Default |
spring.ai.openai.base-url |
The URL to connect to |
[role="bare"]https://api.openai.com |
spring.ai.openai.api-key |
The API Key |
- |
spring.ai.openai.organization-id |
Optionally you can specify which organization is used for an API request. |
- |
spring.ai.openai.project-id |
Optionally, you can specify which project is used for an API request. |
- |
对于属于多个组织(或通过其旧版用户 API 密钥访问其项目)的用户,您可以选择指定用于 API 请求的组织和项目。这些 API 请求的使用将计为指定组织和项目的使用。 |
For users that belong to multiple organizations (or are accessing their projects through their legacy user API key), optionally, you can specify which organization and project is used for an API request. Usage from these API requests will count as usage for the specified organization and project. |
Configuration Properties
现在通过带有前缀 Enabling and disabling of the embedding auto-configurations are now configured via top level properties with the prefix 要启用,spring.ai.model.moderation=openai(默认启用) To enable, spring.ai.model.moderation=openai (It is enabled by default) 要禁用,spring.ai.model.moderation=none(或任何与 openai 不匹配的值) To disable, spring.ai.model.moderation=none (or any value which doesn’t match openai) 此更改旨在允许配置多个模型。 This change is done to allow configuration of multiple models. |
前缀 spring.ai.openai.moderation 用作配置 OpenAI 审核模型的属性前缀。
The prefix spring.ai.openai.moderation is used as the property prefix for configuring the OpenAI moderation model.
Property |
Description |
Default |
spring.ai.model.moderation |
Enable Moderation model |
openai |
spring.ai.openai.moderation.base-url |
The URL to connect to |
[role="bare"]https://api.openai.com |
spring.ai.openai.moderation.api-key |
The API Key |
- |
spring.ai.openai.moderation.organization-id |
Optionally you can specify which organization is used for an API request. |
- |
spring.ai.openai.moderation.project-id |
Optionally, you can specify which project is used for an API request. |
- |
spring.ai.openai.moderation.options.model |
ID of the model to use for moderation. |
text-moderation-latest |
您可以覆盖公共 |
You can override the common |
所有带前缀 |
All properties prefixed with |
Runtime Options
OpenAiModerationOptions 类提供了在发出审核请求时使用的选项。在启动时,使用 spring.ai.openai.moderation 指定的选项,但您可以在运行时覆盖这些选项。
The OpenAiModerationOptions class provides the options to use when making a moderation request. On start-up, the options specified by spring.ai.openai.moderation are used, but you can override these at runtime.
例如:
For example:
OpenAiModerationOptions moderationOptions = OpenAiModerationOptions.builder()
.model("text-moderation-latest")
.build();
ModerationPrompt moderationPrompt = new ModerationPrompt("Text to be moderated", this.moderationOptions);
ModerationResponse response = openAiModerationModel.call(this.moderationPrompt);
// Access the moderation results
Moderation moderation = moderationResponse.getResult().getOutput();
// Print general information
System.out.println("Moderation ID: " + moderation.getId());
System.out.println("Model used: " + moderation.getModel());
// Access the moderation results (there's usually only one, but it's a list)
for (ModerationResult result : moderation.getResults()) {
System.out.println("\nModeration Result:");
System.out.println("Flagged: " + result.isFlagged());
// Access categories
Categories categories = this.result.getCategories();
System.out.println("\nCategories:");
System.out.println("Sexual: " + categories.isSexual());
System.out.println("Hate: " + categories.isHate());
System.out.println("Harassment: " + categories.isHarassment());
System.out.println("Self-Harm: " + categories.isSelfHarm());
System.out.println("Sexual/Minors: " + categories.isSexualMinors());
System.out.println("Hate/Threatening: " + categories.isHateThreatening());
System.out.println("Violence/Graphic: " + categories.isViolenceGraphic());
System.out.println("Self-Harm/Intent: " + categories.isSelfHarmIntent());
System.out.println("Self-Harm/Instructions: " + categories.isSelfHarmInstructions());
System.out.println("Harassment/Threatening: " + categories.isHarassmentThreatening());
System.out.println("Violence: " + categories.isViolence());
// Access category scores
CategoryScores scores = this.result.getCategoryScores();
System.out.println("\nCategory Scores:");
System.out.println("Sexual: " + scores.getSexual());
System.out.println("Hate: " + scores.getHate());
System.out.println("Harassment: " + scores.getHarassment());
System.out.println("Self-Harm: " + scores.getSelfHarm());
System.out.println("Sexual/Minors: " + scores.getSexualMinors());
System.out.println("Hate/Threatening: " + scores.getHateThreatening());
System.out.println("Violence/Graphic: " + scores.getViolenceGraphic());
System.out.println("Self-Harm/Intent: " + scores.getSelfHarmIntent());
System.out.println("Self-Harm/Instructions: " + scores.getSelfHarmInstructions());
System.out.println("Harassment/Threatening: " + scores.getHarassmentThreatening());
System.out.println("Violence: " + scores.getViolence());
}
Manual Configuration
添加 spring-ai-openai
依赖到你的项目的 Maven pom.xml
文件中:
Add the spring-ai-openai
dependency to your project’s Maven pom.xml
file:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai</artifactId>
</dependency>
或添加到您的 Gradle build.gradle
构建文件中:
or to your Gradle build.gradle
build file:
dependencies {
implementation 'org.springframework.ai:spring-ai-openai'
}
|
Refer to the Dependency Management section to add the Spring AI BOM to your build file. |
接下来,创建一个 OpenAiModerationModel:
Next, create an OpenAiModerationModel:
OpenAiModerationApi openAiModerationApi = new OpenAiModerationApi(System.getenv("OPENAI_API_KEY"));
OpenAiModerationModel openAiModerationModel = new OpenAiModerationModel(this.openAiModerationApi);
OpenAiModerationOptions moderationOptions = OpenAiModerationOptions.builder()
.model("text-moderation-latest")
.build();
ModerationPrompt moderationPrompt = new ModerationPrompt("Text to be moderated", this.moderationOptions);
ModerationResponse response = this.openAiModerationModel.call(this.moderationPrompt);