diff options
| author | Mohamed Bassem <me@mbassem.com> | 2025-11-28 10:29:49 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-28 10:29:49 +0000 |
| commit | e2877b458fedbf9a75be16439d18b9de9f5ad924 (patch) | |
| tree | 93f52aeae227fe46ab10c901b9b7042487c9840d /apps/workers | |
| parent | a13a227e20f2890dec4a361b8fc68dda5adc5d22 (diff) | |
| download | karakeep-e2877b458fedbf9a75be16439d18b9de9f5ad924.tar.zst | |
fix: lazy load js-tiktoken in prompts module (#2176)
* feat: lazy load tiktoken to reduce memory footprint
The js-tiktoken module loads a large encoding dictionary into memory
immediately on import. This change defers the loading of the encoding
until it's actually needed by using a lazy getter pattern.
This reduces memory usage for processes that import this module but
don't actually use the token encoding functions.
* fix: use createRequire for lazy tiktoken import in ES module
The previous implementation used bare require() which fails at runtime
in ES modules (ReferenceError: require is not defined). This fixes it
by using createRequire from Node's 'module' package, which creates a
require function that works in ES module contexts.
* refactor: convert tiktoken lazy loading to async dynamic imports
Changed from createRequire to async import() for lazy loading tiktoken,
making buildTextPrompt and buildSummaryPrompt async. This is cleaner for
ES modules and properly defers the large tiktoken encoding data until
it's actually needed.
Updated all callers to await these async functions:
- packages/trpc/routers/bookmarks.ts
- apps/workers/workers/inference/tagging.ts
- apps/workers/workers/inference/summarize.ts
- apps/web/components/settings/AISettings.tsx (converted to useEffect)
* feat: add untruncated prompt builders for UI previews
Added buildTextPromptUntruncated and buildSummaryPromptUntruncated
functions that don't require token counting or truncation. These are
synchronous and don't load tiktoken, making them perfect for UI
previews where exact token limits aren't needed.
Updated AISettings.tsx to use these untruncated versions, eliminating
the need for useEffect/useState and avoiding unnecessary tiktoken
loading in the browser.
* fix
* fix
---------
Co-authored-by: Claude <noreply@anthropic.com>
Diffstat (limited to 'apps/workers')
| -rw-r--r-- | apps/workers/workers/inference/summarize.ts | 2 | ||||
| -rw-r--r-- | apps/workers/workers/inference/tagging.ts | 6 |
2 files changed, 4 insertions, 4 deletions
diff --git a/apps/workers/workers/inference/summarize.ts b/apps/workers/workers/inference/summarize.ts index 0fa24869..23636961 100644 --- a/apps/workers/workers/inference/summarize.ts +++ b/apps/workers/workers/inference/summarize.ts @@ -105,7 +105,7 @@ URL: ${link.url ?? ""} }, }); - const summaryPrompt = buildSummaryPrompt( + const summaryPrompt = await buildSummaryPrompt( serverConfig.inference.inferredTagLang, prompts.map((p) => p.text), textToSummarize, diff --git a/apps/workers/workers/inference/tagging.ts b/apps/workers/workers/inference/tagging.ts index 50a03b00..5a79fd22 100644 --- a/apps/workers/workers/inference/tagging.ts +++ b/apps/workers/workers/inference/tagging.ts @@ -94,7 +94,7 @@ async function buildPrompt( ); return null; } - return buildTextPrompt( + return await buildTextPrompt( serverConfig.inference.inferredTagLang, prompts, `URL: ${bookmark.link.url} @@ -106,7 +106,7 @@ Content: ${content ?? ""}`, } if (bookmark.text) { - return buildTextPrompt( + return await buildTextPrompt( serverConfig.inference.inferredTagLang, prompts, bookmark.text.text ?? "", @@ -215,7 +215,7 @@ async function inferTagsFromPDF( inferenceClient: InferenceClient, abortSignal: AbortSignal, ) { - const prompt = buildTextPrompt( + const prompt = await buildTextPrompt( serverConfig.inference.inferredTagLang, await fetchCustomPrompts(bookmark.userId, "text"), `Content: ${bookmark.asset.content}`, |
