@postalsys/email-ai-tools 1.6.3 → 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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
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
+
3
15
  ## [1.6.3](https://github.com/postalsys/email-ai-tools/compare/v1.6.2...v1.6.3) (2023-10-20)
4
16
 
5
17
 
@@ -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
- const OPENAI_API_URL_CHAT = 'https://api.openai.com/v1/chat/completions';
15
- const OPENAI_API_URL_INSTRUCT = 'https://api.openai.com/v1/completions';
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.
@@ -117,9 +120,16 @@ You are analyzing user questions regarding email retrieval from a database. From
117
120
  Process the user question:
118
121
  `.trim();
119
122
 
123
+ function getApiUrl(baseApiUrl, path) {
124
+ let url = new URL(path || '/', baseApiUrl || OPENAI_API_BASE_URL);
125
+ return url.href;
126
+ }
127
+
120
128
  async function embeddingsQuery(apiToken, opts) {
121
129
  opts = opts || {};
122
130
 
131
+ let baseApiUrl = opts.baseApiUrl || OPENAI_API_BASE_URL;
132
+
123
133
  let systemPrompt = (opts.systemPrompt || DEFAULT_SYSTEM_PROMPT).toString().trim();
124
134
  let question = (opts.question || '').toString().trim();
125
135
  let contextChunks = (opts.contextChunks || '').toString().trim();
@@ -208,7 +218,7 @@ ${promptText}
208
218
  switch (gptModel) {
209
219
  case 'gpt-3.5-turbo-instruct':
210
220
  {
211
- openAiAPIURL = OPENAI_API_URL_INSTRUCT;
221
+ openAiAPIURL = getApiUrl(baseApiUrl, OPENAI_API_URL_INSTRUCT);
212
222
  payload.prompt = `${systemPrompt}\n${prompt}`;
213
223
  let tokens = tokenizer.encode(payload.prompt);
214
224
  payload.max_tokens = 4000 - tokens.bpe.length;
@@ -218,7 +228,7 @@ ${promptText}
218
228
  case 'gpt-3.5-turbo':
219
229
  case 'gpt-4':
220
230
  default:
221
- openAiAPIURL = OPENAI_API_URL_CHAT;
231
+ openAiAPIURL = getApiUrl(baseApiUrl, OPENAI_API_URL_CHAT);
222
232
  payload.messages = [
223
233
  {
224
234
  role: 'system',
@@ -233,7 +243,7 @@ ${promptText}
233
243
  }
234
244
 
235
245
  if (opts.verbose) {
236
- console.error(util.inspect({ requestId, payload }, false, 8, true));
246
+ console.error(util.inspect({ requestId, apiUrl: openAiAPIURL, payload }, false, 8, true));
237
247
  }
238
248
 
239
249
  let run = async () => {
@@ -348,6 +358,8 @@ ${promptText}
348
358
  async function questionQuery(question, apiToken, opts) {
349
359
  opts = opts || {};
350
360
 
361
+ let baseApiUrl = opts.baseApiUrl || OPENAI_API_BASE_URL;
362
+
351
363
  let systemPrompt = (opts.systemPrompt || DEFAULT_SYSTEM_PROMPT).toString().trim();
352
364
  question = (question || '').toString().trim();
353
365
  if (!question) {
@@ -396,7 +408,7 @@ ${question}
396
408
  switch (gptModel) {
397
409
  case 'gpt-3.5-turbo-instruct':
398
410
  {
399
- openAiAPIURL = OPENAI_API_URL_INSTRUCT;
411
+ openAiAPIURL = getApiUrl(baseApiUrl, OPENAI_API_URL_INSTRUCT);
400
412
  payload.prompt = `${systemPrompt}\n${prompt}`;
401
413
  let tokens = tokenizer.encode(payload.prompt);
402
414
  payload.max_tokens = 4000 - tokens.bpe.length;
@@ -406,7 +418,7 @@ ${question}
406
418
  case 'gpt-3.5-turbo':
407
419
  case 'gpt-4':
408
420
  default:
409
- openAiAPIURL = OPENAI_API_URL_CHAT;
421
+ openAiAPIURL = getApiUrl(baseApiUrl, OPENAI_API_URL_CHAT);
410
422
  payload.messages = [
411
423
  {
412
424
  role: 'system',
@@ -421,7 +433,7 @@ ${question}
421
433
  }
422
434
 
423
435
  if (opts.verbose) {
424
- console.error(util.inspect({ requestId, payload }, false, 8, true));
436
+ console.error(util.inspect({ requestId, apiUrl: openAiAPIURL, payload }, false, 8, true));
425
437
  }
426
438
 
427
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 OPENAI_API_URL = 'https://api.openai.com/v1/embeddings';
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(OPENAI_API_URL, {
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;
@@ -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
- const OPENAI_API_URL_CHAT = 'https://api.openai.com/v1/chat/completions';
16
- const OPENAI_API_URL_INSTRUCT = 'https://api.openai.com/v1/completions';
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 {
@@ -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
- const OPENAI_API_URL = 'https://api.openai.com/v1/chat/completions';
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.6.3",
3
+ "version": "1.7.0",
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.6",
31
+ "nodemailer": "6.9.7",
32
32
  "punycode": "2.3.0",
33
33
  "tlds": "1.243.0",
34
- "undici": "5.26.4"
34
+ "undici": "5.26.5"
35
35
  }
36
36
  }