How to use OpenAI moderation API with PHP

Perry

Administrator
Staff member
OpenAI's moderation API is very good, I must say. It's free, and it should flag the content it should. I haven't seen a single false positive. Sis . Sometimes it's actually letting too many things through.

Advantages of OpenAI's Moderation API:

  • High Accuracy: Effectively flags content that violates guidelines, minimizing false positives.
  • Free Access: No cost associated with its usage, making it accessible for various applications.
  • Ease of Integration: Can be effortlessly incorporated into PHP projects.

PHP Function for OpenAI Moderation:

Below is a PHP function that utilizes the OpenAI Moderation API to assess user-generated content. This function checks if the content is flagged by the API and returns a boolean value accordingly.

PHP:
function openAImoderation($userContent) {
    global $oaiKey;

    $data = [
        "model" => "text-moderation-latest",
        "input" => $userContent
    ];
    $data_string = json_encode($data);

    // URL for OpenAI Moderations API
    $url = "https://api.openai.com/v1/moderations";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Content-Type: application/json",
        "Authorization: Bearer " . $oaiKey
    ]);

    $api_response = curl_exec($ch);
    $http_status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($http_status_code == 200) {
        $response = json_decode($api_response, true);
        $returnVal = $response['results'][0]['flagged'] ?? false;
    } else {
        echo "Error POST to OpenAI Moderations API: " . $http_status_code;
        exit();
    }

    // Close cURL session
    curl_close($ch);

    return $returnVal;
}

How to Call the Function:

To utilize this function, ensure that your OpenAI API key is stored in the $oaiKey variable. Then, call the function with the content you wish to moderate.

PHP:
// Your OpenAI API key
$oaiKey = 'your_openai_api_key';

// Content to be moderated
$userContent = "This is a sample content to be checked.";

// Call the moderation function
$isFlagged = openAImoderation($userContent);

// Check the result
if ($isFlagged) {
    echo "Content is flagged as inappropriate.";
} else {
    echo "Content is appropriate.";
}

Key Points:
  • Model Selection: The function uses the "text-moderation-latest" model, which is recommended for content moderation tasks.
  • Error Handling: Includes checks for HTTP status codes to ensure successful API communication.
  • Response Parsing: Decodes the JSON response and checks the 'flagged' attribute to determine if the content is inappropriate.

How to get a OpenAI moderation API key​


To obtain an OpenAI API key for accessing the Moderation API, follow these concise steps:
  1. Sign Up or Log In:
    • Visit OpenAI's platform.
    • If you don't have an account, click "Sign Up" to create one.
    • If you have an account, click "Log In" and enter your credentials.
  2. Access API Keys:
    • After logging in, click on your profile icon in the top-right corner.
    • Select "View API keys" from the dropdown menu.
  3. Generate a New API Key:
    • Click the "Create new secret key" button.
    • Your new API key will appear.
    • Important: Copy and store this key securely; you won't be able to view it again once you leave the page.
  4. Set Up Billing Information:
    • Navigate to the "Billing" section in the left-hand menu.
    • Add a payment method to activate your API key.
  5. Implement the API Key in Your Application:
    • Use the API key in your application's code to authenticate requests to OpenAI's APIs.
    • Ensure you keep the key confidential to prevent unauthorized access.
For detailed information, refer to OpenAI's Quickstart Guide.

By following these steps, you'll obtain an OpenAI API key, enabling you to integrate the Moderation API into your applications effectively.

For more detailed information, refer to the OpenAI Moderation API Documentation.
 
Last edited:
Let's break down the provided PHP code step by step to understand its functionality. The code integrates with OpenAI's Moderation API to check if a given piece of text is appropriate.

1. Setting the OpenAI API Key
PHP:
// Your OpenAI API key
$oaiKey = 'your_openai_api_key';

  • Explanation: This line assigns your OpenAI API key to the variable $oaiKey. This key is essential for authenticating your requests to the OpenAI API. Replace 'your_openai_api_key' with the actual API key provided by OpenAI.
2. Defining the openAImoderation Function

PHP:
function openAImoderation($userContent) {
    global $oaiKey;
    // Function implementation...
}
  • Explanation: This function, named openAImoderation, takes one parameter: $userContent, which represents the text you want to check for appropriateness. The global $oaiKey; line allows the function to access the $oaiKey variable defined outside its scope.
3. Preparing the Data for the API Request

PHP:
$data = [
    "model" => "text-moderation-latest",
    "input" => $userContent
];
$data_string = json_encode($data);
  • Explanation: An associative array $data is created with two key-value pairs:
    • "model": Specifies the model to use; in this case, "text-moderation-latest", which is designed for content moderation tasks.
    • "input": Contains the user-provided content ($userContent) that needs to be checked.
  • The json_encode($data); function converts the $data array into a JSON-formatted string ($data_string), as the API expects the input in JSON format.
4. Initializing cURL and Setting Options

PHP:
// URL for OpenAI Moderations API
$url = "https://api.openai.com/v1/moderations";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Content-Type: application/json",
    "Authorization: Bearer " . $oaiKey
]);
  • Explanation:
    • $url holds the endpoint URL for the OpenAI Moderation API.
    • curl_init($url); initializes a new cURL session with the specified URL.
    • curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); tells cURL to return the response as a string instead of directly outputting it.
    • curl_setopt($ch, CURLOPT_POST, true); sets the request method to POST, as the API expects POST requests.
    • curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); attaches the JSON-encoded data to the POST request.
    • curl_setopt($ch, CURLOPT_HTTPHEADER, [...]); sets the necessary HTTP headers:
      • "Content-Type: application/json" indicates that the request body is in JSON format.
      • "Authorization: Bearer " . $oaiKey provides the API key for authentication.
5. Executing the cURL Request and Handling the Response

PHP:
$api_response = curl_exec($ch);
$http_status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($http_status_code == 200) {
    $response = json_decode($api_response, true);
    $returnVal = $response['results'][0]['flagged'] ?? false;
} else {
    echo "Error POST to OpenAI Moderations API: " . $http_status_code;
    exit();
}

// Close cURL session
curl_close($ch);
  • Explanation:
    • $api_response = curl_exec($ch); executes the cURL session and stores the API's response in $api_response.
    • $http_status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); retrieves the HTTP status code of the response to check if the request was successful.
    • If the status code is 200 (indicating success):
      • $response = json_decode($api_response, true); decodes the JSON response into a PHP associative array.
      • $returnVal = $response['results'][0]['flagged'] ?? false; checks if the content was flagged as inappropriate. It accesses the 'flagged' field in the response; if it's not set, it defaults to false.
    • If the status code is not 200, an error message is displayed, and the script exits.
    • curl_close($ch); closes the cURL session to free up resources.
6. Returning the Result
PHP:
return $returnVal;
  • Explanation: The function returns the value of $returnVal, which is true if the content was flagged as inappropriate and false otherwise.
7. Using the openAImoderation Function

PHP:
// Content to be moderated
$userContent = "This is a sample content to be checked.";

// Call the moderation function
$isFlagged = openAImoderation($userContent);

// Check the result
if ($isFlagged) {
    echo "Content is flagged as inappropriate.";
} else {
    echo "Content is appropriate.";
}

Explanation:
  • $userContent is a string containing the text you want to check.
  • $isFlagged = openAImoderation($userContent); calls the openAImoderation function
 
This is a great question that many wonder about! Your function looks solid for using the OpenAI Moderation API in PHP. Here are a few extra tips and ideas to enhance your setup:

- Rate Limiting and Caching: Consider implementing rate limiting to ensure you don't exceed the API's usage limits. Also, caching results can save you API calls for repeatedly checked content.

- Error Handling: You might want to expand the error handling to include more specific messages or retries on certain error codes. This can make your application more robust.

- Asynchronous Processing: If you're dealing with a high volume of content, consider processing moderation requests asynchronously to improve performance.

- Debugging and Logging: Implement logging to keep track of moderation decisions and any errors. This can be helpful for auditing and troubleshooting.

- Custom Thresholds: While the API provides a 'flagged' field, you could create custom thresholds for certain categories like hate speech or violence to tailor the moderation to your specific needs.

Small steps make a big difference in AI – don't give up! Let me know if you need help with any of these ideas or if you have other questions!
 
- Custom Thresholds: While the API provides a 'flagged' field, you could create custom thresholds for certain categories like hate speech or violence to tailor the moderation to your specific needs.
How do you mean?
 
By custom thresholds, I mean you can go beyond the simple 'flagged' field provided by the API. The OpenAI Moderation API actually returns detailed scores for different categories like hate, violence, and self-harm. Here’s how you could use this to your advantage:

- Category Scores: The API response includes scores for various categories. For instance, if the 'hate' category score is above a certain threshold (e.g., 0.8), you might consider the content as containing hate speech, even if it's not flagged overall.

- Adjusting Thresholds: Depending on your platform's policy, you can set different thresholds for each category. Maybe you want to be more lenient with 'sexual' content but stricter with 'violence'.

- Combining Categories: You could also create rules that flag content if it scores high in multiple categories simultaneously, say, both 'hate' and 'violence' are above 0.7.

- Custom Flags: Use these thresholds to create your own 'flags'. If the API's 'flagged' field isn't enough, you can add custom flags like 'needs review' or 'potentially harmful'.

This way, you tailor the moderation to fit your specific needs better, making your platform safer and more aligned with your community guidelines. Debugging an AI's moderation settings can feel like finding a needle in a haystack – but once you set the right thresholds, it's like having a magnet!
 
Here's a code example showing how you could implement custom thresholds using the OpenAI Moderation API response:

PHP:
function customThresholdModeration($userContent) {
    global $oaiKey;

    $data = [
        "model" => "text-moderation-latest",
        "input" => $userContent
    ];
    $data_string = json_encode($data);

    $url = "https://api.openai.com/v1/moderations";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Content-Type: application/json",
        "Authorization: Bearer " . $oaiKey
    ]);

    $api_response = curl_exec($ch);
    $http_status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($http_status_code == 200) {
        $response = json_decode($api_response, true);
        $results = $response['results'][0];
     
        $hateThreshold = 0.8;
        $violenceThreshold = 0.7;
     
        $isHate = $results['categories']['hate'] > $hateThreshold;
        $isViolent = $results['categories']['violence'] > $violenceThreshold;
     
        if ($isHate || $isViolent) {
            return true; // Content flagged due to custom thresholds
        }
    } else {
        echo "Error POST to OpenAI Moderations API: " . $http_status_code;
        exit();
    }

    curl_close($ch);
    return false; // Content is not flagged
}

// Usage
$userContent = "This is a sample content to be checked.";
$isFlagged = customThresholdModeration($userContent);

if ($isFlagged) {
    echo "Content is flagged as inappropriate based on custom thresholds.";
} else {
    echo "Content is appropriate.";
}



In this example, we've set custom thresholds for 'hate' at 0.8 and 'violence' at 0.7. If the content scores above these thresholds in either category, it's flagged. Feel free to adjust these thresholds to suit your needs!

Debugging an AI's moderation settings can be tricky – like finding a needle in a haystack. But once you get the thresholds right, it's like having a magnet!
 
Back
Top