Repository navigation
feat(sse): real LLMLingua-2 ONNX compression engine (stable) #4014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d433b7e
feat(sse): add llmlingua model registry + model store
diegosouzapw 3c59386
feat(sse): extend llmlingua config schema + backend options + minToke…
diegosouzapw 5c98480
feat(sse): wire real llmlingua onnx worker-thread backend
diegosouzapw f8ed940
feat(sse): declare llmlingua optional deps + pin transformers 3.5.2
diegosouzapw 7ad8719
feat(sse): ship llmlingua onnx worker in standalone build + warm-on-s…
diegosouzapw 9175393
build: keep llmlingua onnxWorker.js in the standalone bundle
diegosouzapw 99ff136
fix(sse): resolve llmlingua worker via import.meta.url walk-up (cwd-i…
diegosouzapw 629fc57
feat(sse): promote llmlingua to stable + fix optional-deps gate
diegosouzapw 6417e3f
Merge remote-tracking branch 'origin/release/v3.8.27' into HEAD
diegosouzapw 816f476
chore(quality): allowlist LLMLingua-2 ONNX engine deps
diegosouzapw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
open-sse/services/compression/engines/llmlingua/constants.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /** | ||
| * LLMLingua real-engine constants — pure data + types. | ||
| * | ||
| * NO imports of native deps (transformers.js, onnxruntime, etc). This module is | ||
| * safe to import from anywhere (main thread, worker, tests) without pulling in | ||
| * the heavy ONNX runtime. | ||
| * | ||
| * The real backend uses `@atjsh/llmlingua-2` (ONNX via `@huggingface/transformers`), | ||
| * which downloads models from the HuggingFace Hub into a cache dir. Only the two | ||
| * models PROVEN to work end-to-end are registered here. | ||
| */ | ||
|
|
||
| export type LlmlinguaFactory = "WithBERTMultilingual" | "WithXLMRoBERTa"; | ||
|
|
||
| export interface LlmlinguaModelEntry { | ||
| /** config value, e.g. "tinybert" */ | ||
| id: string; | ||
| /** HuggingFace Hub repo id */ | ||
| hfRepo: string; | ||
| factory: LlmlinguaFactory; | ||
| dtype: "fp32"; | ||
| /** transformers.js subfolder option; "" for both proven models */ | ||
| subfolder: string; | ||
| sizeMB: number; | ||
| label: string; | ||
| } | ||
|
|
||
| export const DEFAULT_LLMLINGUA_MODEL = "tinybert"; | ||
|
|
||
| /** Registry keyed by config `model` value. Only the two PROVEN models. */ | ||
| export const LLMLINGUA_MODELS: Record<string, LlmlinguaModelEntry> = { | ||
| tinybert: { | ||
| id: "tinybert", | ||
| hfRepo: "atjsh/llmlingua-2-js-tinybert-meetingbank", | ||
| factory: "WithBERTMultilingual", | ||
| dtype: "fp32", | ||
| subfolder: "", | ||
| sizeMB: 57, | ||
| label: "TinyBERT (57MB, fast — default)", | ||
| }, | ||
| "bert-base": { | ||
| id: "bert-base", | ||
| hfRepo: "Arcoldd/llmlingua4j-bert-base-onnx", | ||
| factory: "WithBERTMultilingual", | ||
| dtype: "fp32", | ||
| subfolder: "", | ||
| sizeMB: 710, | ||
| label: "BERT-base (710MB, higher accuracy)", | ||
| }, | ||
| }; | ||
|
|
||
| /** Per-call worker reply timeout → fail-open. */ | ||
| export const LLMLINGUA_WORKER_TIMEOUT_MS = 5000; | ||
| /** Terminate the idle worker after this long to free model RAM. */ | ||
| export const LLMLINGUA_WORKER_IDLE_MS = 300000; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
open-sse/services/compression/engines/llmlingua/modelStore.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /** | ||
| * LLMLingua model store — thin path/config resolver. | ||
| * | ||
| * transformers.js owns the actual model download (from the HuggingFace Hub into | ||
| * its `cacheDir`). This module only resolves the cache directory, maps config | ||
| * model ids to registry entries, and configures a transformers.js `env` object | ||
| * for either Hub download (default) or a local modelPath override. | ||
| * | ||
| * Deliberately does NOT import the native `@huggingface/transformers` dep — it | ||
| * accepts a minimal structural `env` so the heavy runtime stays out of this path. | ||
| */ | ||
|
|
||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
| import fs from "node:fs"; | ||
| import { | ||
| DEFAULT_LLMLINGUA_MODEL, | ||
| LLMLINGUA_MODELS, | ||
| type LlmlinguaModelEntry, | ||
| } from "./constants.ts"; | ||
|
|
||
| /** A minimal structural type for the transformers.js `env` object (avoids importing the native dep here). */ | ||
| export interface TransformersEnvLike { | ||
| cacheDir?: string; | ||
| localModelPath?: string; | ||
| allowRemoteModels?: boolean; | ||
| [key: string]: unknown; | ||
| } | ||
|
|
||
| /** Base data dir. Mirrors rtk's getDataDir() at engines/rtk/filterLoader.ts. */ | ||
| function getDataDir(): string { | ||
| return process.env.DATA_DIR || path.join(os.homedir(), ".omniroute"); | ||
| } | ||
|
|
||
| /** Resolve (and ensure) the model cache dir: `${DATA_DIR}/models/llmlingua`. Mirrors rtk's getDataDir(). */ | ||
| export function getLlmlinguaModelCacheDir(): string { | ||
| const dir = path.join(getDataDir(), "models", "llmlingua"); | ||
| try { | ||
| fs.mkdirSync(dir, { recursive: true }); | ||
| } catch { | ||
| // Ignore mkdir errors — fail-open philosophy: transformers.js will surface a | ||
| // clearer error if the dir is genuinely unusable, and callers fail-open anyway. | ||
| } | ||
| return dir; | ||
| } | ||
|
|
||
| /** Resolve a config model id to its registry entry; falls back to the default for unknown/empty ids. */ | ||
| export function resolveLlmlinguaModel(modelId: string | undefined | null): LlmlinguaModelEntry { | ||
| if (typeof modelId === "string" && modelId.length > 0 && LLMLINGUA_MODELS[modelId]) { | ||
| return LLMLINGUA_MODELS[modelId]; | ||
| } | ||
| return LLMLINGUA_MODELS[DEFAULT_LLMLINGUA_MODEL]; | ||
| } | ||
|
|
||
| /** Configure a transformers.js `env` for either Hub download (default) or a local modelPath override. */ | ||
| export function configureTransformersEnv( | ||
| env: TransformersEnvLike, | ||
| opts: { modelPath?: string } | ||
| ): void { | ||
| env.cacheDir = getLlmlinguaModelCacheDir(); | ||
| if (typeof opts.modelPath === "string" && opts.modelPath.length > 0) { | ||
| env.localModelPath = opts.modelPath; | ||
| env.allowRemoteModels = false; | ||
| } else { | ||
| env.allowRemoteModels = true; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
m.contentis an array of message parts (e.g., in multi-part messages containing text and other media), usingJSON.stringifyon the entire array includes JSON syntax boilerplate (brackets, keys, quotes, etc.). This artificially inflates the estimated token count calculated byestimateCompressionTokens, which can cause the engine to incorrectly skip or apply the token floor. Extracting only the actual text parts from the array (similar to howprocessMessageshandles it) provides a much more accurate token estimation.