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

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

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

[ad_1]

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

Picture by https://unsplash.com/es/@tma

Historically, any pure language processing textual content classification challenge would begin with gathering cases, defining their respective labels, and coaching a classification mannequin, corresponding to a logistic regression mannequin, to categorise the cases. At the moment, the fashions obtainable in OpenAI may be immediately used for classification duties that will usually require amassing 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 supplies a graphical interface for the fashions applied by OpenAI. Nevertheless, what if we need to run these fashions immediately in Python? Properly, the obtainable various is the OpenAI API, which permits us to entry their fashions from a programming surroundings. On this article, we’ll describe with a short instance how we will 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 enable 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, notably for large-scale initiatives, it suffices for the needs of this text’s instance.

As soon as now we have created the account, we will entry the OpenAI fashions obtainable 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 afterward.

The subsequent 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 desirous about two issues. Firstly, a worth between 0 and 1 indicating the likelihood of the SMS being spam or not, and secondly, a proof of the mannequin’s reasoning behind that call. Moreover, we want the end in JSON format in order that we will later convert it right into a dataframe. Beneath is the template we’ll use for these predictions.

Now, we’d like some messages to check how nicely the OpenAI mannequin predicts whether or not a message is spam or not. For this function, we’ll use the SMS Spam Assortment dataset obtainable 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 isn’t 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 nicely the pre-trained OpenAI mannequin can predict this drawback. As talked about at first of the article, there are important limitations relating to the variety of requests per minute and per day that we will make with the free model. The dataset accommodates 5,574 cases, however to check the mannequin, we’ll solely use the primary 50 cases. In case you have a paid model of OpenAI, you may enhance the variety of messages examined because the time limitations are a lot decrease.

Labels of cases (Picture created by the creator)

Earlier than making predictions with the mannequin, now we have verified that our dataset of fifty cases accommodates each messages which are 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 under, now we have stored monitor of the obtainable credit and tokens always. Moreover, now we have applied a sleep perform within the code for 60 seconds to make sure compliance with the restrictions relating to 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 supply 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 worth between 0 and 1. A price of 0 signifies that the message isn’t spam, whereas a worth of 1 signifies that it’s. To assign the labels, we’ll use a threshold of 0.5, which means {that a} rating increased 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 anticipated labels and assess how nicely 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 are 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 charge, accurately classifying 47 out of fifty cases.

Since we’ve requested the mannequin to supply us with info relating to its reasoning for classifying a message as spam or not, we will look at why it misclassified 3 messages that don’t belong to the spam class. Beneath are the reasons offered by the mannequin:

  • The message accommodates 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. Due to this fact, there’s a excessive likelihood of this message being spam.
  • The message accommodates suggestive and inappropriate content material, which is a typical attribute of spam messages. Moreover, the usage of improper grammar and language could point out that this message is spam. Due to this fact, the likelihood of this message being spam is excessive.
  • The message accommodates suggestive content material associated to non-public attributes, which is a typical attribute of spam messages. Moreover, the usage of express language will increase the probability of this message being thought-about spam.

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

Historically, initiatives requiring textual content classification started with labeled databases and a mannequin that wanted coaching. The emergence of pre-trained Language Fashions (LLMs) now gives the potential of classifying a mess of texts with out the necessity to prepare a mannequin beforehand, as these fashions have already been skilled for a lot of use instances. On this article, now we have defined easy methods to 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 cases accurately, which signifies a excessive stage of accuracy. Sooner or later, it could possibly be worthwhile to guage different OpenAI fashions and discover totally different prompts which will yield higher efficiency. By the way in which, the execution of the challenge solely costed $0.007.

[ad_2]