Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Fix web LLM runtime defaults

Prateek Sunal87bb6a3e4f95
diff --git a/web/apps/ensu/src/pages/chat.tsx b/web/apps/ensu/src/pages/chat.tsx
index b8aaf65f18..f285cd48ed 100644
--- a/web/apps/ensu/src/pages/chat.tsx
+++ b/web/apps/ensu/src/pages/chat.tsx
@@ -94,6 +94,8 @@ const formatTime = (timestamp: number) => {
};
const DEFAULT_GENERATION_MAX_TOKENS = 8_192;
+const OVERFLOW_SAFETY_TOKENS = 256;
+const DEFAULT_WEB_CONTEXT_SIZE = 4096;
const loadingPhraseVerbs = [
"Generating",
@@ -1208,17 +1210,37 @@ const Page: React.FC = () => {
contextLength?: string;
maxTokens?: string;
};
+ const rawContextLength = parsed.contextLength ?? "";
+ const clampedContextLength =
+ !isTauriRuntime &&
+ rawContextLength &&
+ Number(rawContextLength) > DEFAULT_WEB_CONTEXT_SIZE
+ ? String(DEFAULT_WEB_CONTEXT_SIZE)
+ : rawContextLength;
+ if (clampedContextLength !== rawContextLength) {
+ const nextSettings = {
+ useCustomModel: !!parsed.useCustomModel,
+ modelUrl: parsed.modelUrl ?? "",
+ mmprojUrl: allowMmproj ? (parsed.mmprojUrl ?? "") : "",
+ contextLength: clampedContextLength,
+ maxTokens: parsed.maxTokens ?? "",
+ };
+ window.localStorage.setItem(
+ "ensu.modelSettings",
+ JSON.stringify(nextSettings),
+ );
+ }
setUseCustomModel(
MODEL_SETTINGS_ENABLED && !!parsed.useCustomModel,
);
setModelUrl(parsed.modelUrl ?? "");
setMmprojUrl(allowMmproj ? (parsed.mmprojUrl ?? "") : "");
- setContextLength(parsed.contextLength ?? "");
+ setContextLength(clampedContextLength);
setMaxTokens(parsed.maxTokens ?? "");
} catch (error) {
log.error("Failed to read model settings", error);
}
- }, [allowMmproj]);
+ }, [allowMmproj, isTauriRuntime]);
const applyDownloadProgress = useCallback((progress: DownloadProgress) => {
setDownloadStatus(progress);
@@ -1944,25 +1966,36 @@ const Page: React.FC = () => {
);
const formatErrorMessage = useCallback((error: unknown) => {
- if (error instanceof Error) return error.message;
- if (typeof error === "string") return error;
+ const normalizeErrorMessage = (message: string) => {
+ if (
+ message
+ .toLowerCase()
+ .includes("length out of range of buffer")
+ ) {
+ return "Prompt exceeds the model context window. Reduce history, lower max tokens, or increase context length.";
+ }
+ return message;
+ };
+
+ if (error instanceof Error) return normalizeErrorMessage(error.message);
+ if (typeof error === "string") return normalizeErrorMessage(error);
if (error && typeof error === "object") {
const maybeMessage = (error as { message?: unknown }).message;
if (typeof maybeMessage === "string" && maybeMessage.trim()) {
- return maybeMessage;
+ return normalizeErrorMessage(maybeMessage);
}
if ("__wbg_ptr" in error) {
return "Model failed to start. Please refresh and try again.";
}
const text = String(error);
if (text && text !== "[object Object]") {
- return text;
+ return normalizeErrorMessage(text);
}
}
try {
- return JSON.stringify(error);
+ return normalizeErrorMessage(JSON.stringify(error));
} catch {
- return String(error);
+ return normalizeErrorMessage(String(error));
}
}, []);
@@ -2369,6 +2402,7 @@ const Page: React.FC = () => {
contextSize -
(maxTokensCount ?? DEFAULT_GENERATION_MAX_TOKENS) -
safetyMargin;
+ budget -= approxTokens(buildChatSystemPrompt());
budget -= approxTokens(promptText);
if (budget <= 0) return [];
@@ -2913,6 +2947,17 @@ const Page: React.FC = () => {
...history,
{ role: "user", content: promptText },
];
+ const promptTokenEstimate = messages.reduce(
+ (total, message) => total + approxTokens(message.content),
+ 0,
+ );
+ const inputBudget =
+ contextSize - maxTokens - OVERFLOW_SAFETY_TOKENS;
+ if (promptTokenEstimate > inputBudget) {
+ throw new Error(
+ "Prompt exceeds the model context window. Reduce history, lower max tokens, or increase context length.",
+ );
+ }
if (pendingCancelRef.current || stopRequestedRef.current) {
pendingCancelRef.current = false;
@@ -3338,27 +3383,38 @@ const Page: React.FC = () => {
);
const suggestedModels = useMemo(
- () => [
- {
- name: "Qwen 3.5 2B (Q8_0)",
- url: "https://huggingface.co/unsloth/Qwen3.5-2B-GGUF/resolve/main/Qwen3.5-2B-Q8_0.gguf?download=true",
- mmproj: "https://huggingface.co/unsloth/Qwen3.5-2B-GGUF/resolve/main/mmproj-F16.gguf",
- },
- {
- name: "LFM 2.5 1.2B Instruct (Q4_0)",
- url: "https://huggingface.co/LiquidAI/LFM2.5-1.2B-Instruct-GGUF/resolve/main/LFM2.5-1.2B-Instruct-Q4_0.gguf",
- },
- {
- name: "Qwen3-VL 2B Instruct (Q4_K_M)",
- url: "https://huggingface.co/Qwen/Qwen3-VL-2B-Instruct-GGUF/resolve/main/Qwen3VL-2B-Instruct-Q4_K_M.gguf",
- mmproj: "https://huggingface.co/Qwen/Qwen3-VL-2B-Instruct-GGUF/resolve/main/mmproj-Qwen3VL-2B-Instruct-Q8_0.gguf",
- },
- {
- name: "Llama 3.2 1B Instruct (Q4_K_M)",
- url: "https://huggingface.co/bartowski/Llama-3.2-1B-Instruct-GGUF/resolve/main/Llama-3.2-1B-Instruct-Q4_K_M.gguf",
- },
- ],
- [],
+ () =>
+ isTauriRuntime
+ ? [
+ {
+ name: "Qwen 3.5 0.8B (Q4_K_M)",
+ url: "https://huggingface.co/unsloth/Qwen3.5-0.8B-GGUF/resolve/main/Qwen3.5-0.8B-Q4_K_M.gguf?download=true",
+ mmproj: "https://huggingface.co/unsloth/Qwen3.5-0.8B-GGUF/resolve/main/mmproj-F16.gguf",
+ },
+ {
+ name: "Qwen 3.5 2B (Q8_0)",
+ url: "https://huggingface.co/unsloth/Qwen3.5-2B-GGUF/resolve/main/Qwen3.5-2B-Q8_0.gguf?download=true",
+ mmproj: "https://huggingface.co/unsloth/Qwen3.5-2B-GGUF/resolve/main/mmproj-F16.gguf",
+ },
+ {
+ name: "Qwen 3.5 4B (Q4_K_M)",
+ url: "https://huggingface.co/unsloth/Qwen3.5-4B-GGUF/resolve/main/Qwen3.5-4B-Q4_K_M.gguf?download=true",
+ mmproj: "https://huggingface.co/unsloth/Qwen3.5-4B-GGUF/resolve/main/mmproj-F16.gguf",
+ },
+ ]
+ : [
+ {
+ name: "LFM 2.5 VL 1.6B (Q4_0)",
+ url: "https://huggingface.co/LiquidAI/LFM2.5-VL-1.6B-GGUF/resolve/main/LFM2.5-VL-1.6B-Q4_0.gguf",
+ mmproj: "https://huggingface.co/LiquidAI/LFM2.5-VL-1.6B-GGUF/resolve/main/mmproj-LFM2.5-VL-1.6b-Q8_0.gguf",
+ },
+ {
+ name: "Qwen 3.5 0.8B (Q4_K_M)",
+ url: "https://huggingface.co/unsloth/Qwen3.5-0.8B-GGUF/resolve/main/Qwen3.5-0.8B-Q4_K_M.gguf?download=true",
+ mmproj: "https://huggingface.co/unsloth/Qwen3.5-0.8B-GGUF/resolve/main/mmproj-F16.gguf",
+ },
+ ],
+ [isTauriRuntime],
);
const validateModelSettings = useCallback(() => {
diff --git a/web/apps/ensu/src/services/llm/inference.ts b/web/apps/ensu/src/services/llm/inference.ts
index fb2bf12659..f1d7861687 100644
--- a/web/apps/ensu/src/services/llm/inference.ts
+++ b/web/apps/ensu/src/services/llm/inference.ts
@@ -16,7 +16,7 @@ import type {
} from "./types";
const WLLAMA_VERSION = "2.3.7";
-const CDN_BASE = `https://unpkg.com/@wllama/wllama@${WLLAMA_VERSION}/esm`;
+const CDN_BASE = `https://cdn.jsdelivr.net/npm/@wllama/wllama@${WLLAMA_VERSION}/src`;
const MIN_GGUF_BYTES = 1024 * 1024;
const DEFAULT_GENERATION_MAX_TOKENS = 8_192;
diff --git a/web/apps/ensu/src/services/llm/provider.ts b/web/apps/ensu/src/services/llm/provider.ts
index 21ab6a3ae2..b393f0ef5e 100644
--- a/web/apps/ensu/src/services/llm/provider.ts
+++ b/web/apps/ensu/src/services/llm/provider.ts
@@ -9,22 +9,23 @@ import type {
ModelSettings,
} from "./types";
-const DEFAULT_CONTEXT_SIZE = 12000;
+const DEFAULT_WEB_CONTEXT_SIZE = 4096;
+const DEFAULT_TAURI_CONTEXT_SIZE = 12000;
const DEFAULT_GENERATION_MAX_TOKENS = 8_192;
const OVERFLOW_SAFETY_TOKENS = 256;
const MIN_GGUF_BYTES = 1024 * 1024; // 1MB
const MIN_HIGH_RAM_MAC_BYTES = 16 * 1024 * 1024 * 1024;
export const DEFAULT_MODEL: ModelInfo = {
- id: "qwen-3.5-0.8b-q4km",
- name: "Qwen 3.5 0.8B (Q4_K_M)",
- url: "https://huggingface.co/unsloth/Qwen3.5-0.8B-GGUF/resolve/main/Qwen3.5-0.8B-Q4_K_M.gguf?download=true",
+ id: "lfm-2.5-vl-1.6b",
+ name: "LFM 2.5 VL 1.6B (Q4_0)",
+ url: "https://huggingface.co/LiquidAI/LFM2.5-VL-1.6B-GGUF/resolve/main/LFM2.5-VL-1.6B-Q4_0.gguf",
mmprojUrl:
- "https://huggingface.co/unsloth/Qwen3.5-0.8B-GGUF/resolve/main/mmproj-F16.gguf",
- description: "Qwen multimodal model (text-only on web)",
- sizeBytes: 532_517_120,
- mmprojSizeBytes: 204_987_232,
- sizeHuman: "533 MB",
+ "https://huggingface.co/LiquidAI/LFM2.5-VL-1.6B-GGUF/resolve/main/mmproj-LFM2.5-VL-1.6b-Q8_0.gguf",
+ description: "Liquid AI multimodal model (text-only on web)",
+ sizeBytes: 695_752_160,
+ mmprojSizeBytes: 583_109_888,
+ sizeHuman: "~664 MB",
};
const TAURI_DEFAULT_MODEL: ModelInfo = {
@@ -101,10 +102,18 @@ export class LlmProvider {
public resolveRuntimeSettings(settings: ModelSettings) {
const model = this.resolveTargetModel(settings);
- const contextSize =
+ const defaultContextSize =
+ this.backend.kind === "tauri"
+ ? DEFAULT_TAURI_CONTEXT_SIZE
+ : DEFAULT_WEB_CONTEXT_SIZE;
+ const requestedContextSize =
settings.contextLength ??
model.contextLength ??
- DEFAULT_CONTEXT_SIZE;
+ defaultContextSize;
+ const contextSize =
+ this.backend.kind === "tauri"
+ ? requestedContextSize
+ : Math.min(requestedContextSize, DEFAULT_WEB_CONTEXT_SIZE);
const configuredMaxTokens = settings.maxTokens ?? model.maxTokens;
const maxAllowedTokens = Math.max(
1,
@@ -308,7 +317,11 @@ export class LlmProvider {
await this.backend.freeContext();
this.currentContextKey = undefined;
if (this.currentModel && this.currentModelPath) {
- const resolvedContext = contextSize ?? DEFAULT_CONTEXT_SIZE;
+ const resolvedContext =
+ contextSize ??
+ (this.backend.kind === "tauri"
+ ? DEFAULT_TAURI_CONTEXT_SIZE
+ : DEFAULT_WEB_CONTEXT_SIZE);
await this.backend.createContext(
{ modelPath: this.currentModelPath },
{ contextSize: resolvedContext },