@postalsys/email-ai-tools 1.7.2 → 1.8.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 +7 -0
- package/examples/list-models.js +14 -0
- package/index.js +2 -0
- package/lib/list-models.js +130 -0
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.8.0](https://github.com/postalsys/email-ai-tools/compare/v1.7.2...v1.8.0) (2023-11-27)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* **list-models:** new method to list available models ([7ddb033](https://github.com/postalsys/email-ai-tools/commit/7ddb0334d1cc7b702533aec004b249ef5b1654b7))
|
|
9
|
+
|
|
3
10
|
## [1.7.2](https://github.com/postalsys/email-ai-tools/compare/v1.7.1...v1.7.2) (2023-10-31)
|
|
4
11
|
|
|
5
12
|
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { listModels } = require('../lib/list-models');
|
|
4
|
+
const util = require('util');
|
|
5
|
+
|
|
6
|
+
async function main() {
|
|
7
|
+
const result = await listModels(process.env.OPENAI_API_KEY, {
|
|
8
|
+
verbose: true
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
console.log(util.inspect(result, false, 22, true));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
main();
|
package/index.js
CHANGED
|
@@ -4,6 +4,7 @@ const { generateSummary, DEFAULT_SYSTEM_PROMPT, DEFAULT_USER_PROMPT } = require(
|
|
|
4
4
|
const riskAnalysis = require('./lib/risk-analysis');
|
|
5
5
|
const { generateEmbeddings, getChunkEmbeddings } = require('./lib/generate-embeddings');
|
|
6
6
|
const { embeddingsQuery, questionQuery } = require('./lib/embeddings-query');
|
|
7
|
+
const { listModels } = require('./lib/list-models');
|
|
7
8
|
|
|
8
9
|
module.exports = {
|
|
9
10
|
generateSummary,
|
|
@@ -12,6 +13,7 @@ module.exports = {
|
|
|
12
13
|
embeddingsQuery,
|
|
13
14
|
questionQuery,
|
|
14
15
|
riskAnalysis,
|
|
16
|
+
listModels,
|
|
15
17
|
DEFAULT_SYSTEM_PROMPT,
|
|
16
18
|
DEFAULT_USER_PROMPT
|
|
17
19
|
};
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const packageData = require('../package.json');
|
|
4
|
+
const { fetch: fetchCmd, Agent } = require('undici');
|
|
5
|
+
const fetchAgent = new Agent({ connect: { timeout: 90 * 1000 } });
|
|
6
|
+
const util = require('util');
|
|
7
|
+
const crypto = require('crypto');
|
|
8
|
+
|
|
9
|
+
const OPENAI_API_BASE_URL = 'https://api.openai.com';
|
|
10
|
+
const OPENAI_API_MODELS = '/v1/models';
|
|
11
|
+
|
|
12
|
+
function getApiUrl(baseApiUrl, path) {
|
|
13
|
+
let url = new URL(path || '/', baseApiUrl || OPENAI_API_BASE_URL);
|
|
14
|
+
return url.href;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async function listModels(apiToken, opts) {
|
|
18
|
+
opts = opts || {};
|
|
19
|
+
|
|
20
|
+
let baseApiUrl = opts.baseApiUrl || OPENAI_API_BASE_URL;
|
|
21
|
+
|
|
22
|
+
let openAiAPIURL = getApiUrl(baseApiUrl, OPENAI_API_MODELS);
|
|
23
|
+
|
|
24
|
+
let headers = {
|
|
25
|
+
'User-Agent': `${packageData.name}/${packageData.version}`,
|
|
26
|
+
Authorization: `Bearer ${apiToken}`,
|
|
27
|
+
'Content-Type': 'application/json'
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const requestId = crypto.randomBytes(8).toString('base64');
|
|
31
|
+
|
|
32
|
+
let res;
|
|
33
|
+
let data;
|
|
34
|
+
let retries = 0;
|
|
35
|
+
|
|
36
|
+
if (opts.verbose) {
|
|
37
|
+
console.error(util.inspect({ requestId, apiUrl: openAiAPIURL }, false, 8, true));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let run = async () => {
|
|
41
|
+
res = await fetchCmd(openAiAPIURL, {
|
|
42
|
+
method: 'get',
|
|
43
|
+
headers,
|
|
44
|
+
dispatcher: fetchAgent
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
data = await res.json();
|
|
48
|
+
|
|
49
|
+
if (!res.ok) {
|
|
50
|
+
if (res.status === 429 && ++retries < 5) {
|
|
51
|
+
// try again
|
|
52
|
+
await new Promise(r => setTimeout(r, 1000));
|
|
53
|
+
return await run();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (data && data.error) {
|
|
57
|
+
let error = new Error(data.error.message || data.error);
|
|
58
|
+
if (data.error.code) {
|
|
59
|
+
error.code = data.error.code;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
error.statusCode = res.status;
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
let error = new Error('Failed to run API request');
|
|
67
|
+
error.statusCode = res.status;
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (!data) {
|
|
72
|
+
throw new Error(`Failed to POST API request`);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const reqStartTime = Date.now();
|
|
77
|
+
await run();
|
|
78
|
+
const reqEndTime = Date.now();
|
|
79
|
+
|
|
80
|
+
const response = { models: [].concat((data && data.data) || []).filter(entry => !['openai-dev'].includes(entry.owned_by)) };
|
|
81
|
+
|
|
82
|
+
if (opts.verbose) {
|
|
83
|
+
response._time = reqEndTime - reqStartTime;
|
|
84
|
+
|
|
85
|
+
console.error(util.inspect({ requestId, response }, false, 8, true));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
response.models.sort((a, b) => {
|
|
89
|
+
if (/^gpt/.test(a.id) && !/^gpt/.test(b.id)) {
|
|
90
|
+
return -1;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (/^gpt/.test(b.id) && !/^gpt/.test(a.id)) {
|
|
94
|
+
return 1;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (/-\d{4,}$/.test(b.id) && !/-\d{4,}$/.test(a.id)) {
|
|
98
|
+
return -1;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (/-\d{4,}$/.test(a.id) && !/-\d{4,}$/.test(b.id)) {
|
|
102
|
+
return 1;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (/-preview/.test(b.id) && !/-preview/.test(a.id)) {
|
|
106
|
+
return -1;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (/-preview/.test(a.id) && !/-preview/.test(b.id)) {
|
|
110
|
+
return 1;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return a.id.localeCompare(b.id);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
response.models.forEach(entry => {
|
|
117
|
+
entry.name = entry.id
|
|
118
|
+
.replace(/-/g, ' ')
|
|
119
|
+
.replace(/^.| ./g, c => c.toUpperCase())
|
|
120
|
+
.replace(/\bhd\b/gi, c => c.toUpperCase())
|
|
121
|
+
.replace(/\b\d+k\b/gi, c => c.toUpperCase())
|
|
122
|
+
.replace(/Dall E/g, 'Dall-E')
|
|
123
|
+
.replace(/^Whisper /g, 'Whisper-')
|
|
124
|
+
.replace(/^(gpt|tts)\s/gi, (o, n) => `${n.toUpperCase()}-`);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
return response;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
module.exports = { listModels };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@postalsys/email-ai-tools",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"description": "Email AI tools",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"linkify-it": "4.0.1",
|
|
31
31
|
"nodemailer": "6.9.7",
|
|
32
32
|
"punycode": "2.3.1",
|
|
33
|
-
"tlds": "1.
|
|
34
|
-
"undici": "5.
|
|
33
|
+
"tlds": "1.247.0",
|
|
34
|
+
"undici": "5.28.1"
|
|
35
35
|
}
|
|
36
36
|
}
|