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:
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.
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.
Key Points:
To obtain an OpenAI API key for accessing the Moderation API, follow these concise steps:
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.
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:
- 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.
- Access API Keys:
- After logging in, click on your profile icon in the top-right corner.
- Select "View API keys" from the dropdown menu.
- 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.
- Set Up Billing Information:
- Navigate to the "Billing" section in the left-hand menu.
- Add a payment method to activate your API key.
- 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.
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: