OAI Inferring: Email Subject Sentiment Classifier#


Import Modules#

import os
from openai import OpenAI
from dotenv import load_dotenv

Set an OpenAI Client#

# Get your key: https://platform.openai.com/account/api-keys
load_dotenv()

# Set up OpenAI client
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

Create Completion Function#

def get_completion(prompt, model="gpt-3.5-turbo"):
    messages = [{"role":"user", "content":prompt}]
    response = client.chat.completions.create(
        model=model,
        messages=messages,
        temperature=0, # Degree of randomness
    )
    return response.choices[0].message.content

Email Sentiment Classifier#

Create Prompt Classifier#

prompt = """
Classify the email subject text below, delimited by three dashes (-),
as being malicious or benign. Explain your classification.

---
Account email verification code, enter now and reply
---
"""

Run Prompt#

print(get_completion(prompt))
Benign.

This email subject text appears to be a standard account verification email. It does not contain any suspicious or alarming language that would indicate malicious intent. The purpose of this email is likely to verify the account owner's email address for security purposes.

Provide a few Examples (Few-Shot Learning)#

prompt = """
Classify the email subject text below, delimited by triple backticks ('''),
as being malicious or benign. Explain your classification.

Examples:

Subjet: CY23 Email Verification Now
Label: malicious

Subjet: Enter Market Email Verification Code Today
Label: malicious

Subjet: New Account Email Verification Code Verify now
Label: malicious

Subject: Submit your code review today
Label: benign

Subject: '''Account email verification code, enter now and reply'''
Label:
"""
print(get_completion(prompt))
malicious

Explanation: The subject text contains phrases like "email verification code" and "enter now and reply," which are commonly used in phishing emails to trick users into providing their personal information. This suggests that the email may be malicious and should be treated with caution.