By now, everyone knows about ChatGPT, GPT itself, and OpenAI. However, OpenAI is more than GPT: you can use their API to do image generation, speech to text, and text moderation assessments. This post is about their text moderation API, as I've recently discovered it's free.

The idea is to use the moderation API to decide whether the rest of OpenAI's models would accept the input text, but it can be used on its own as well. It might change in the future, but moderation API calls are not metered against your bill. 

Why would you want to use it? Because it's easy to use and it returns detailed information about whether text falls into any of these categories: hate, hate/threatening, self-harm, sexual, sexual/minors, violence, violence/graphic.

Example Implementation in PHP

It's also very easy to implement. Almost every language has a library for OpenAI now, and moderations are a part of most of them. Here's an example of how to start with PHP. First, install the openai-php package:

composer require openai-php/client

Then a simple script like this shows you how to use it:



$yourApiKey = 'sk-xxxxxxx';
$client = OpenAI::client($yourApiKey);

$result = $client->moderations()->create([
'model' => 'text-moderation-stable',
'input' => 'I want to kill them.',


The output $result variable is an object that tells you whether the message is flagged, including the categories that were triggered. I've truncated the output a little bit by removing some categories, but you can get a good idea of what you can do with this example:

'id' => 'modr-6zOjwQ0P3kSNuijBg2uUYJVmqMRJX',
'model' => 'text-moderation-001',
'results' =>
array (
0 =>
'categories' =>
array (
'hate/threatening' =>
'category' => \OpenAI\Enums\Moderations\Category::HateThreatening,
'violated' => true,
'score' => 0.4133393466472626,
'sexual' =>
'category' => \OpenAI\Enums\Moderations\Category::Sexual,
'violated' => false,
'score' => 0.01407555304467678,
'violence' =>
'category' => \OpenAI\Enums\Moderations\Category::Violence,
'violated' => true,
'score' => 0.922382652759552,
'flagged' => true,


Share the wealth!