Recommended files with a quant; floating selection bar

Picking model weights in Add model or Download more also selects the
files that usually go with them (chat template, MTP head, mmproj, ...),
shown in a strip where each can be unchecked. Variants are matched by
quant, otherwise the best single one; kinds already selected aren't
added twice. Which kinds is set in Settings -> Downloads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Pascal LinxweilerandClaude Opus 5.5 committed 2026-10-05 23:10:53 +02:00
1 parent 145818f149
commit 0247f257fb
16 files changed
+514 -27

No files matched your search

+56
View File
@@ -0,0 +1,56 @@
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { DEFAULT_RECOMMENDED_ROLES, quantTag, recommendFiles } from "./recommend";
const f = (path: string, size = 1) => ({ path, size });
// Trimmed from unsloth/Qwen3.8-27B-GGUF and unsloth/Qwen3-8B-GGUF.
const qwen38 = [
f(".gitattributes"),
f("MTP/mtp-Qwen3.8-27B-Q4_0.gguf", 1_369_590_656),
f("Qwen3.8-27B-Q4_0.gguf"),
f("Qwen3.8-27B-UD-Q4_K_XL.gguf"),
f("README.md"),
f("config.json"),
f("imatrix_unsloth.gguf"),
f("mmproj-BF16.gguf", 931_146_432),
f("mmproj-F16.gguf", 927_607_488),
];
const qwen3 = [f("Qwen3-8B-Q4_K_M.gguf"), f("Qwen3-8B-Q8_0.gguf"), f("params"), f("template"), f("config.json"), f("imatrix_unsloth.dat")];
describe("quantTag", () => {
it("reads quant and precision tags", () => {
assert.equal(quantTag("MTP/mtp-Qwen3.8-27B-Q4_0.gguf"), "q4_0");
assert.equal(quantTag("mmproj-F16.gguf"), "f16");
assert.equal(quantTag("Qwen3.8-27B-UD-Q4_K_XL.gguf"), "ud-q4_k_xl");
});
});
describe("recommendFiles", () => {
it("adds the only MTP head and prefers the F16 mmproj", () => {
const rec = recommendFiles(qwen38, ["Qwen3.8-27B-UD-Q4_K_XL.gguf"], DEFAULT_RECOMMENDED_ROLES);
assert.deepEqual(rec.sort(), ["MTP/mtp-Qwen3.8-27B-Q4_0.gguf", "mmproj-F16.gguf"]);
});
it("matches the variant with the same quant", () => {
const files = [...qwen38, f("MTP/mtp-Qwen3.8-27B-Q8_0.gguf"), f("Qwen3.8-27B-Q8_0.gguf")];
assert.deepEqual(recommendFiles(files, ["Qwen3.8-27B-Q8_0.gguf"], ["mtp"]), ["MTP/mtp-Qwen3.8-27B-Q8_0.gguf"]);
assert.deepEqual(recommendFiles(files, ["Qwen3.8-27B-Q4_0.gguf"], ["mtp"]), ["MTP/mtp-Qwen3.8-27B-Q4_0.gguf"]);
});
it("adds the chat template, and config only when enabled", () => {
assert.deepEqual(recommendFiles(qwen3, ["Qwen3-8B-Q4_K_M.gguf"], DEFAULT_RECOMMENDED_ROLES), ["template"]);
assert.deepEqual(recommendFiles(qwen3, ["Qwen3-8B-Q4_K_M.gguf"], ["config"]).sort(), ["config.json", "params"]);
});
it("doesn't add a second variant when one is already selected", () => {
assert.deepEqual(recommendFiles(qwen38, ["Qwen3.8-27B-Q4_0.gguf"], ["vision"], ["mmproj-BF16.gguf"]), []);
assert.deepEqual(recommendFiles(qwen38, ["Qwen3.8-27B-Q4_0.gguf", "mmproj-BF16.gguf"], ["vision", "mtp"]), [
"MTP/mtp-Qwen3.8-27B-Q4_0.gguf",
]);
});
it("recommends nothing without picked model files", () => {
assert.deepEqual(recommendFiles(qwen38, [], DEFAULT_RECOMMENDED_ROLES), []);
});
});
+93
View File
@@ -0,0 +1,93 @@
// Files that usually go with the model weights you pick: chat template, MTP head, vision
// projector (mmproj), … Which kinds is a setting. Shared by UI and server (pure functions).
import { groupSplitFiles } from "./file-groups";
import { fileRole, type FileRole } from "./file-roles";
import { quantGroup } from "./selection";
/** File kinds that can be recommended, in display order. */
export const RECOMMENDABLE_ROLES: FileRole[] = ["template", "mtp", "vision", "draft", "config", "tokenizer", "imatrix", "adapter", "docs"];
export const DEFAULT_RECOMMENDED_ROLES: FileRole[] = ["template", "mtp", "vision"];
/** Kinds where a repo has alternatives (one per quant or precision) and one is enough. */
const VARIANT_ROLES = new Set<FileRole>(["mtp", "vision", "draft", "adapter", "imatrix"]);
/** Preferred precision when no variant matches the picked quant. */
const PRECISION_ORDER = ["f16", "bf16", "q8_0", "f32"];
const basename = (p: string) => p.slice(p.lastIndexOf("/") + 1);
const dirname = (p: string) => (p.includes("/") ? p.slice(0, p.lastIndexOf("/")) : "");
/** Quant/precision tag from a file name: "mtp-X-Q4_0.gguf" → "q4_0", "mmproj-F16.gguf" → "f16". */
export function quantTag(path: string): string | null {
const g = quantGroup(basename(path));
return g ? g.replace(/^mmproj-/i, "").toLowerCase() : null;
}
const withoutUd = (q: string) => q.replace(/^ud-/, "");
const bits = (q: string) => q.match(/^(?:ud-)?(?:i?q|tq|bf|f)(\d+)/)?.[1] ?? null;
/**
* Paths to download along with `modelPaths` (model weights), for the given kinds. Variant kinds
* (MTP, mmproj, …) get the variant matching a picked quant, else the best single one; other kinds
* (template, config, …) get every such file at the repo root or next to a picked model file.
* `have`: everything already selected; a variant kind that's already there isn't added again.
*/
export function recommendFiles(
files: { path: string; size: number }[],
modelPaths: string[],
roles: FileRole[],
have: string[] = [],
): string[] {
if (modelPaths.length === 0 || roles.length === 0) return [];
const picked = new Set([...modelPaths, ...have]);
const haveRoles = new Set([...picked].map(fileRole));
const quants = modelPaths.map(quantTag).filter((q): q is string => !!q);
const dirs = new Set(["", ...modelPaths.map(dirname)]);
const out: string[] = [];
for (const role of roles) {
const entries = groupSplitFiles(files.filter((f) => !picked.has(f.path) && fileRole(f.path) === role));
if (entries.length === 0) continue;
if (!VARIANT_ROLES.has(role)) {
for (const e of entries) if (dirs.has(dirname(e.path))) out.push(...e.parts.map((p) => p.path));
continue;
}
if (haveRoles.has(role)) continue; // e.g. an mmproj is already selected
if (entries.length === 1) {
out.push(...entries[0].parts.map((p) => p.path));
continue;
}
const tag = (e: (typeof entries)[number]) => quantTag(e.path) ?? "";
// Same quant as a picked model file (ignoring unsloth's "UD-" prefix): take those.
const same = entries.filter((e) => quants.some((q) => withoutUd(q) === withoutUd(tag(e))));
if (same.length > 0) {
for (const e of same) out.push(...e.parts.map((p) => p.path));
continue;
}
// Otherwise one variant: next to the model, then preferred precision, then same bit width, then smallest.
const pickedBits = new Set(quants.map(bits).filter(Boolean));
const score = (e: (typeof entries)[number]) => {
const t = tag(e);
const precision = PRECISION_ORDER.indexOf(t);
return [
dirs.has(dirname(e.path)) && dirname(e.path) !== "" ? 0 : 1,
precision === -1 ? PRECISION_ORDER.length : precision,
pickedBits.has(bits(t)) ? 0 : 1,
e.size,
];
};
const best = [...entries].sort((a, b) => {
const sa = score(a);
const sb = score(b);
for (let i = 0; i < sa.length; i++) if (sa[i] !== sb[i]) return sa[i] - sb[i];
return 0;
})[0];
out.push(...best.parts.map((p) => p.path));
}
return [...new Set(out)];
}
/** Model-weight paths among `paths`. */
export function modelFilePaths(paths: Iterable<string>): string[] {
return [...paths].filter((p) => fileRole(p) === "model");
}
+17
View File
@@ -4,6 +4,7 @@ import { db } from "@/lib/db/client";
import { settings } from "@/lib/db/schema";
import { isLabelColor } from "@/lib/colors";
import { FILE_ROLE_ORDER, FILE_ROLES, type FileRole, type FileRoleStyles } from "@/lib/file-roles";
import { DEFAULT_RECOMMENDED_ROLES, RECOMMENDABLE_ROLES } from "@/lib/recommend";
const FILE_ROLES_KEY = "labels.fileRoles";
export const FILE_ROLE_LABEL_MAX = 24;
@@ -67,3 +68,19 @@ export async function saveFileRoleStyles(input: Record<string, { label?: unknown
await write(FILE_ROLES_KEY, stored);
return getFileRoleStyles();
}
const RECOMMENDED_KEY = "downloads.recommendedRoles";
/** File kinds picked automatically along with model weights (Settings → Downloads). */
export async function getRecommendedRoles(): Promise<FileRole[]> {
const stored = await read<unknown>(RECOMMENDED_KEY);
if (!Array.isArray(stored)) return DEFAULT_RECOMMENDED_ROLES;
return RECOMMENDABLE_ROLES.filter((r) => stored.includes(r));
}
export async function saveRecommendedRoles(roles: string[]): Promise<FileRole[]> {
const unknown = roles.find((r) => !RECOMMENDABLE_ROLES.includes(r as FileRole));
if (unknown) throw new SettingsError(`"${unknown}" can't be recommended.`);
await write(RECOMMENDED_KEY, RECOMMENDABLE_ROLES.filter((r) => roles.includes(r)));
return getRecommendedRoles();
}