@postalsys/email-ai-tools 1.14.13 → 2.0.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/.ncurc.js +1 -6
- package/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +18 -0
- package/CLAUDE.md +19 -13
- package/README.md +120 -1
- package/examples/summary.js +6 -5
- package/index.js +4 -10
- package/lib/api-request.js +7 -0
- package/lib/generate-summary.js +445 -296
- package/lib/list-models.js +82 -46
- package/lib/token-estimate.js +26 -81
- package/package.json +3 -7
- package/test/dispatcher.test.js +2 -46
- package/test/generate-summary.test.js +357 -153
- package/test/helpers/mock-server.js +10 -35
- package/test/index.test.js +23 -23
- package/test/list-models.test.js +75 -4
- package/test/token-estimate.test.js +26 -63
- package/examples/emeddings.js +0 -38
- package/examples/question.js +0 -18
- package/examples/risk.js +0 -86
- package/lib/embeddings-query.js +0 -400
- package/lib/generate-embeddings.js +0 -346
- package/lib/risk-analysis.js +0 -195
- package/test/embeddings-query.test.js +0 -304
- package/test/generate-embeddings.test.js +0 -233
- package/test/risk-analysis.test.js +0 -211
|
@@ -64,47 +64,23 @@ async function createMockServer() {
|
|
|
64
64
|
|
|
65
65
|
function chatResponse(content, opts) {
|
|
66
66
|
opts = opts || {};
|
|
67
|
+
const total = opts.tokens || 150;
|
|
67
68
|
return {
|
|
68
69
|
status: 200,
|
|
69
70
|
body: {
|
|
70
71
|
id: opts.id || 'chatcmpl-test123',
|
|
72
|
+
model: opts.model || 'test-model-2026-01-01',
|
|
71
73
|
choices: [
|
|
72
74
|
{
|
|
73
75
|
index: 0,
|
|
76
|
+
finish_reason: opts.finishReason || 'stop',
|
|
74
77
|
message: {
|
|
75
78
|
role: 'assistant',
|
|
76
79
|
content: typeof content === 'string' ? content : JSON.stringify(content)
|
|
77
80
|
}
|
|
78
81
|
}
|
|
79
82
|
],
|
|
80
|
-
usage: {
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function instructResponse(content, opts) {
|
|
86
|
-
opts = opts || {};
|
|
87
|
-
return {
|
|
88
|
-
status: 200,
|
|
89
|
-
body: {
|
|
90
|
-
id: opts.id || 'cmpl-test123',
|
|
91
|
-
choices: [
|
|
92
|
-
{
|
|
93
|
-
index: 0,
|
|
94
|
-
text: typeof content === 'string' ? content : JSON.stringify(content)
|
|
95
|
-
}
|
|
96
|
-
],
|
|
97
|
-
usage: { total_tokens: opts.tokens || 100 }
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function embeddingResponse(embedding) {
|
|
103
|
-
return {
|
|
104
|
-
status: 200,
|
|
105
|
-
body: {
|
|
106
|
-
data: [{ embedding: embedding || [0.1, 0.2, 0.3] }],
|
|
107
|
-
usage: { total_tokens: 50 }
|
|
83
|
+
usage: { prompt_tokens: total - 30, completion_tokens: 30, total_tokens: total }
|
|
108
84
|
}
|
|
109
85
|
};
|
|
110
86
|
}
|
|
@@ -114,21 +90,22 @@ function modelsResponse(models) {
|
|
|
114
90
|
status: 200,
|
|
115
91
|
body: {
|
|
116
92
|
data: models || [
|
|
117
|
-
{ id: 'gpt-
|
|
118
|
-
{ id: 'gpt-
|
|
119
|
-
{ id: 'text-embedding-
|
|
93
|
+
{ id: 'gpt-6-luna', owned_by: 'system' },
|
|
94
|
+
{ id: 'gpt-5-mini', owned_by: 'system' },
|
|
95
|
+
{ id: 'text-embedding-3-small', owned_by: 'system' }
|
|
120
96
|
]
|
|
121
97
|
}
|
|
122
98
|
};
|
|
123
99
|
}
|
|
124
100
|
|
|
125
|
-
function errorResponse(status, message, code) {
|
|
101
|
+
function errorResponse(status, message, code, param) {
|
|
126
102
|
return {
|
|
127
103
|
status,
|
|
128
104
|
body: {
|
|
129
105
|
error: {
|
|
130
106
|
message,
|
|
131
|
-
code
|
|
107
|
+
code,
|
|
108
|
+
param
|
|
132
109
|
}
|
|
133
110
|
}
|
|
134
111
|
};
|
|
@@ -137,8 +114,6 @@ function errorResponse(status, message, code) {
|
|
|
137
114
|
module.exports = {
|
|
138
115
|
createMockServer,
|
|
139
116
|
chatResponse,
|
|
140
|
-
instructResponse,
|
|
141
|
-
embeddingResponse,
|
|
142
117
|
modelsResponse,
|
|
143
118
|
errorResponse
|
|
144
119
|
};
|
package/test/index.test.js
CHANGED
|
@@ -9,37 +9,37 @@ describe('index exports', () => {
|
|
|
9
9
|
assert.equal(typeof lib.generateSummary, 'function');
|
|
10
10
|
});
|
|
11
11
|
|
|
12
|
-
it('exports generateEmbeddings as a function', () => {
|
|
13
|
-
assert.equal(typeof lib.generateEmbeddings, 'function');
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it('exports getChunkEmbeddings as a function', () => {
|
|
17
|
-
assert.equal(typeof lib.getChunkEmbeddings, 'function');
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it('exports embeddingsQuery as a function', () => {
|
|
21
|
-
assert.equal(typeof lib.embeddingsQuery, 'function');
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it('exports questionQuery as a function', () => {
|
|
25
|
-
assert.equal(typeof lib.questionQuery, 'function');
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it('exports riskAnalysis as a function', () => {
|
|
29
|
-
assert.equal(typeof lib.riskAnalysis, 'function');
|
|
30
|
-
});
|
|
31
|
-
|
|
32
12
|
it('exports listModels as a function', () => {
|
|
33
13
|
assert.equal(typeof lib.listModels, 'function');
|
|
34
14
|
});
|
|
35
15
|
|
|
16
|
+
it('exports the default model and reasoning effort', () => {
|
|
17
|
+
assert.equal(typeof lib.DEFAULT_MODEL, 'string');
|
|
18
|
+
assert.ok(lib.DEFAULT_MODEL.length > 0);
|
|
19
|
+
assert.equal(lib.DEFAULT_REASONING_EFFORT, 'low');
|
|
20
|
+
});
|
|
21
|
+
|
|
36
22
|
it('exports DEFAULT_SYSTEM_PROMPT as a non-empty string', () => {
|
|
37
23
|
assert.equal(typeof lib.DEFAULT_SYSTEM_PROMPT, 'string');
|
|
38
24
|
assert.ok(lib.DEFAULT_SYSTEM_PROMPT.length > 0);
|
|
39
25
|
});
|
|
40
26
|
|
|
41
|
-
it('exports
|
|
42
|
-
assert.equal(typeof lib.
|
|
43
|
-
assert.ok(lib.
|
|
27
|
+
it('exports DEFAULT_INSTRUCTIONS as a non-empty string', () => {
|
|
28
|
+
assert.equal(typeof lib.DEFAULT_INSTRUCTIONS, 'string');
|
|
29
|
+
assert.ok(lib.DEFAULT_INSTRUCTIONS.length > 0);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('no longer exports the removed modules', () => {
|
|
33
|
+
for (const name of [
|
|
34
|
+
'riskAnalysis',
|
|
35
|
+
'generateEmbeddings',
|
|
36
|
+
'getChunkEmbeddings',
|
|
37
|
+
'embeddingsQuery',
|
|
38
|
+
'questionQuery',
|
|
39
|
+
'DEFAULT_USER_PROMPT',
|
|
40
|
+
'resetParameterCache'
|
|
41
|
+
]) {
|
|
42
|
+
assert.ok(!(name in lib), name);
|
|
43
|
+
}
|
|
44
44
|
});
|
|
45
45
|
});
|
package/test/list-models.test.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
const { describe, it, before, after, beforeEach } = require('node:test');
|
|
4
4
|
const assert = require('node:assert/strict');
|
|
5
5
|
const { createMockServer, modelsResponse, errorResponse } = require('./helpers/mock-server');
|
|
6
|
-
const { listModels } = require('../lib/list-models');
|
|
6
|
+
const { listModels, nonChatModelPattern } = require('../lib/list-models');
|
|
7
7
|
|
|
8
8
|
describe('listModels', () => {
|
|
9
9
|
let mock;
|
|
@@ -64,6 +64,77 @@ describe('listModels', () => {
|
|
|
64
64
|
assert.ok(result.models.every(m => m.owned_by !== 'openai-dev'));
|
|
65
65
|
});
|
|
66
66
|
|
|
67
|
+
it('leaves out the unmistakable non-chat models of a compatible endpoint unless asked', async () => {
|
|
68
|
+
const models = [
|
|
69
|
+
{ id: 'gpt-6-luna', owned_by: 'system' },
|
|
70
|
+
{ id: 'nomic-embed-text', owned_by: 'library' },
|
|
71
|
+
{ id: 'text-embedding-3-small', owned_by: 'system' },
|
|
72
|
+
{ id: 'whisper-1', owned_by: 'system' },
|
|
73
|
+
{ id: 'Llama-3.1-8B-Instruct', owned_by: 'library' },
|
|
74
|
+
{ id: 'llama3.2', owned_by: 'library' }
|
|
75
|
+
];
|
|
76
|
+
mock.setHandler(() => modelsResponse(models));
|
|
77
|
+
|
|
78
|
+
const result = await listModels('test-token', { baseApiUrl: mock.url });
|
|
79
|
+
assert.deepEqual(
|
|
80
|
+
result.models.map(m => m.id),
|
|
81
|
+
['gpt-6-luna', 'Llama-3.1-8B-Instruct', 'llama3.2']
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
const all = await listModels('test-token', { baseApiUrl: mock.url, chatOnly: false });
|
|
85
|
+
assert.equal(all.models.length, models.length);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('knows which of the names on the OpenAI endpoint are not chat models', () => {
|
|
89
|
+
const pattern = nonChatModelPattern();
|
|
90
|
+
for (const id of [
|
|
91
|
+
'text-embedding-3-small',
|
|
92
|
+
'gpt-5-search-api',
|
|
93
|
+
'gpt-5.3-codex',
|
|
94
|
+
'gpt-3.5-turbo-instruct',
|
|
95
|
+
'gpt-realtime-2.1',
|
|
96
|
+
'gpt-live-1',
|
|
97
|
+
'gpt-image-2',
|
|
98
|
+
'gpt-5.6-cyber',
|
|
99
|
+
'whisper-1',
|
|
100
|
+
'tts-1',
|
|
101
|
+
'omni-moderation-latest',
|
|
102
|
+
'sora-2'
|
|
103
|
+
]) {
|
|
104
|
+
assert.ok(pattern.test(id), id);
|
|
105
|
+
}
|
|
106
|
+
for (const id of ['gpt-6-luna', 'gpt-6-astra', 'gpt-5.4-mini', 'gpt-5-nano', 'gpt-4.1', 'o4-mini', 'chat-latest']) {
|
|
107
|
+
assert.ok(!pattern.test(id), id);
|
|
108
|
+
}
|
|
109
|
+
assert.equal(nonChatModelPattern('https://api.openai.com/v1'), pattern);
|
|
110
|
+
assert.notEqual(nonChatModelPattern('https://resource.openai.azure.com/openai/v1'), pattern);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('sorts the newest family first and names it', async () => {
|
|
114
|
+
mock.setHandler(() =>
|
|
115
|
+
modelsResponse([
|
|
116
|
+
{ id: 'gpt-4.1-mini', owned_by: 'system' },
|
|
117
|
+
{ id: 'o4-mini', owned_by: 'system' },
|
|
118
|
+
{ id: 'gpt-5-mini', owned_by: 'system' },
|
|
119
|
+
{ id: 'gpt-5', owned_by: 'system' },
|
|
120
|
+
{ id: 'gpt-5.4-nano', owned_by: 'system' },
|
|
121
|
+
{ id: 'gpt-6-luna', owned_by: 'system' },
|
|
122
|
+
{ id: 'gpt-6-astra', owned_by: 'system' },
|
|
123
|
+
{ id: 'gpt-10', owned_by: 'system' },
|
|
124
|
+
{ id: 'mistral-nemo', owned_by: 'library' }
|
|
125
|
+
])
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
const result = await listModels('test-token', { baseApiUrl: mock.url });
|
|
129
|
+
|
|
130
|
+
assert.deepEqual(
|
|
131
|
+
result.models.map(m => m.id),
|
|
132
|
+
['gpt-10', 'gpt-6-astra', 'gpt-6-luna', 'gpt-5.4-nano', 'gpt-5', 'gpt-5-mini', 'gpt-4.1-mini', 'o4-mini', 'mistral-nemo']
|
|
133
|
+
);
|
|
134
|
+
assert.equal(result.models[1].name, 'GPT-6 Astra');
|
|
135
|
+
assert.equal(result.models[3].name, 'GPT-5.4 Nano');
|
|
136
|
+
});
|
|
137
|
+
|
|
67
138
|
it('sorts GPT models first', async () => {
|
|
68
139
|
mock.setHandler(() =>
|
|
69
140
|
modelsResponse([
|
|
@@ -131,7 +202,7 @@ describe('listModels', () => {
|
|
|
131
202
|
it('formats TTS prefix to uppercase', async () => {
|
|
132
203
|
mock.setHandler(() => modelsResponse([{ id: 'tts-1', owned_by: 'openai' }]));
|
|
133
204
|
|
|
134
|
-
const result = await listModels('test-token', { baseApiUrl: mock.url });
|
|
205
|
+
const result = await listModels('test-token', { baseApiUrl: mock.url, chatOnly: false });
|
|
135
206
|
|
|
136
207
|
assert.ok(result.models[0].name.startsWith('TTS-'));
|
|
137
208
|
});
|
|
@@ -139,7 +210,7 @@ describe('listModels', () => {
|
|
|
139
210
|
it('formats Dall-E name correctly', async () => {
|
|
140
211
|
mock.setHandler(() => modelsResponse([{ id: 'dall-e-3', owned_by: 'openai' }]));
|
|
141
212
|
|
|
142
|
-
const result = await listModels('test-token', { baseApiUrl: mock.url });
|
|
213
|
+
const result = await listModels('test-token', { baseApiUrl: mock.url, chatOnly: false });
|
|
143
214
|
|
|
144
215
|
assert.ok(result.models[0].name.includes('Dall-E'));
|
|
145
216
|
});
|
|
@@ -155,7 +226,7 @@ describe('listModels', () => {
|
|
|
155
226
|
it('formats HD abbreviation to uppercase', async () => {
|
|
156
227
|
mock.setHandler(() => modelsResponse([{ id: 'dall-e-3-hd', owned_by: 'openai' }]));
|
|
157
228
|
|
|
158
|
-
const result = await listModels('test-token', { baseApiUrl: mock.url });
|
|
229
|
+
const result = await listModels('test-token', { baseApiUrl: mock.url, chatOnly: false });
|
|
159
230
|
|
|
160
231
|
assert.ok(result.models[0].name.includes('HD'));
|
|
161
232
|
});
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
const { describe, it, before, after, beforeEach } = require('node:test');
|
|
4
4
|
const assert = require('node:assert/strict');
|
|
5
5
|
const { createMockServer, chatResponse } = require('./helpers/mock-server');
|
|
6
|
-
const { estimateTokens, fitLength, fitPrompt
|
|
7
|
-
const { generateSummary
|
|
6
|
+
const { estimateTokens, fitLength, fitPrompt } = require('../lib/token-estimate');
|
|
7
|
+
const { generateSummary } = require('..');
|
|
8
8
|
|
|
9
9
|
// 64 KB of CJK text with no whitespace or punctuation, the input that made the old tokenizer
|
|
10
10
|
// loop take 24 s per encode
|
|
@@ -47,25 +47,6 @@ describe('token estimate', () => {
|
|
|
47
47
|
}
|
|
48
48
|
});
|
|
49
49
|
|
|
50
|
-
it('splitByTokens keeps characters whole and loses nothing', () => {
|
|
51
|
-
const text = cjk(3000) + ' ' + 'word '.repeat(2000) + '\u{1F600}'.repeat(500);
|
|
52
|
-
const chunks = splitByTokens(text, 100);
|
|
53
|
-
assert.ok(chunks.length > 1);
|
|
54
|
-
assert.equal(chunks.join(''), text);
|
|
55
|
-
for (const chunk of chunks) {
|
|
56
|
-
assert.ok(!chunk.includes('\ufffd'));
|
|
57
|
-
assert.ok(!/^[\udc00-\udfff]/.test(chunk), 'chunk starts with a low surrogate');
|
|
58
|
-
assert.ok(!/[\ud800-\udbff]$/.test(chunk), 'chunk ends with a high surrogate');
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it('splitByTokens breaks ASCII text at whitespace', () => {
|
|
63
|
-
const chunks = splitByTokens('word '.repeat(2000), 100);
|
|
64
|
-
for (const chunk of chunks.slice(0, -1)) {
|
|
65
|
-
assert.ok(/ $/.test(chunk), JSON.stringify(chunk.slice(-10)));
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
|
|
69
50
|
it('fits a 64 KB CJK body in well under 100 ms', () => {
|
|
70
51
|
const text = cjk(64 * 1024);
|
|
71
52
|
const ms = timeIt(() => fitLength(text, 18000), 1);
|
|
@@ -99,11 +80,10 @@ describe('prompt fitting', () => {
|
|
|
99
80
|
});
|
|
100
81
|
|
|
101
82
|
it('fitPrompt cuts only the text and reports what it removed', () => {
|
|
102
|
-
const
|
|
103
|
-
const result = fitPrompt({ text: 'word '.repeat(1000), maxTokens: 100,
|
|
83
|
+
const overhead = 'Prefix\n';
|
|
84
|
+
const result = fitPrompt({ text: 'word '.repeat(1000), maxTokens: 100, overhead });
|
|
104
85
|
|
|
105
|
-
assert.ok(estimateTokens(result.
|
|
106
|
-
assert.equal(result.prompt, build(result.text));
|
|
86
|
+
assert.ok(estimateTokens(overhead) + estimateTokens(result.text) <= 100);
|
|
107
87
|
assert.ok('word '.repeat(1000).startsWith(result.text));
|
|
108
88
|
assert.equal(result.charactersRemoved, 5000 - result.text.length);
|
|
109
89
|
assert.ok(result.charactersRemoved > 0);
|
|
@@ -111,15 +91,19 @@ describe('prompt fitting', () => {
|
|
|
111
91
|
|
|
112
92
|
it('fitPrompt applies the length cap without splitting a surrogate pair', () => {
|
|
113
93
|
const text = 'a'.repeat(9) + '\ud83d\ude00' + 'b'.repeat(10);
|
|
114
|
-
const result = fitPrompt({ text, maxLength: 10, maxTokens: 1000
|
|
94
|
+
const result = fitPrompt({ text, maxLength: 10, maxTokens: 1000 });
|
|
115
95
|
|
|
116
96
|
assert.equal(result.text, 'a'.repeat(9));
|
|
117
97
|
assert.equal(result.charactersRemoved, text.length - 9);
|
|
118
98
|
});
|
|
119
99
|
|
|
120
|
-
it('fitPrompt
|
|
100
|
+
it('fitPrompt takes the overhead as strings or token counts already estimated', () => {
|
|
101
|
+
const fitted = fitPrompt({ text: 'hello world, hello world', maxTokens: 6, overhead: [2, 'xx'] });
|
|
102
|
+
assert.ok(estimateTokens(fitted.text) <= 3);
|
|
103
|
+
assert.ok(fitted.charactersRemoved > 0);
|
|
104
|
+
|
|
121
105
|
assert.throws(
|
|
122
|
-
() => fitPrompt({ text: 'hello', maxTokens: 5,
|
|
106
|
+
() => fitPrompt({ text: 'hello', maxTokens: 5, overhead: 'x'.repeat(100) }),
|
|
123
107
|
err => {
|
|
124
108
|
assert.equal(err.code, 'PROMPT_TOO_LONG');
|
|
125
109
|
assert.equal(err.originalLength, 5);
|
|
@@ -127,6 +111,7 @@ describe('prompt fitting', () => {
|
|
|
127
111
|
return true;
|
|
128
112
|
}
|
|
129
113
|
);
|
|
114
|
+
assert.throws(() => fitPrompt({ text: 'hello', maxTokens: 5, overhead: [3, 'xxxxxx'] }), { code: 'PROMPT_TOO_LONG' });
|
|
130
115
|
});
|
|
131
116
|
|
|
132
117
|
it('generateSummary prepares a 64 KB CJK body in one step and fits the limit', { timeout: 5000 }, async () => {
|
|
@@ -134,22 +119,27 @@ describe('prompt fitting', () => {
|
|
|
134
119
|
|
|
135
120
|
const text = cjk(64 * 1024);
|
|
136
121
|
// the old loop took minutes here, the test timeout is the regression catch
|
|
137
|
-
const result = await generateSummary({ headers: [], text }, 'token', { baseApiUrl: mock.url, maxTokens: 18000
|
|
122
|
+
const { result, usage } = await generateSummary({ headers: [], text }, 'token', { baseApiUrl: mock.url, maxTokens: 18000 });
|
|
138
123
|
assert.equal(result.summary, 'ok');
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
const
|
|
124
|
+
assert.ok(usage.charactersRemoved > 0);
|
|
125
|
+
|
|
126
|
+
// the budget covers the system message and the email object together
|
|
127
|
+
const [system, user] = mock.requests[0].body.messages;
|
|
128
|
+
const sent = JSON.parse(user.content);
|
|
129
|
+
const sentText = sent.text;
|
|
130
|
+
const estimate = estimateTokens(system.content) + estimateTokens(JSON.stringify(Object.assign({}, sent, { text: '' }))) + estimateTokens(sentText);
|
|
131
|
+
assert.ok(estimate <= 18000, `estimate ${estimate}`);
|
|
143
132
|
assert.ok(sentText.length > 10000 && sentText.length < text.length);
|
|
144
133
|
assert.ok(text.startsWith(sentText));
|
|
134
|
+
assert.equal(usage.charactersRemoved, text.length - sentText.length);
|
|
145
135
|
});
|
|
146
136
|
|
|
147
137
|
it('generateSummary keeps short text intact', async () => {
|
|
148
138
|
mock.setHandler(() => chatResponse({ sentiment: 'neutral', summary: 'ok', shouldReply: false, riskAssessment: { risk: 1 } }));
|
|
149
139
|
|
|
150
|
-
const
|
|
151
|
-
assert.equal(
|
|
152
|
-
assert.equal(
|
|
140
|
+
const { usage } = await generateSummary({ headers: [], text: 'Short message' }, 'token', { baseApiUrl: mock.url, verbose: true });
|
|
141
|
+
assert.equal(usage.charactersRemoved, 0);
|
|
142
|
+
assert.equal(usage.text, 'Short message');
|
|
153
143
|
});
|
|
154
144
|
|
|
155
145
|
it('generateSummary throws PROMPT_TOO_LONG when the prompt alone exceeds the limit', async () => {
|
|
@@ -158,31 +148,4 @@ describe('prompt fitting', () => {
|
|
|
158
148
|
});
|
|
159
149
|
assert.equal(mock.requests.length, 0);
|
|
160
150
|
});
|
|
161
|
-
|
|
162
|
-
it('embeddingsQuery fits a 64 KB CJK context in one step', { timeout: 5000 }, async () => {
|
|
163
|
-
mock.setHandler(() => chatResponse('Answer: yes\nMessage-ID: <id1>'));
|
|
164
|
-
|
|
165
|
-
await embeddingsQuery('token', { baseApiUrl: mock.url, question: 'What?', contextChunks: cjk(64 * 1024), maxTokens: 18000 });
|
|
166
|
-
|
|
167
|
-
const prompt = mock.requests[0].body.messages[1].content;
|
|
168
|
-
assert.ok(estimateTokens(prompt) <= 18000);
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
it('riskAnalysis fits a 64 KB CJK body in one step', { timeout: 5000 }, async () => {
|
|
172
|
-
mock.setHandler(() => chatResponse({ risk: 1, assessment: 'fine' }));
|
|
173
|
-
|
|
174
|
-
const result = await riskAnalysis({ headers: [], text: cjk(64 * 1024) }, 'token', { baseApiUrl: mock.url, maxTokens: 18000 });
|
|
175
|
-
assert.ok(result._cr > 0);
|
|
176
|
-
assert.ok(estimateTokens(mock.requests[0].body.messages[1].content) <= 18000);
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
it('riskAnalysis and embeddingsQuery throw the same PROMPT_TOO_LONG error', async () => {
|
|
180
|
-
await assert.rejects(() => riskAnalysis({ headers: [], text: 'hello' }, 'token', { baseApiUrl: mock.url, maxTokens: 50 }), {
|
|
181
|
-
code: 'PROMPT_TOO_LONG'
|
|
182
|
-
});
|
|
183
|
-
await assert.rejects(() => embeddingsQuery('token', { baseApiUrl: mock.url, question: 'What?', contextChunks: 'hello', maxTokens: 50 }), {
|
|
184
|
-
code: 'PROMPT_TOO_LONG'
|
|
185
|
-
});
|
|
186
|
-
assert.equal(mock.requests.length, 0);
|
|
187
|
-
});
|
|
188
151
|
});
|
package/examples/emeddings.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const fs = require('fs').promises;
|
|
4
|
-
const { generateEmbeddings } = require('../lib/generate-embeddings');
|
|
5
|
-
const simpleParser = require('mailparser').simpleParser;
|
|
6
|
-
const libmime = require('libmime');
|
|
7
|
-
const util = require('util');
|
|
8
|
-
|
|
9
|
-
async function main() {
|
|
10
|
-
const eml = await fs.readFile(process.argv[2]);
|
|
11
|
-
|
|
12
|
-
const parsed = await simpleParser(eml);
|
|
13
|
-
|
|
14
|
-
const result = await generateEmbeddings(
|
|
15
|
-
{
|
|
16
|
-
headers: parsed.headerLines.map(header => libmime.decodeHeader(header.line)),
|
|
17
|
-
attachments: parsed.attachments,
|
|
18
|
-
html: parsed.html,
|
|
19
|
-
text: parsed.text
|
|
20
|
-
},
|
|
21
|
-
process.env.OPENAI_API_KEY,
|
|
22
|
-
{
|
|
23
|
-
//gptModel: 'gpt-3.5-turbo',
|
|
24
|
-
gptModel: 'gpt-4',
|
|
25
|
-
maxTokens: 6000,
|
|
26
|
-
verbose: true
|
|
27
|
-
}
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
console.log(util.inspect(result, false, 22, true));
|
|
31
|
-
|
|
32
|
-
for (let e of result?.embeddings || []) {
|
|
33
|
-
console.log('CHUNK');
|
|
34
|
-
console.log(e.chunk);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
main();
|
package/examples/question.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { questionQuery } = require('../lib/embeddings-query');
|
|
4
|
-
const util = require('util');
|
|
5
|
-
|
|
6
|
-
async function main() {
|
|
7
|
-
const question = process.argv[2] || 'How much did my last purchase on Amazon cost?';
|
|
8
|
-
|
|
9
|
-
const info = await questionQuery(question, process.env.OPENAI_API_KEY, {
|
|
10
|
-
//gptModel: 'gpt-3.5-turbo',
|
|
11
|
-
//gptModel: 'gpt-4',
|
|
12
|
-
verbose: true
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
console.log(util.inspect(info, false, 22, true));
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
main();
|
package/examples/risk.js
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const fs = require('fs').promises;
|
|
4
|
-
const { generateSummary } = require('..');
|
|
5
|
-
const simpleParser = require('mailparser').simpleParser;
|
|
6
|
-
const libmime = require('libmime');
|
|
7
|
-
const Path = require('path');
|
|
8
|
-
|
|
9
|
-
const BASE_PATH = process.argv[2] || '.';
|
|
10
|
-
const MAX_ENTRIES = Math.abs(Number(process.argv[3]) || 0);
|
|
11
|
-
|
|
12
|
-
async function analyzeEmail(path) {
|
|
13
|
-
const eml = await fs.readFile(path);
|
|
14
|
-
|
|
15
|
-
const parsed = await simpleParser(eml);
|
|
16
|
-
|
|
17
|
-
const result = await generateSummary(
|
|
18
|
-
{
|
|
19
|
-
headers: parsed.headerLines.map(header => libmime.decodeHeader(header.line)),
|
|
20
|
-
attachments: parsed.attachments,
|
|
21
|
-
html: parsed.html,
|
|
22
|
-
text: parsed.text,
|
|
23
|
-
subject: parsed.subject
|
|
24
|
-
},
|
|
25
|
-
process.env.OPENAI_API_KEY,
|
|
26
|
-
{
|
|
27
|
-
//gptModel: 'gpt-3.5-turbo'
|
|
28
|
-
gptModel: 'gpt-4',
|
|
29
|
-
maxTokens: 6000
|
|
30
|
-
}
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
result.file = Path.basename(path);
|
|
34
|
-
|
|
35
|
-
console.log(JSON.stringify(result));
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function shuffleArray(array) {
|
|
39
|
-
for (let i = array.length - 1; i > 0; i--) {
|
|
40
|
-
let j = Math.floor(Math.random() * (i + 1));
|
|
41
|
-
let temp = array[i];
|
|
42
|
-
array[i] = array[j];
|
|
43
|
-
array[j] = temp;
|
|
44
|
-
}
|
|
45
|
-
return array;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
async function main() {
|
|
49
|
-
const stat = await fs.stat(BASE_PATH);
|
|
50
|
-
if (stat.isDirectory()) {
|
|
51
|
-
// process as directory
|
|
52
|
-
|
|
53
|
-
let processed = 0;
|
|
54
|
-
let list = await fs.readdir(BASE_PATH);
|
|
55
|
-
|
|
56
|
-
for (let file of shuffleArray(list)) {
|
|
57
|
-
if (Path.extname(file).toLowerCase() === '.eml') {
|
|
58
|
-
if (processed) {
|
|
59
|
-
process.stdout.write(',');
|
|
60
|
-
}
|
|
61
|
-
try {
|
|
62
|
-
await analyzeEmail(Path.join(BASE_PATH, file));
|
|
63
|
-
console.error(`Processed ${file} [${processed + 1}]`);
|
|
64
|
-
} catch (err) {
|
|
65
|
-
console.log(JSON.stringify({ file, error: err.message }));
|
|
66
|
-
console.error(`Failed processing ${file} [${processed + 1}]`);
|
|
67
|
-
console.error(err);
|
|
68
|
-
}
|
|
69
|
-
processed++;
|
|
70
|
-
|
|
71
|
-
if (MAX_ENTRIES && processed >= MAX_ENTRIES) {
|
|
72
|
-
break;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
console.error(`Processed ${processed} files from ${BASE_PATH}`);
|
|
77
|
-
} else {
|
|
78
|
-
// process as file
|
|
79
|
-
await analyzeEmail(BASE_PATH);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
console.log('[');
|
|
84
|
-
main().then(() => {
|
|
85
|
-
console.log(']');
|
|
86
|
-
});
|