@postalsys/email-ai-tools 1.6.2 → 1.7.0
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/.github/workflows/release.yaml +2 -2
- package/CHANGELOG.md +19 -0
- package/examples/question.js +1 -1
- package/lib/embeddings-query.js +37 -18
- package/lib/generate-embeddings.js +15 -4
- package/lib/generate-summary.js +16 -6
- package/lib/risk-analysis.js +12 -2
- package/package.json +7 -6
|
@@ -17,7 +17,7 @@ jobs:
|
|
|
17
17
|
id: release
|
|
18
18
|
with:
|
|
19
19
|
release-type: node
|
|
20
|
-
package-name: ${{
|
|
20
|
+
package-name: ${{vars.NPM_MODULE_NAME}}
|
|
21
21
|
pull-request-title-pattern: 'chore${scope}: release ${version} [skip-ci]'
|
|
22
22
|
# The logic below handles the npm publication:
|
|
23
23
|
- uses: actions/checkout@v3
|
|
@@ -26,7 +26,7 @@ jobs:
|
|
|
26
26
|
if: ${{ steps.release.outputs.release_created }}
|
|
27
27
|
- uses: actions/setup-node@v3
|
|
28
28
|
with:
|
|
29
|
-
node-version:
|
|
29
|
+
node-version: 20
|
|
30
30
|
registry-url: 'https://registry.npmjs.org'
|
|
31
31
|
if: ${{ steps.release.outputs.release_created }}
|
|
32
32
|
- run: npm ci
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.7.0](https://github.com/postalsys/email-ai-tools/compare/v1.6.3...v1.7.0) (2023-10-24)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* **openai:** Allow to specify base API url ([04ebd3d](https://github.com/postalsys/email-ai-tools/commit/04ebd3d5adfd88ec7a681bbffed4fb302c982846))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* **logging:** Log API url if verbose ([2e2b9d9](https://github.com/postalsys/email-ai-tools/commit/2e2b9d9bc605b3261f420855d5ae2d0ca5878153))
|
|
14
|
+
|
|
15
|
+
## [1.6.3](https://github.com/postalsys/email-ai-tools/compare/v1.6.2...v1.6.3) (2023-10-20)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Bug Fixes
|
|
19
|
+
|
|
20
|
+
* **chat:** generate and return chat topic ([47755fb](https://github.com/postalsys/email-ai-tools/commit/47755fb0a8625df3bc43c2dc5b410d419e90bc03))
|
|
21
|
+
|
|
3
22
|
## [1.6.2](https://github.com/postalsys/email-ai-tools/compare/v1.6.1...v1.6.2) (2023-10-03)
|
|
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 = '
|
|
7
|
+
const question = '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.
|
|
@@ -49,14 +52,15 @@ const SCHEMA_PROMPT = `
|
|
|
49
52
|
|
|
50
53
|
**Output Guidelines:**
|
|
51
54
|
|
|
52
|
-
1.
|
|
53
|
-
2.
|
|
54
|
-
3.
|
|
55
|
-
4.
|
|
55
|
+
1. Answer the user's question using only the provided context information.
|
|
56
|
+
2. Your objective is to sift through the email context and pinpoint the answer that best addresses the given query.
|
|
57
|
+
3. If no email matches the query criteria, or if the match is ambiguous, refrain from providing an answer.
|
|
58
|
+
4. Limit your sources strictly to the provided email context. External references are not to be utilized.
|
|
59
|
+
5. Format your response as follows:
|
|
56
60
|
- Start with \`Answer:\` followed by the relevant information.
|
|
57
61
|
- On a new line, begin with \`Message-ID:\` and cite the unique Message-ID(s) of the emails you sourced your answer from.
|
|
58
|
-
|
|
59
|
-
|
|
62
|
+
6. Ensure that the Message-ID is never embedded within the main body of your response.
|
|
63
|
+
7. Avoid including any additional commentary or annotations.
|
|
60
64
|
|
|
61
65
|
**Context**:
|
|
62
66
|
|
|
@@ -77,6 +81,10 @@ You are analyzing user questions regarding email retrieval from a database. From
|
|
|
77
81
|
- Identify the starting point for the query ('start_time').
|
|
78
82
|
- Identify when to stop the query ('end_time').
|
|
79
83
|
|
|
84
|
+
3. **Topic for the Hypothetical Answer**:
|
|
85
|
+
- Try to answer the question without knowing the actual context and generate a topic for the answer.
|
|
86
|
+
- For missing context assume any generic or likely value
|
|
87
|
+
|
|
80
88
|
**Context**:
|
|
81
89
|
|
|
82
90
|
- The current time is '${new Date().toUTCString()}'.
|
|
@@ -90,29 +98,38 @@ You are analyzing user questions regarding email retrieval from a database. From
|
|
|
90
98
|
- For unspecified time zones, timestamps should follow the 'YYYY-MM-DD hh:mm:ss' format.
|
|
91
99
|
- If only the date is known, use the 'YYYY-MM-DD' format.
|
|
92
100
|
- Assume the week starts on Monday.
|
|
101
|
+
- Identify the topic for the question.
|
|
93
102
|
- Your response should be structured in JSON, strictly adhering to the schema:
|
|
94
103
|
\`\`\`
|
|
95
104
|
{
|
|
96
105
|
"ordering": "",
|
|
97
106
|
"start_time": "",
|
|
98
|
-
"end_time": ""
|
|
107
|
+
"end_time": "",
|
|
108
|
+
"topic": ""
|
|
99
109
|
}
|
|
100
110
|
\`\`\`
|
|
101
111
|
- Example Queries and Responses:
|
|
102
112
|
- **Query**: "When is the next conference event?"
|
|
103
|
-
**Response**: \`{"ordering":"newer_first"}\`
|
|
113
|
+
**Response**: \`{"ordering":"newer_first", "topic": "Conference event happening next week"}\`
|
|
104
114
|
- **Query**: "What did James write to me about last Friday?" (assuming that current time is "2023-10-02")
|
|
105
|
-
**Response**: \`{"ordering":"best_match", "start_time": "2023-09-29", "end_time": "2023-09-30"}\`
|
|
115
|
+
**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"}\`
|
|
106
116
|
- **Query**: "When did I receive my first Amazon invoice?"
|
|
107
|
-
**Response**: \`{"ordering":"older_first"}\`
|
|
117
|
+
**Response**: \`{"ordering":"older_first", "topic": "Amazon invoice sent last week"}\`
|
|
108
118
|
|
|
109
119
|
**User's Query**:
|
|
110
120
|
Process the user question:
|
|
111
121
|
`.trim();
|
|
112
122
|
|
|
123
|
+
function getApiUrl(baseApiUrl, path) {
|
|
124
|
+
let url = new URL(path || '/', baseApiUrl || OPENAI_API_BASE_URL);
|
|
125
|
+
return url.href;
|
|
126
|
+
}
|
|
127
|
+
|
|
113
128
|
async function embeddingsQuery(apiToken, opts) {
|
|
114
129
|
opts = opts || {};
|
|
115
130
|
|
|
131
|
+
let baseApiUrl = opts.baseApiUrl || OPENAI_API_BASE_URL;
|
|
132
|
+
|
|
116
133
|
let systemPrompt = (opts.systemPrompt || DEFAULT_SYSTEM_PROMPT).toString().trim();
|
|
117
134
|
let question = (opts.question || '').toString().trim();
|
|
118
135
|
let contextChunks = (opts.contextChunks || '').toString().trim();
|
|
@@ -201,7 +218,7 @@ ${promptText}
|
|
|
201
218
|
switch (gptModel) {
|
|
202
219
|
case 'gpt-3.5-turbo-instruct':
|
|
203
220
|
{
|
|
204
|
-
openAiAPIURL = OPENAI_API_URL_INSTRUCT;
|
|
221
|
+
openAiAPIURL = getApiUrl(baseApiUrl, OPENAI_API_URL_INSTRUCT);
|
|
205
222
|
payload.prompt = `${systemPrompt}\n${prompt}`;
|
|
206
223
|
let tokens = tokenizer.encode(payload.prompt);
|
|
207
224
|
payload.max_tokens = 4000 - tokens.bpe.length;
|
|
@@ -211,7 +228,7 @@ ${promptText}
|
|
|
211
228
|
case 'gpt-3.5-turbo':
|
|
212
229
|
case 'gpt-4':
|
|
213
230
|
default:
|
|
214
|
-
openAiAPIURL = OPENAI_API_URL_CHAT;
|
|
231
|
+
openAiAPIURL = getApiUrl(baseApiUrl, OPENAI_API_URL_CHAT);
|
|
215
232
|
payload.messages = [
|
|
216
233
|
{
|
|
217
234
|
role: 'system',
|
|
@@ -226,7 +243,7 @@ ${promptText}
|
|
|
226
243
|
}
|
|
227
244
|
|
|
228
245
|
if (opts.verbose) {
|
|
229
|
-
console.error(util.inspect({ requestId, payload }, false, 8, true));
|
|
246
|
+
console.error(util.inspect({ requestId, apiUrl: openAiAPIURL, payload }, false, 8, true));
|
|
230
247
|
}
|
|
231
248
|
|
|
232
249
|
let run = async () => {
|
|
@@ -341,6 +358,8 @@ ${promptText}
|
|
|
341
358
|
async function questionQuery(question, apiToken, opts) {
|
|
342
359
|
opts = opts || {};
|
|
343
360
|
|
|
361
|
+
let baseApiUrl = opts.baseApiUrl || OPENAI_API_BASE_URL;
|
|
362
|
+
|
|
344
363
|
let systemPrompt = (opts.systemPrompt || DEFAULT_SYSTEM_PROMPT).toString().trim();
|
|
345
364
|
question = (question || '').toString().trim();
|
|
346
365
|
if (!question) {
|
|
@@ -389,7 +408,7 @@ ${question}
|
|
|
389
408
|
switch (gptModel) {
|
|
390
409
|
case 'gpt-3.5-turbo-instruct':
|
|
391
410
|
{
|
|
392
|
-
openAiAPIURL = OPENAI_API_URL_INSTRUCT;
|
|
411
|
+
openAiAPIURL = getApiUrl(baseApiUrl, OPENAI_API_URL_INSTRUCT);
|
|
393
412
|
payload.prompt = `${systemPrompt}\n${prompt}`;
|
|
394
413
|
let tokens = tokenizer.encode(payload.prompt);
|
|
395
414
|
payload.max_tokens = 4000 - tokens.bpe.length;
|
|
@@ -399,7 +418,7 @@ ${question}
|
|
|
399
418
|
case 'gpt-3.5-turbo':
|
|
400
419
|
case 'gpt-4':
|
|
401
420
|
default:
|
|
402
|
-
openAiAPIURL = OPENAI_API_URL_CHAT;
|
|
421
|
+
openAiAPIURL = getApiUrl(baseApiUrl, OPENAI_API_URL_CHAT);
|
|
403
422
|
payload.messages = [
|
|
404
423
|
{
|
|
405
424
|
role: 'system',
|
|
@@ -414,7 +433,7 @@ ${question}
|
|
|
414
433
|
}
|
|
415
434
|
|
|
416
435
|
if (opts.verbose) {
|
|
417
|
-
console.error(util.inspect({ requestId, payload }, false, 8, true));
|
|
436
|
+
console.error(util.inspect({ requestId, apiUrl: openAiAPIURL, payload }, false, 8, true));
|
|
418
437
|
}
|
|
419
438
|
|
|
420
439
|
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,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@postalsys/email-ai-tools",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "Email AI tools",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
8
|
+
"update": "rm -rf node_modules package-lock.json && ncu -u && npm install"
|
|
8
9
|
},
|
|
9
10
|
"repository": {
|
|
10
11
|
"type": "git",
|
|
@@ -23,13 +24,13 @@
|
|
|
23
24
|
"mailparser": "3.6.5"
|
|
24
25
|
},
|
|
25
26
|
"dependencies": {
|
|
26
|
-
"@postalsys/email-text-tools": "2.1.
|
|
27
|
+
"@postalsys/email-text-tools": "2.1.2",
|
|
27
28
|
"gpt3-tokenizer": "1.1.5",
|
|
28
29
|
"libmime": "5.2.1",
|
|
29
30
|
"linkify-it": "4.0.1",
|
|
30
|
-
"nodemailer": "6.9.
|
|
31
|
+
"nodemailer": "6.9.7",
|
|
31
32
|
"punycode": "2.3.0",
|
|
32
|
-
"tlds": "1.
|
|
33
|
-
"undici": "5.
|
|
33
|
+
"tlds": "1.243.0",
|
|
34
|
+
"undici": "5.26.5"
|
|
34
35
|
}
|
|
35
36
|
}
|