-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (76 loc) · 2.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Import your dependencies
import "dotenv/config.js";
import Nylas from "nylas";
import { Configuration, OpenAIApi } from "openai";
import chalk from "chalk";
// Configure your Nylas client
Nylas.config({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
});
const nylas = Nylas.with(process.env.ACCESS_TOKEN);
// Configure your OpenAI client
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
// Get messages from Nylas
const getMessageList = async () => {
try {
const messageList = await nylas.messages.list({ limit: 10 });
console.log(`Found ${messageList.length} messages in your inbox...`);
return messageList;
} catch (err) {
console.error("Error:\n", err);
}
};
// Pass in a message list and get a list of classified messages
const classifyMessages = async (messageList) => {
return Promise.all(
messageList.map(async (message) => {
const { id, date, from, subject } = message;
const formattedDate = new Date(date).toLocaleDateString();
const messageLabel = await classifyMessage(message);
return { id, formattedDate, from, subject, messageLabel };
})
);
};
// Pass a message to GPT
// Get a string value for whether the user should read and why
const classifyMessage = async ({ from, subject, snippet }) => {
try {
const response = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [
{
role: "system",
content: `You're an email assistant and you help me figure out which emails are something I should read and which are not worth my time.
This is very important—the following are categories I want to avoid: spam, newsletters, sales messages, junk.
Answer with "Yes" or "No", then a comma followed by a one-word category to demonstrate your reason.
Is the following message something I should read?
From: ${from}
Subject: ${subject}
Snippet: ${snippet}`,
},
],
});
return response.data.choices[0].message.content;
} catch (error) {
console.error(`Error: ${error.response.status}:`);
console.dir(error.response.data, { depth: null });
process.exit(1);
}
};
// Run the script
const messageList = await getMessageList();
const classifiedMessages = await classifyMessages(messageList);
// Log the results
classifiedMessages.forEach(
({ formattedDate, from, subject, messageLabel, id }) =>
console.log(
`Message on ${formattedDate} from: ${from[0].email}
Subject: ${subject}
${chalk.bold("Read? Why?:")} ${chalk.blue.bold(messageLabel)}
Message id: ${id}`
)
);