Monday, March 18, 2024

Using the OpenAI API for Detection of SMS Spam Messages | by Amanda Iglesias Moreno | Mar, 2024

Must read

Unlocking environment friendly textual content classification with pre-trained fashions: a case research utilizing OpenAI’s GPT-3.5-turbo

Towards Data Science
Picture by

Historically, any pure language processing textual content classification mission would begin with gathering situations, defining their respective labels, and coaching a classification mannequin, resembling a logistic regression mannequin, to categorise the situations. At the moment, the fashions accessible in OpenAI could be immediately used for classification duties that might sometimes require gathering a considerable quantity of labeled information to coach the mannequin. These pre-trained fashions can be utilized for a number of text-processing duties, together with classification, summarization, spell-checking, and key phrase identification.
We don’t require any labeled information or the necessity to prepare a mannequin. Easy, proper?

ChatGPT offers a graphical interface for the fashions carried out by OpenAI. Nonetheless, what if we wish to run these fashions immediately in Python? Effectively, the accessible different is the OpenAI API, which permits us to entry their fashions from a programming setting. On this article, we’ll describe with a short instance how we are able to entry the API to detect whether or not an SMS is spam or not. To perform this, we’ll make the most of one of many Open AI fashions, particularly the GPT-3.5-turbo mannequin.

The preliminary step to entry the OpenAI API includes creating an account on OpenAI to acquire the API key required for accessing the fashions. Upon creating the account, we’ll have $5 of credit score at our disposal, which, as we’ll later observe, will permit us to conduct quite a few assessments.

On this instance, we’ll make the most of the free model of OpenAI, which comes with limitations on requests per minute and per day. Adhering to those limits is essential to keep away from Price Restrict Errors. The values for these two parameters are set at 3 requests per minute and 200 per day. Whereas this naturally imposes constraints, significantly for large-scale tasks, it suffices for the needs of this text’s instance.

As soon as we’ve got created the account, we are able to entry the OpenAI fashions accessible for the free model from Python utilizing the OpenAI library. First, we create a perform known as chat_with_gpt to entry the GPT-3.5-turbo mannequin. The enter to this perform would be the immediate, which we’ll design in a while.

The following step is to create the immediate that we are going to present to the GPT-3.5-turbo mannequin. On this explicit case, we’re excited by two issues. Firstly, a price between 0 and 1 indicating the likelihood of the SMS being spam or not, and secondly, an evidence of the mannequin’s reasoning behind that call. Moreover, we want the end in JSON format in order that we are able to later convert it right into a dataframe. Under is the template we’ll use for these predictions.

Now, we’d like some messages to check how effectively the OpenAI mannequin predicts whether or not a message is spam or not. For this goal, we’ll use the SMS Spam Assortment dataset accessible on the UCI Machine Studying Repository.

We learn the info and convert it right into a DataFrame. As noticed, the dataset consists of two columns: one containing the messages and the second containing the corresponding labels. ham signifies that the message will not be spam, whereas spam signifies that it’s.

Spam Assortment dataset (Picture created by the creator)

Now, we’ll use the mannequin to detect whether or not the messages are spam or not and consider how effectively the pre-trained OpenAI mannequin can predict this downside. As talked about initially of the article, there are important limitations concerning the variety of requests per minute and per day that we are able to make with the free model. The dataset comprises 5,574 situations, however to check the mannequin, we’ll solely use the primary 50 situations. When you’ve got a paid model of OpenAI, you possibly can enhance the variety of messages examined because the time limitations are a lot decrease.

Labels of situations (Picture created by the creator)

Earlier than making predictions with the mannequin, we’ve got verified that our dataset of fifty situations comprises each messages which might be spam and messages that aren’t. In complete, there are 40 non-spam messages and 10 spam messages.

Lastly, we proceed to make the prediction. As proven beneath, we’ve got stored observe of the accessible credit and tokens always. Moreover, we’ve got carried out a sleep perform within the code for 60 seconds to make sure compliance with the restrictions concerning the variety of requests.

The message offered by the mannequin with the prediction has been saved in a variable known as prediction. This message is a JSON file with the next construction: { “spam”: “0.1”, “reasoning”: “The message appears to be a typical promotional message a couple of buffet provide and doesn't comprise any typical spam key phrases or traits. The likelihood of this message being spam is low.” }.

Testing output (Picture created by the creator)

The predictions yield a price between 0 and 1. A price of 0 signifies that the message will not be spam, whereas a price of 1 signifies that it’s. To assign the labels, we’ll use a threshold of 0.5, that means {that a} rating greater than 0.5 by the mannequin will categorize the message as spam.

Predicted Labels (Picture created by the creator)

Now, all that’s left is to check the precise labels with the expected labels and assess how effectively the GPT-3.5-turbo mannequin has carried out the predictions.

Confusion Matrix (Picture created by the creator)

Above is the confusion matrix of the mannequin.

  • For messages that aren’t spam (ham), the mannequin accurately predicted 37 of them as not spam (true negatives), however misclassified 3 of them as spam (false positives).
  • For messages which might be spam, the mannequin accurately predicted 10 of them as spam (true positives), with no noticed false negatives on this case.

The mannequin demonstrates good sensitivity in detecting spam messages, with a number of false positives indicating potential areas for enchancment in its precision. As proven, the mannequin achieved a 94% accuracy fee, accurately classifying 47 out of fifty situations.

Since we’ve requested the mannequin to offer us with data concerning its reasoning for classifying a message as spam or not, we are able to look at why it misclassified 3 messages that don’t belong to the spam class. Under are the reasons offered by the mannequin:

  • The message comprises uncommon language and grammar errors generally related to spam messages. It additionally mentions delicate matters like AIDS, which is a typical tactic utilized in spam messages to evoke feelings and immediate a response. Subsequently, there’s a excessive likelihood of this message being spam.
  • The message comprises suggestive and inappropriate content material, which is a typical attribute of spam messages. Moreover, the usage of improper grammar and language might point out that this message is spam. Subsequently, the likelihood of this message being spam is excessive.
  • The message comprises suggestive content material associated to private attributes, which is a typical attribute of spam messages. Moreover, the usage of specific language will increase the probability of this message being thought of spam.

Messages with inappropriate content material or quite a few spelling errors are typically categorised as spam.

Historically, tasks requiring textual content classification started with labeled databases and a mannequin that wanted coaching. The emergence of pre-trained Language Fashions (LLMs) now presents the potential for classifying a mess of texts with out the necessity to prepare a mannequin beforehand, as these fashions have already been educated for a lot of use instances. On this article, we’ve got defined how one can make the most of the OpenAI API to entry the GPT-3.5-turbo mannequin and decide whether or not a set of messages is spam or not. The mannequin was in a position to classify 94% of the situations accurately, which signifies a excessive degree of accuracy. Sooner or later, it may very well be worthwhile to judge different OpenAI fashions and discover completely different prompts that will yield higher efficiency. By the way in which, the execution of the mission solely costed $0.007.

Supply hyperlink

More articles


Please enter your comment!
Please enter your name here

Latest article