Enhancing Content Moderation with AI: Integrating OpenAI API into Your Laravel Application
As the developer behind Yo Viajo, a web app that helps people find bus information in Costa Rica, I’ve tackled various challenges in user engagement. Initially, I required users to create an account to enable the comments feature, aiming to maintain a level of security and decorum. However, with the rise of ChatGPT, I saw an opportunity to use OpenAI’s API for comment moderation, aiming to create a safer and more user-friendly environment.
Moderation Challenges:
Manual comment moderation can be exhaustive and subjective, making consistency in enforcement a real challenge. AI integration promises scalability and uniformity, significantly easing the workload for human moderators and enabling quicker response times.
Data Protection:
For registered users, rest assured that user data is protected. The system anonymizes messages before sending them to the API, seeking determinations of appropriateness without compromising privacy.
This is how I did it and my step by step process:
- Sign up at OpenAI’s platform and obtain your API key here.
- Add a new variable
OPEN_AI_KEY
to your.env
file and insert your key. - Create a new controller or service for your OpenAI API interactions. We’ll be using Guzzle for HTTP requests, but Laravel’s native HTTP client works just as well.
use GuzzleHttp\Client;
class MyOpenAIController extends Controller
{
public function __construct()
{
$this->client = new Client([
'base_uri' => 'https://api.openai.com/v1/',
'headers' => [
'Authorization' => 'Bearer '.env('OPEN_AI_KEY'),
'Content-Type' => 'application/json',
]
]);
}
}
4. Implement a function to manage the chat completions using your chosen model:
private function communicateWithOpenAIModel($messages)
{
$response = $this->client->post('chat/completions', [
'json' => [
'model' => 'gpt-4',
'messages' => $messages,
'temperature' => 0.7
],
'timeout' => 15.0
]);
$responseData = json_decode($response->getBody(), true);
return $responseData['choices'][0]['message']['content'];
}
I chose GPT-4 for its advanced capabilities, but GPT-3.5 or others are also viable options, especially if you’re looking to manage costs initially. You can review available models here.
5. Develop a function to validate comments. It’s essential to construct a reliable prompt for the AI to understand the context of the moderation task:
public function isValidComment($commentContent) {
$messages = [
[
'role' => 'system',
'content' => 'Please, evaluate whether the following comment is appropriate: "' . $commentContent . '"'
],
[
'role' => 'user',
'content' => $commentContent
]
];
$interpretedMessage = $this->communicateWithOpenAI($messages);
$position = strpos($interpretedMessage, 'inappropriate');
if ($position !== false){
return false;
}
return true;
}
6. Here’s a practical example to illustrate how a comment might be evaluated (Note: Part of this example is presented in Spanish to reflect the app’s primary audience):

This comment would likely be flagged by the AI for containing negative sentiments and potentially inappropriate content.
This quick and straightforward method to incorporate OpenAI’s API into your Laravel project can revolutionize how you manage user interactions. My journey into AI-powered moderation has been transformative, and I’m excited for other developers to experience its benefits.
Your Input Makes a Difference:
I’d love to hear from you! If you’ve tried integrating OpenAI with Laravel (or any other framework), or have any questions or suggestions after reading this post, please drop a comment below. Let’s learn from each other and improve together.