Jose Jimenez
Software Architect & Developer
> >

Using OpenAI's GPT Model to Filter Famous Personalities and Brands in Text Input

Published in Artificial Intelligence, Laravel, PHP, OpenAPI on Mar 30, 2023

In this article, we will be utilizing the OpenAI GPT-3 or GPT-4 model to identify famous personalities and brands from textual input. This approach can be useful for either blocking or collecting user input, based on the detected items.

Recently, I encountered a use case where in Stable Diffusion image generation project we aimed to prevent users from generating any content featuring famous personalities or brands. One of the primary benefits of using the GPT-3 model for this particular task is that it has been trained on an extensive dataset from across the globe, providing a wider range of content beyond that of the United States alone.

For this example we are Leveraging Laravel's Http:: facade.

$prompt = 'I am going to give you text, return only famous people and brands and respond only with JSON.'
    . 'Use this format{"success":true, "famous_people":[], "brands":[]}'
    . 'If no results respond only with {"success":false, "message":"No results found."}';

$user_prompt = "\nI am went out hiking with Terry Crews and Aubrey Plaza, my friend Mike also joined us."
    . 'I wore Asics shoes and they were wearing Nike shoes.';

$response = Http::withHeaders([
    'Content-Type' => 'application/json',
    'Authorization' => 'Bearer ' . 'sk-XXXXXXX',
])->post('', [
    'model' => 'gpt-3.5-turbo',
    'messages' => [
            'role' => 'user',
            'content' => $prompt . $user_prompt,

dd($response->json(), json_decode($response->json()['choices']['0']['message']['content'], true))

In this example we tell the AI to respond in JSON format, the format we want things in, and if there are no results what to respond with. AI is extremely smart and does a very good job at inserting data into the appropriate response.

In the above example for $user_prompt the system returns:

array:3 [
  "success" => true
  "famous_people" => array:2 [
    0 => "Terry Crews"
    1 => "Aubrey Plaza"
  "brands" => array:2 [
    0 => "Asics"
    1 => "Nike"

If we change the $user_prompt to:

I went running with my friend Mike

It will return no results because I did not specify who my friend Mike is.

array:2 [
  "success" => false
  "message" => "No results found."

Storing the data in an array provides the advantage of enabling you to save the results in a database, which allows your team to perform further analysis on the data using your internal system before attempting to run it against OpenAI servers. This approach helps you avoid making unnecessary requests to an external provider.

To summarize, GPT is a highly powerful tool that can be leveraged for content filtering purposes. While there are many tools available in the market like Google Cloud Natural Language API and Amazon Comprehend that can aid in this scenario, OpenAI offers a highly competitive pricing model with remarkable precision in identifying the intended content.