DEV Community

Cover image for Automating comment validation on your website with AI: Easy and (almost) free!
Darko Todorić
Darko Todorić

Posted on

Automating comment validation on your website with AI: Easy and (almost) free!

Unlocking the potential of AI for web development has become both accessible and budget-friendly. In this article, we'll delve into the simple process of automating comment validation on your website using the ChatGPT API.


It's free, but...

The ChatGPT Moderation API is a powerful OpenAI model designed to help maintain a safe and respectful online environment. This API is currently offered for free though it's important to note that this pricing may change in the future.

It excels in handling content primarily in English, being highly optimized for this language. While it can work with other languages, its performance may not be as accurate or reliable as it is with English text.

The primary purpose of this model is to flag comments or content that may be inappropriate or harmful. It can categorize comments into various categories such as "hate," "harassment", "self-harm," "sexual," and so on.

To acquire a classification for a given comment, initiate a request to the moderation endpoint as exemplified in the following code samples:

curl https://api.openai.com/v1/moderations \
  -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{"input": "Comment goes here"}'
Enter fullscreen mode Exit fullscreen mode

So a simple example of how it will look in PHP code:

if($this->openAi->isCommentFlagged($comment)){
  // code here
}
Enter fullscreen mode Exit fullscreen mode

With just a few lines of code, you can automate the majority of your tasks.


What about non-English comments?

Image description

To complete this task, we need to utilize the ChatGPT 3.5 API, which is a paid service, but it performs excellently in all languages supported by ChatGPT.

To acquire a classification for a given comment in any language, initiate a request to the ChatGPT API:

curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
  "model": "gpt-3.5-turbo",
  "messages": [
    {
      "role": "system",
      "content": "Check the comment that I will forward to you and see if it falls into one of these categories: '\''hate, harassment, self-harm, sexual, violence'\''. Return me a JSON response in the following format: {'\''flagged'\'': true} or {'\''flagged'\'': false}."
    },
    {
      "role": "user",
      "content": "Comment goes here"
    }
  ],
  "temperature": 1,
  "max_tokens": 256,
  "top_p": 1,
  "frequency_penalty": 0,
  "presence_penalty": 0
}'
Enter fullscreen mode Exit fullscreen mode

Also, for this code, the PHP part will look exactly the same:

if($this->openAi->isCommentFlagged($comment)){
  // code here
}
Enter fullscreen mode Exit fullscreen mode

What about the price?

Well, if we say that the average comment on websites contains about 250 characters (which is quite a long comment), the current price using the "gpt-3.5-turbo-1106" model would be about 0.0001$ per comment, which leads us to the fact that checking 10,000 comments will cost us 1$.


Conclusion

In conclusion, harnessing the power of AI for comment validation can be a game-changer for your business. By automating this process, you not only save valuable time but also enhance the security and user experience, ultimately saving your business money.

Top comments (1)

Collapse
 
sagsman profile image
Sagiru Garba

Really mm