Module moderations

Source
Expand description

This module provides functionality for classifying text against OpenAI’s content moderation policies using the OpenAI Moderations API.

The Moderations API takes text (or multiple pieces of text) and returns a set of boolean flags indicating whether the content violates certain categories (e.g., hate, self-harm, sexual), along with confidence scores for each category.

§Overview

You can call create_moderation with a CreateModerationRequest, specifying your input text(s) (and optionally, a specific model). The response (CreateModerationResponse) includes a list of ModerationResult objects—one per input. Each result contains a set of ModerationCategories, matching confidence scores (ModerationCategoryScores), and a flagged boolean indicating if the text violates policy overall.

§Example

use chat_gpt_lib_rs::api_resources::moderations::{create_moderation, CreateModerationRequest, ModerationsInput};
use chat_gpt_lib_rs::error::OpenAIError;
use chat_gpt_lib_rs::OpenAIClient;

#[tokio::main]
async fn main() -> Result<(), OpenAIError> {
    // load environment variables from a .env file, if present (optional).
    dotenvy::dotenv().ok();

    let client = OpenAIClient::new(None)?;
    let request = CreateModerationRequest {
        input: ModerationsInput::String("I hate you and want to harm you.".to_string()),
        model: None, // or Some("text-moderation-latest".into())
    };

    let response = create_moderation(&client, &request).await?;
    for (i, result) in response.results.iter().enumerate() {
        println!("== Result {} ==", i);
        println!("Flagged: {}", result.flagged);
        println!("Hate category: {}", result.categories.hate);
        println!("Hate score: {}", result.category_scores.hate);
        // ...and so on for other categories
    }

    Ok(())
}

Structs§

CreateModerationRequest
A request struct for creating a moderation check using the OpenAI Moderations API.
CreateModerationResponse
The response returned by the OpenAI Moderations API.
ModerationCategories
A breakdown of the moderation categories.
ModerationCategoryScores
Floating-point confidence scores for each moderated category.
ModerationResult
A single moderation result, indicating how the input text matches various policy categories.

Enums§

ModerationsInput
Represents the multiple ways the input can be supplied for moderations:

Functions§

create_moderation
Creates a moderation request using the OpenAI Moderations API.