Skip to content

Commit ef07e91

Browse files
committed
fix(shared): cap content length before tokenizing prompts
The prompt builder tokenized the whole article with the pure-JS tiktoken before truncating it to the context length, blocking the event loop on long articles. Cut the content to contextLength * 8 characters before encoding.
1 parent 123893e commit ef07e91

1 file changed

Lines changed: 3 additions & 0 deletions

File tree

‎packages/shared/prompts.server.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ async function truncateContent(
2828
length: number,
2929
): Promise<string> {
3030
const enc = await getEncodingInstance();
31+
// Tokenizing is slow and blocks the event loop, so cut the content to a
32+
// generous upper bound of characters before encoding it.
33+
content = content.slice(0, length * 8);
3134
const tokens = enc.encode(content);
3235
if (tokens.length <= length) {
3336
return content;

0 commit comments

Comments
 (0)