@postalsys/email-ai-tools 1.3.4 → 1.4.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 +14 -0
- package/examples/emeddings.js +5 -0
- package/lib/generate-embeddings.js +7 -1
- package/lib/generate-summary.js +37 -17
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.4.1](https://github.com/postalsys/email-ai-tools/compare/v1.4.0...v1.4.1) (2023-09-23)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* **embeddings:** remove quoted lines ([fb5dd92](https://github.com/postalsys/email-ai-tools/commit/fb5dd92b834249a0290113df4c42b2f01eb22e84))
|
|
9
|
+
|
|
10
|
+
## [1.4.0](https://github.com/postalsys/email-ai-tools/compare/v1.3.4...v1.4.0) (2023-09-20)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
* **models:** Support GTP-3 Instruct ([6a15fc4](https://github.com/postalsys/email-ai-tools/commit/6a15fc4fdda175485fc22b2bc5fa68a281ee65f5))
|
|
16
|
+
|
|
3
17
|
## [1.3.4](https://github.com/postalsys/email-ai-tools/compare/v1.3.3...v1.3.4) (2023-09-19)
|
|
4
18
|
|
|
5
19
|
|
package/examples/emeddings.js
CHANGED
|
@@ -151,7 +151,13 @@ class Embedder {
|
|
|
151
151
|
|
|
152
152
|
let textChunks = [];
|
|
153
153
|
if (this.text) {
|
|
154
|
-
let
|
|
154
|
+
let preparedText = this.text
|
|
155
|
+
.replace(/\r?\n/g, '\n')
|
|
156
|
+
.replace(/^\s*>.*$/gm, '')
|
|
157
|
+
.replace(/^\s*>$/gm)
|
|
158
|
+
.replace(/\n\n+/g, '\n\n');
|
|
159
|
+
|
|
160
|
+
let textTokens = tokenizer.encode(preparedText);
|
|
155
161
|
|
|
156
162
|
let pos = 0;
|
|
157
163
|
while (pos < textTokens.bpe.length) {
|
package/lib/generate-summary.js
CHANGED
|
@@ -11,7 +11,8 @@ 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
|
|
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';
|
|
15
16
|
|
|
16
17
|
const DEFAULT_ALLOWED_HEADERS = ['from', 'to', 'cc', 'bcc', 'subject', 'mime-version', 'authentication-results', 'date', 'content-type', 'list-id'];
|
|
17
18
|
|
|
@@ -181,7 +182,7 @@ ${SCHEMA_PROMPT}
|
|
|
181
182
|
${JSON.stringify(content)}`;
|
|
182
183
|
|
|
183
184
|
let tokens = tokenizer.encode(prompt);
|
|
184
|
-
if (tokens.
|
|
185
|
+
if (tokens.bpe.length <= maxAllowedTokens) {
|
|
185
186
|
break;
|
|
186
187
|
}
|
|
187
188
|
if (promptText.length > 2 * 1024 * 1024) {
|
|
@@ -214,17 +215,7 @@ ${JSON.stringify(content)}`;
|
|
|
214
215
|
};
|
|
215
216
|
|
|
216
217
|
let payload = {
|
|
217
|
-
model: gptModel
|
|
218
|
-
messages: [
|
|
219
|
-
{
|
|
220
|
-
role: 'system',
|
|
221
|
-
content: `${systemPrompt}`
|
|
222
|
-
},
|
|
223
|
-
{
|
|
224
|
-
role: 'user',
|
|
225
|
-
content: prompt
|
|
226
|
-
}
|
|
227
|
-
]
|
|
218
|
+
model: gptModel
|
|
228
219
|
};
|
|
229
220
|
|
|
230
221
|
if (opts.temperature && !isNaN(opts.temperature)) {
|
|
@@ -239,12 +230,40 @@ ${JSON.stringify(content)}`;
|
|
|
239
230
|
let data;
|
|
240
231
|
let retries = 0;
|
|
241
232
|
|
|
233
|
+
let openAiAPIURL;
|
|
234
|
+
switch (gptModel) {
|
|
235
|
+
case 'gpt-3.5-turbo-instruct':
|
|
236
|
+
{
|
|
237
|
+
openAiAPIURL = OPENAI_API_URL_INSTRUCT;
|
|
238
|
+
payload.prompt = `${systemPrompt}\n${prompt}`;
|
|
239
|
+
let tokens = tokenizer.encode(payload.prompt);
|
|
240
|
+
payload.max_tokens = 4000 - tokens.bpe.length;
|
|
241
|
+
}
|
|
242
|
+
break;
|
|
243
|
+
|
|
244
|
+
case 'gpt-3.5-turbo':
|
|
245
|
+
case 'gpt-4':
|
|
246
|
+
default:
|
|
247
|
+
openAiAPIURL = OPENAI_API_URL_CHAT;
|
|
248
|
+
payload.messages = [
|
|
249
|
+
{
|
|
250
|
+
role: 'system',
|
|
251
|
+
content: `${systemPrompt}`
|
|
252
|
+
},
|
|
253
|
+
{
|
|
254
|
+
role: 'user',
|
|
255
|
+
content: prompt
|
|
256
|
+
}
|
|
257
|
+
];
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
|
|
242
261
|
if (opts.verbose) {
|
|
243
262
|
console.log(util.inspect(payload, false, 5, true));
|
|
244
263
|
}
|
|
245
264
|
|
|
246
265
|
let run = async () => {
|
|
247
|
-
res = await fetchCmd(
|
|
266
|
+
res = await fetchCmd(openAiAPIURL, {
|
|
248
267
|
method: 'post',
|
|
249
268
|
headers,
|
|
250
269
|
body: JSON.stringify(payload),
|
|
@@ -252,6 +271,7 @@ ${JSON.stringify(content)}`;
|
|
|
252
271
|
});
|
|
253
272
|
|
|
254
273
|
data = await res.json();
|
|
274
|
+
|
|
255
275
|
if (!res.ok) {
|
|
256
276
|
if (res.status === 429 && ++retries < 5) {
|
|
257
277
|
// try again
|
|
@@ -288,10 +308,10 @@ ${JSON.stringify(content)}`;
|
|
|
288
308
|
data &&
|
|
289
309
|
data.choices &&
|
|
290
310
|
data.choices
|
|
291
|
-
.filter(msg => msg && msg.message && msg.message.role === 'assistant' && msg.message.content)
|
|
311
|
+
.filter(msg => msg && ((msg.message && msg.message.role === 'assistant' && msg.message.content) || msg.text))
|
|
292
312
|
.sort((a, b) => ((a && a.index) || 0) - ((b && b.index) || 0))
|
|
293
|
-
.map(msg => msg.message.content)
|
|
294
|
-
.join('
|
|
313
|
+
.map(msg => (msg.message && msg.message.content) || msg.text)
|
|
314
|
+
.join('')
|
|
295
315
|
.trim();
|
|
296
316
|
|
|
297
317
|
try {
|