Jose Jimenez
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.

1$prompt = 'I am going to give you text, return only famous people and brands and respond only with JSON.'
2 . 'Use this format{"success":true, "famous_people":[], "brands":[]}'
3 . 'If no results respond only with {"success":false, "message":"No results found."}';
 4 
5$user_prompt = "\nI am went out hiking with Terry Crews and Aubrey Plaza, my friend Mike also joined us."
6 . 'I wore Asics shoes and they were wearing Nike shoes.';
 7 
8$response = Http::withHeaders([
9 'Content-Type' => 'application/json',
10 'Authorization' => 'Bearer ' . 'sk-XXXXXXX',
11])->post('https://api.openai.com/v1/chat/completions', [
12 'model' => 'gpt-3.5-turbo',
13 'messages' => [
14 [
15 'role' => 'user',
16 'content' => $prompt . $user_prompt,
17 ],
18 ],
19]);
20 
21dd($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:

1array:3 [
2 "success" => true
3 "famous_people" => array:2 [
4 0 => "Terry Crews"
5 1 => "Aubrey Plaza"
6 ]
7 "brands" => array:2 [
8 0 => "Asics"
9 1 => "Nike"
10 ]
11]

If we change the $user_prompt to:

1I went running with my friend Mike

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

1array:2 [
2 "success" => false
3 "message" => "No results found."
4]

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.