@postalsys/email-ai-tools 1.6.3 → 1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/examples/question.js +1 -1
- package/lib/embeddings-query.js +25 -10
- package/lib/generate-embeddings.js +15 -4
- package/lib/generate-summary.js +16 -6
- package/lib/risk-analysis.js +12 -2
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.7.1](https://github.com/postalsys/email-ai-tools/compare/v1.7.0...v1.7.1) (2023-10-26)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* **questions:** Tuned sort ordering ([6f6b3f4](https://github.com/postalsys/email-ai-tools/commit/6f6b3f467e70cca1dab82410c07029ca98932d7d))
|
|
9
|
+
|
|
10
|
+
## [1.7.0](https://github.com/postalsys/email-ai-tools/compare/v1.6.3...v1.7.0) (2023-10-24)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
* **openai:** Allow to specify base API url ([04ebd3d](https://github.com/postalsys/email-ai-tools/commit/04ebd3d5adfd88ec7a681bbffed4fb302c982846))
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Bug Fixes
|
|
19
|
+
|
|
20
|
+
* **logging:** Log API url if verbose ([2e2b9d9](https://github.com/postalsys/email-ai-tools/commit/2e2b9d9bc605b3261f420855d5ae2d0ca5878153))
|
|
21
|
+
|
|
3
22
|
## [1.6.3](https://github.com/postalsys/email-ai-tools/compare/v1.6.2...v1.6.3) (2023-10-20)
|
|
4
23
|
|
|
5
24
|
|
package/examples/question.js
CHANGED
|
@@ -4,7 +4,7 @@ const { questionQuery } = require('../lib/embeddings-query');
|
|
|
4
4
|
const util = require('util');
|
|
5
5
|
|
|
6
6
|
async function main() {
|
|
7
|
-
const question = 'How much did my last purchase on Amazon cost?';
|
|
7
|
+
const question = process.argv[2] || 'How much did my last purchase on Amazon cost?';
|
|
8
8
|
|
|
9
9
|
const info = await questionQuery(question, process.env.OPENAI_API_KEY, {
|
|
10
10
|
//gptModel: 'gpt-3.5-turbo',
|
package/lib/embeddings-query.js
CHANGED
|
@@ -11,8 +11,11 @@ const tokenizer = new GPT3Tokenizer({ type: 'gpt3' });
|
|
|
11
11
|
|
|
12
12
|
const MAX_ALLOWED_TEXT_LENGTH = 32 * 1024;
|
|
13
13
|
const MAX_ALLOWED_TOKENS = 4000;
|
|
14
|
-
|
|
15
|
-
const
|
|
14
|
+
|
|
15
|
+
const OPENAI_API_BASE_URL = 'https://api.openai.com';
|
|
16
|
+
|
|
17
|
+
const OPENAI_API_URL_CHAT = '/v1/chat/completions';
|
|
18
|
+
const OPENAI_API_URL_INSTRUCT = '/v1/completions';
|
|
16
19
|
|
|
17
20
|
const DEFAULT_SYSTEM_PROMPT = `
|
|
18
21
|
You are an automated system designed to extract and provide information based on stored emails.
|
|
@@ -89,7 +92,10 @@ You are analyzing user questions regarding email retrieval from a database. From
|
|
|
89
92
|
**Output Guidelines**:
|
|
90
93
|
|
|
91
94
|
- For terms implying a near-future context (e.g., "next", "newest", "upcoming"), opt for the 'newer_first' ordering.
|
|
92
|
-
- For terms implying a distant past (e.g., "
|
|
95
|
+
- For terms implying a distant past (e.g., "oldest"), use the 'older_first' ordering.
|
|
96
|
+
- For terms implying ordering by time:
|
|
97
|
+
- start of the list (e.g., "first"), use the 'older_first' ordering.
|
|
98
|
+
- end of the list (e.g., "last", "latest"), opt for the 'newer_first' ordering.
|
|
93
99
|
- If the user's query does not provide a clear time frame, or if the system's confidence in deducing a timeframe is below 70%, exclude 'start_time' and 'end_time' from the output.
|
|
94
100
|
- If the deduced 'end_time' aligns with current time, omit the 'end_time'.
|
|
95
101
|
- For unspecified time zones, timestamps should follow the 'YYYY-MM-DD hh:mm:ss' format.
|
|
@@ -111,15 +117,22 @@ You are analyzing user questions regarding email retrieval from a database. From
|
|
|
111
117
|
- **Query**: "What did James write to me about last Friday?" (assuming that current time is "2023-10-02")
|
|
112
118
|
**Response**: \`{"ordering":"best_match", "start_time": "2023-09-29", "end_time": "2023-09-30", "topic": "A message from James about an event in 2023-09-30"}\`
|
|
113
119
|
- **Query**: "When did I receive my first Amazon invoice?"
|
|
114
|
-
**Response**: \`{"ordering":"older_first", "topic": "Amazon invoice
|
|
120
|
+
**Response**: \`{"ordering":"older_first", "topic": "Amazon invoice"}\`
|
|
115
121
|
|
|
116
122
|
**User's Query**:
|
|
117
123
|
Process the user question:
|
|
118
124
|
`.trim();
|
|
119
125
|
|
|
126
|
+
function getApiUrl(baseApiUrl, path) {
|
|
127
|
+
let url = new URL(path || '/', baseApiUrl || OPENAI_API_BASE_URL);
|
|
128
|
+
return url.href;
|
|
129
|
+
}
|
|
130
|
+
|
|
120
131
|
async function embeddingsQuery(apiToken, opts) {
|
|
121
132
|
opts = opts || {};
|
|
122
133
|
|
|
134
|
+
let baseApiUrl = opts.baseApiUrl || OPENAI_API_BASE_URL;
|
|
135
|
+
|
|
123
136
|
let systemPrompt = (opts.systemPrompt || DEFAULT_SYSTEM_PROMPT).toString().trim();
|
|
124
137
|
let question = (opts.question || '').toString().trim();
|
|
125
138
|
let contextChunks = (opts.contextChunks || '').toString().trim();
|
|
@@ -208,7 +221,7 @@ ${promptText}
|
|
|
208
221
|
switch (gptModel) {
|
|
209
222
|
case 'gpt-3.5-turbo-instruct':
|
|
210
223
|
{
|
|
211
|
-
openAiAPIURL = OPENAI_API_URL_INSTRUCT;
|
|
224
|
+
openAiAPIURL = getApiUrl(baseApiUrl, OPENAI_API_URL_INSTRUCT);
|
|
212
225
|
payload.prompt = `${systemPrompt}\n${prompt}`;
|
|
213
226
|
let tokens = tokenizer.encode(payload.prompt);
|
|
214
227
|
payload.max_tokens = 4000 - tokens.bpe.length;
|
|
@@ -218,7 +231,7 @@ ${promptText}
|
|
|
218
231
|
case 'gpt-3.5-turbo':
|
|
219
232
|
case 'gpt-4':
|
|
220
233
|
default:
|
|
221
|
-
openAiAPIURL = OPENAI_API_URL_CHAT;
|
|
234
|
+
openAiAPIURL = getApiUrl(baseApiUrl, OPENAI_API_URL_CHAT);
|
|
222
235
|
payload.messages = [
|
|
223
236
|
{
|
|
224
237
|
role: 'system',
|
|
@@ -233,7 +246,7 @@ ${promptText}
|
|
|
233
246
|
}
|
|
234
247
|
|
|
235
248
|
if (opts.verbose) {
|
|
236
|
-
console.error(util.inspect({ requestId, payload }, false, 8, true));
|
|
249
|
+
console.error(util.inspect({ requestId, apiUrl: openAiAPIURL, payload }, false, 8, true));
|
|
237
250
|
}
|
|
238
251
|
|
|
239
252
|
let run = async () => {
|
|
@@ -348,6 +361,8 @@ ${promptText}
|
|
|
348
361
|
async function questionQuery(question, apiToken, opts) {
|
|
349
362
|
opts = opts || {};
|
|
350
363
|
|
|
364
|
+
let baseApiUrl = opts.baseApiUrl || OPENAI_API_BASE_URL;
|
|
365
|
+
|
|
351
366
|
let systemPrompt = (opts.systemPrompt || DEFAULT_SYSTEM_PROMPT).toString().trim();
|
|
352
367
|
question = (question || '').toString().trim();
|
|
353
368
|
if (!question) {
|
|
@@ -396,7 +411,7 @@ ${question}
|
|
|
396
411
|
switch (gptModel) {
|
|
397
412
|
case 'gpt-3.5-turbo-instruct':
|
|
398
413
|
{
|
|
399
|
-
openAiAPIURL = OPENAI_API_URL_INSTRUCT;
|
|
414
|
+
openAiAPIURL = getApiUrl(baseApiUrl, OPENAI_API_URL_INSTRUCT);
|
|
400
415
|
payload.prompt = `${systemPrompt}\n${prompt}`;
|
|
401
416
|
let tokens = tokenizer.encode(payload.prompt);
|
|
402
417
|
payload.max_tokens = 4000 - tokens.bpe.length;
|
|
@@ -406,7 +421,7 @@ ${question}
|
|
|
406
421
|
case 'gpt-3.5-turbo':
|
|
407
422
|
case 'gpt-4':
|
|
408
423
|
default:
|
|
409
|
-
openAiAPIURL = OPENAI_API_URL_CHAT;
|
|
424
|
+
openAiAPIURL = getApiUrl(baseApiUrl, OPENAI_API_URL_CHAT);
|
|
410
425
|
payload.messages = [
|
|
411
426
|
{
|
|
412
427
|
role: 'system',
|
|
@@ -421,7 +436,7 @@ ${question}
|
|
|
421
436
|
}
|
|
422
437
|
|
|
423
438
|
if (opts.verbose) {
|
|
424
|
-
console.error(util.inspect({ requestId, payload }, false, 8, true));
|
|
439
|
+
console.error(util.inspect({ requestId, apiUrl: openAiAPIURL, payload }, false, 8, true));
|
|
425
440
|
}
|
|
426
441
|
|
|
427
442
|
let run = async () => {
|
|
@@ -21,10 +21,19 @@ const linkify = linkifyIt()
|
|
|
21
21
|
|
|
22
22
|
const tokenizer = new GPT3Tokenizer({ type: 'gpt3' });
|
|
23
23
|
|
|
24
|
-
const
|
|
24
|
+
const OPENAI_API_BASE_URL = 'https://api.openai.com';
|
|
25
|
+
|
|
26
|
+
const OPENAI_API_URL = '/v1/embeddings';
|
|
27
|
+
|
|
28
|
+
function getApiUrl(baseApiUrl, path) {
|
|
29
|
+
let url = new URL(path || '/', baseApiUrl || OPENAI_API_BASE_URL);
|
|
30
|
+
return url.href;
|
|
31
|
+
}
|
|
25
32
|
|
|
26
33
|
async function getChunkEmbeddings(chunk, apiToken, opts) {
|
|
27
|
-
let { gptModel, verbose } = opts || {};
|
|
34
|
+
let { gptModel, verbose, baseApiUrl } = opts || {};
|
|
35
|
+
|
|
36
|
+
baseApiUrl = baseApiUrl || OPENAI_API_BASE_URL;
|
|
28
37
|
|
|
29
38
|
gptModel = gptModel || 'text-embedding-ada-002';
|
|
30
39
|
|
|
@@ -49,12 +58,14 @@ async function getChunkEmbeddings(chunk, apiToken, opts) {
|
|
|
49
58
|
let data;
|
|
50
59
|
let retries = 0;
|
|
51
60
|
|
|
61
|
+
const openAiAPIURL = getApiUrl(baseApiUrl, OPENAI_API_URL);
|
|
62
|
+
|
|
52
63
|
if (verbose) {
|
|
53
64
|
console.error(util.inspect({ requestId, payload }, false, 8, true));
|
|
54
65
|
}
|
|
55
66
|
|
|
56
67
|
let run = async () => {
|
|
57
|
-
res = await fetchCmd(
|
|
68
|
+
res = await fetchCmd(openAiAPIURL, {
|
|
58
69
|
method: 'post',
|
|
59
70
|
headers,
|
|
60
71
|
body: JSON.stringify(payload),
|
|
@@ -94,7 +105,7 @@ async function getChunkEmbeddings(chunk, apiToken, opts) {
|
|
|
94
105
|
const reqEndTime = Date.now();
|
|
95
106
|
|
|
96
107
|
if (opts.verbose) {
|
|
97
|
-
console.error(util.inspect({ requestId, output: data }, false, 8, true));
|
|
108
|
+
console.error(util.inspect({ requestId, apiUrl: openAiAPIURL, output: data }, false, 8, true));
|
|
98
109
|
}
|
|
99
110
|
|
|
100
111
|
let embedding = data?.data?.[0]?.embedding;
|
package/lib/generate-summary.js
CHANGED
|
@@ -12,8 +12,11 @@ const tokenizer = new GPT3Tokenizer({ type: 'gpt3' });
|
|
|
12
12
|
|
|
13
13
|
const MAX_ALLOWED_TEXT_LENGTH = 32 * 1024;
|
|
14
14
|
const MAX_ALLOWED_TOKENS = 4000;
|
|
15
|
-
|
|
16
|
-
const
|
|
15
|
+
|
|
16
|
+
const OPENAI_API_BASE_URL = 'https://api.openai.com';
|
|
17
|
+
|
|
18
|
+
const OPENAI_API_URL_CHAT = '/v1/chat/completions';
|
|
19
|
+
const OPENAI_API_URL_INSTRUCT = '/v1/completions';
|
|
17
20
|
|
|
18
21
|
const DEFAULT_ALLOWED_HEADERS = ['from', 'to', 'cc', 'bcc', 'subject', 'mime-version', 'authentication-results', 'date', 'content-type', 'list-id'];
|
|
19
22
|
|
|
@@ -114,9 +117,16 @@ Output facts:
|
|
|
114
117
|
|
|
115
118
|
Analyze the following email:`.trim();
|
|
116
119
|
|
|
120
|
+
function getApiUrl(baseApiUrl, path) {
|
|
121
|
+
let url = new URL(path || '/', baseApiUrl || OPENAI_API_BASE_URL);
|
|
122
|
+
return url.href;
|
|
123
|
+
}
|
|
124
|
+
|
|
117
125
|
async function generateSummary(message, apiToken, opts) {
|
|
118
126
|
opts = opts || {};
|
|
119
127
|
|
|
128
|
+
let baseApiUrl = opts.baseApiUrl || OPENAI_API_BASE_URL;
|
|
129
|
+
|
|
120
130
|
let systemPrompt = (opts.systemPrompt || DEFAULT_SYSTEM_PROMPT).toString().trim();
|
|
121
131
|
let userPrompt = (opts.userPrompt || DEFAULT_USER_PROMPT).toString().trim();
|
|
122
132
|
|
|
@@ -241,7 +251,7 @@ ${JSON.stringify(content)}`;
|
|
|
241
251
|
switch (gptModel) {
|
|
242
252
|
case 'gpt-3.5-turbo-instruct':
|
|
243
253
|
{
|
|
244
|
-
openAiAPIURL = OPENAI_API_URL_INSTRUCT;
|
|
254
|
+
openAiAPIURL = getApiUrl(baseApiUrl, OPENAI_API_URL_INSTRUCT);
|
|
245
255
|
payload.prompt = `${systemPrompt}\n${prompt}`;
|
|
246
256
|
let tokens = tokenizer.encode(payload.prompt);
|
|
247
257
|
payload.max_tokens = 4000 - tokens.bpe.length;
|
|
@@ -251,7 +261,7 @@ ${JSON.stringify(content)}`;
|
|
|
251
261
|
case 'gpt-3.5-turbo':
|
|
252
262
|
case 'gpt-4':
|
|
253
263
|
default:
|
|
254
|
-
openAiAPIURL = OPENAI_API_URL_CHAT;
|
|
264
|
+
openAiAPIURL = getApiUrl(baseApiUrl, OPENAI_API_URL_CHAT);
|
|
255
265
|
payload.messages = [
|
|
256
266
|
{
|
|
257
267
|
role: 'system',
|
|
@@ -266,7 +276,7 @@ ${JSON.stringify(content)}`;
|
|
|
266
276
|
}
|
|
267
277
|
|
|
268
278
|
if (opts.verbose) {
|
|
269
|
-
console.error(util.inspect({ requestId, payload }, false, 8, true));
|
|
279
|
+
console.error(util.inspect({ requestId, apiUrl: openAiAPIURL, payload }, false, 8, true));
|
|
270
280
|
}
|
|
271
281
|
|
|
272
282
|
let run = async () => {
|
|
@@ -322,7 +332,7 @@ ${JSON.stringify(content)}`;
|
|
|
322
332
|
.trim();
|
|
323
333
|
|
|
324
334
|
if (opts.verbose) {
|
|
325
|
-
console.error(util.inspect({ output, data }, false, 8, true));
|
|
335
|
+
console.error(util.inspect({ requestId, output, data }, false, 8, true));
|
|
326
336
|
}
|
|
327
337
|
|
|
328
338
|
try {
|
package/lib/risk-analysis.js
CHANGED
|
@@ -11,7 +11,10 @@ const tokenizer = new GPT3Tokenizer({ type: 'gpt3' });
|
|
|
11
11
|
|
|
12
12
|
const MAX_ALLOWED_TEXT_LENGTH = 32 * 1024;
|
|
13
13
|
const MAX_ALLOWED_TOKENS = 4000;
|
|
14
|
-
|
|
14
|
+
|
|
15
|
+
const OPENAI_API_BASE_URL = 'https://api.openai.com';
|
|
16
|
+
|
|
17
|
+
const OPENAI_API_URL = '/v1/chat/completions';
|
|
15
18
|
|
|
16
19
|
const DEFAULT_SYSTEM_PROMPT = `
|
|
17
20
|
I want you to act as are an IT security expert that monitors emails for suspicious and risky activity.
|
|
@@ -63,9 +66,16 @@ Facts:
|
|
|
63
66
|
Analyze the following email:
|
|
64
67
|
`.trim();
|
|
65
68
|
|
|
69
|
+
function getApiUrl(baseApiUrl, path) {
|
|
70
|
+
let url = new URL(path || '/', baseApiUrl || OPENAI_API_BASE_URL);
|
|
71
|
+
return url.href;
|
|
72
|
+
}
|
|
73
|
+
|
|
66
74
|
async function riskAnalysis(message, apiToken, opts) {
|
|
67
75
|
opts = opts || {};
|
|
68
76
|
|
|
77
|
+
let baseApiUrl = opts.baseApiUrl || OPENAI_API_BASE_URL;
|
|
78
|
+
|
|
69
79
|
let systemPrompt = opts.systemPrompt || DEFAULT_SYSTEM_PROMPT;
|
|
70
80
|
let userPrompt = opts.userPrompt || DEFAULT_USER_PROMPT;
|
|
71
81
|
|
|
@@ -179,7 +189,7 @@ ${JSON.stringify(content)}`;
|
|
|
179
189
|
let retries = 0;
|
|
180
190
|
|
|
181
191
|
let run = async () => {
|
|
182
|
-
res = await fetchCmd(OPENAI_API_URL, {
|
|
192
|
+
res = await fetchCmd(getApiUrl(baseApiUrl, OPENAI_API_URL), {
|
|
183
193
|
method: 'post',
|
|
184
194
|
headers,
|
|
185
195
|
body: JSON.stringify(payload),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@postalsys/email-ai-tools",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.1",
|
|
4
4
|
"description": "Email AI tools",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -28,9 +28,9 @@
|
|
|
28
28
|
"gpt3-tokenizer": "1.1.5",
|
|
29
29
|
"libmime": "5.2.1",
|
|
30
30
|
"linkify-it": "4.0.1",
|
|
31
|
-
"nodemailer": "6.9.
|
|
31
|
+
"nodemailer": "6.9.7",
|
|
32
32
|
"punycode": "2.3.0",
|
|
33
33
|
"tlds": "1.243.0",
|
|
34
|
-
"undici": "5.26.
|
|
34
|
+
"undici": "5.26.5"
|
|
35
35
|
}
|
|
36
36
|
}
|