-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathscenario-message-truncation.mjs
More file actions
51 lines (47 loc) · 1.82 KB
/
scenario-message-truncation.mjs
File metadata and controls
51 lines (47 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import * as Sentry from '@sentry/node';
import { generateText } from 'ai';
import { MockLanguageModelV1 } from 'ai/test';
async function run() {
await Sentry.startSpan({ op: 'function', name: 'main' }, async () => {
const largeContent1 = 'A'.repeat(15000); // ~15KB
const largeContent2 = 'B'.repeat(15000); // ~15KB
const largeContent3 = 'C'.repeat(25000) + 'D'.repeat(25000); // ~50KB (will be truncated)
// Test 1: Messages array with large last message that gets truncated
// Only the last message should be kept, and it should be truncated to only Cs
await generateText({
experimental_telemetry: { isEnabled: true },
model: new MockLanguageModelV1({
doGenerate: async () => ({
rawCall: { rawPrompt: null, rawSettings: {} },
finishReason: 'stop',
usage: { promptTokens: 10, completionTokens: 5 },
text: 'Response to truncated messages',
}),
}),
messages: [
{ role: 'user', content: largeContent1 },
{ role: 'assistant', content: largeContent2 },
{ role: 'user', content: largeContent3 },
],
});
// Test 2: Messages array where last message is small and kept intact
const smallContent = 'This is a small message that fits within the limit';
await generateText({
experimental_telemetry: { isEnabled: true },
model: new MockLanguageModelV1({
doGenerate: async () => ({
rawCall: { rawPrompt: null, rawSettings: {} },
finishReason: 'stop',
usage: { promptTokens: 10, completionTokens: 5 },
text: 'Response to small message',
}),
}),
messages: [
{ role: 'user', content: largeContent1 },
{ role: 'assistant', content: largeContent2 },
{ role: 'user', content: smallContent },
],
});
});
}
run();