False positives in Gmail are unusual however can occur, which means an essential e mail may mistakenly find yourself in your spam folder. Once you’re coping with a whole bunch of spam messages every day, figuring out these authentic emails turns into much more difficult.
You’ll be able to create filters in Gmail such that emails from particular senders or with sure key phrases are by no means marked as spam. However these filters would clearly not work for emails from new or unknown senders.
Discover incorrectly labeled messages in Gmail Spam
What if we used AI to research our spam emails in Gmail and predict which of them are probably false positives? With this checklist of misclassified emails, we might robotically transfer these emails to the inbox or generate a report for handbook assessment.
Right here’s a pattern report generated from Gmail. It features a checklist of emails with a low spam rating which can be probably authentic and must be moved to the inbox. The report additionally features a abstract of the e-mail content material in your most popular language.
To get began, open this Google Script and make a replica of it in your Google Drive. Change to the Apps Script editor and supply your e mail handle, OpenAI API key, and most popular language for the e-mail abstract.
Select the reportFalsePositives
perform from the dropdown and click on the play button to run the script. It is going to seek for unread spam emails in your Gmail account, analyze them utilizing OpenAI’s API, and ship you a report of emails with a low spam rating.
If you want to run this script robotically at common intervals, go to the “Triggers” menu within the Google Apps Script editor and arrange a time-driven set off to run this script as soon as day-after-day as proven under. You can even select the time of the day if you want to obtain the report.
How AI Spam Classification Works – The Technical Half
If you’re curious to know the way the script works, here’s a temporary overview:
The Gmail Script makes use of the Gmail API to seek for unread spam emails in your Gmail account. It then sends the e-mail content material to OpenAI’s API to categorise the spam rating and generate a abstract in your most popular language. Emails with a low spam rating are probably false positives and may be moved to the inbox.
1. Consumer Configuration
You’ll be able to present your e mail handle the place the report must be despatched, your OpenAI API key, your most popular LLM mannequin, and the language for the e-mail abstract.
// Primary configuration
const USER_EMAIL = 'e mail@area.com'; // E-mail handle to ship the report back to
const OPENAI_API_KEY = 'sk-proj-123'; // API key for OpenAI
const OPENAI_MODEL = 'gpt-4o'; // Mannequin title to make use of with OpenAI
const USER_LANGUAGE = 'English'; // Language for the e-mail abstract
2. Discover Unread Emails in Gmail Spam Folder
We use the epoch time to seek out spam emails that arrived within the final 24 hours and are nonetheless unread.
const HOURS_AGO = 24; // Time-frame to seek for emails (in hours)
const MAX_THREADS = 25; // Most variety of e mail threads to course of
const getSpamThreads_ = () => {
const epoch = (date) => Math.ground(date.getTime() / 1000);
const beforeDate = new Date();
const afterDate = new Date();
afterDate.setHours(afterDate.getHours() - HOURS_AGO);
const searchQuery = `is:unread in:spam after:${epoch(afterDate)} earlier than:${epoch(beforeDate)}`;
return GmailApp.search(searchQuery, 0, MAX_THREADS);
};
3. Create a Immediate for the OpenAI Mannequin
We create a immediate for the OpenAI mannequin utilizing the e-mail message. The immediate asks the AI mannequin to research the e-mail content material and assign a spam rating on a scale from 0 to 10. The response must be in JSON format.
const SYSTEM_PROMPT = `You're an AI e mail classifier. Given the content material of an e mail, analyze it and assign a spam rating on a scale from 0 to 10, the place 0 signifies a authentic e mail and 10 signifies a particular spam e mail. Present a brief abstract of the e-mail in ${USER_LANGUAGE}. Your response must be in JSON format.`;
const MAX_BODY_LENGTH = 200; // Most size of e mail physique to incorporate within the AI immediate
const getMessagePrompt_ = (message) => {
const physique = message
.getPlainBody()
.change(/https?://[^s>]+/g, '')
.change(/[nrt]/g, ' ')
.change(/s+/g, ' ')
.trim(); // take away all URLs, and whitespace characters
return [
`Subject: ${message.getSubject()}`,
`Sender: ${message.getFrom()}`,
`Body: ${body.substring(0, MAX_BODY_LENGTH)}`
].be a part of('n');
};
4. Name the OpenAI API to get the Spam Rating
We go the message immediate to the OpenAI API and get the spam rating and a abstract of the e-mail content material. The spam rating is used to find out if the e-mail is a false constructive.
The tokens
variable retains monitor of the variety of tokens used within the OpenAI API calls and is included within the e mail report. You need to use this info to watch your API utilization.
let tokens = 0;
const getMessageScore_ = (messagePrompt) => {
const apiUrl = `https://api.openai.com/v1/chat/completions`;
const headers = {
'Content material-Kind': 'software/json',
Authorization: `Bearer ${OPENAI_API_KEY}`
};
const response = UrlFetchApp.fetch(apiUrl, {
technique: 'POST',
headers,
payload: JSON.stringify({
mannequin: OPENAI_MODEL,
messages: [
{ role: 'system', content: SYSTEM_PROMPT },
{ role: 'user', content: messagePrompt }
],
temperature: 0.2,
max_tokens: 124,
response_format: { kind: 'json_object' }
})
});
const information = JSON.parse(response.getContentText());
tokens += information.utilization.total_tokens;
const content material = JSON.parse(information.decisions[0].message.content material);
return content material;
};
5. Course of Spam Emails and e mail the Report
You’ll be able to run this Google script manually or arrange a cron set off to run it robotically at common intervals. It marks the spam emails as learn so that they aren’t processed once more.
const SPAM_THRESHOLD = 2; // Threshold for spam rating to incorporate within the report
const reportFalsePositives = () => {
const html = [];
const threads = getSpamThreads_();
for (let i = 0; i < threads.size; i += 1) {
const [message] = threads[i].getMessages();
const messagePrompt = getMessagePrompt_(message);
// Get the spam rating and abstract from OpenAI
const { spam_score, abstract } = getMessageScore_(messagePrompt);
if (spam_score <= SPAM_THRESHOLD) {
// Add e mail message to the report if the spam rating is under the brink
html.push(`${message.getFrom()} ${abstract} `);
}
}
threads.forEach((thread) => thread.markRead()); // Mark all processed emails as learn
if (html.size > 0) {
const htmlBody = [
``,
'Email Sender Summary ',
html.join(''),
'
'
].be a part of('');
const topic = `Gmail Spam Report - ${tokens} tokens used`;
GmailApp.sendEmail(USER_EMAIL, topic, '', { htmlBody });
}
};
Additionally see: Authenticate your Gmail messages