project-nomad/admin/app/validators/settings.ts
jakeaturner 27be71b46e feat(AI): give chat a sampler instead of the backend's defaults
Chat sent num_ctx, num_predict and nothing else, so every turn decoded at
whatever the backend picked: temperature 0.8, top_p 0.9, no min_p. On a
heavily quantized 1-3B model that is the wandering, repetitive output people
read as "the local model is bad".

Adds `ai.responseStyle` with four profiles. min_p is the lever, not
temperature: it floors relative to the top token, so it truncates hard on a
confident distribution and barely at all on a flat one, which is where small
models start to wander. top_p stays at 1.0 rather than stacked on top of it.
repeat_penalty moves with the style, because a grounded answer legitimately
repeats a part number inside the 64-token window: `focused` relaxes it to
1.05, `creative` tightens it to 1.15.

`auto` is the only style that defers to the model. Plenty of library models
ship author-tuned samplers (qwen3 sets 0.6/0.95/20), so those win key by key
and NOMAD fills only the gap, which in practice is min_p. A style the user
picked by name overrides them, or it would silently do nothing on half
their models.

The compat transport drops min_p, so a profile carries a separate compatTopP.
Passing top_p 1.0 to a backend with no min_p behind it would sample the raw
distribution, which is worse than the 0.9 it replaced.

Resolved in the controller, so the grammar-constrained task calls keep their
temperature 0. An explicit temperature still outranks a profile's, which is
what keeps the eval harness deterministic.

`eval:generation --style` is new and load-bearing: temperature 0 makes min_p,
top_p and top_k inert, so a default run cannot see this change at all.
`--style=off` sends no samplers and no temperature, and is the control.

Verified against Ollama 0.32.9, min_p accepted. 22 unit tests.
2026-09-29 15:54:41 -07:00

124 lines
5.5 KiB
TypeScript

import vine from "@vinejs/vine";
import { SETTINGS_KEYS } from "../../constants/kv_store.js";
import type { KVStoreKey } from "../../types/kv_store.js";
import { CONTEXT_LADDER } from "../utils/context_window.js";
import { RESPONSE_STYLE_PRESETS } from "../../constants/ollama.js";
export const getSettingSchema = vine.compile(vine.object({
key: vine.enum(SETTINGS_KEYS),
}))
export const updateSettingSchema = vine.compile(vine.object({
key: vine.enum(SETTINGS_KEYS),
value: vine.any().optional(),
}))
const HHMM_PATTERN = /^([01]\d|2[0-3]):[0-5]\d$/
/**
* Validate the *value* for keys that have format constraints beyond the generic
* enum/any check (the generic validator only constrains the key). Returns an
* error message string when invalid, or null when the value is acceptable.
*/
export function validateSettingValue(key: KVStoreKey, value: unknown): string | null {
switch (key) {
case 'autoUpdate.windowStart':
case 'autoUpdate.windowEnd':
case 'contentAutoUpdate.windowStart':
case 'contentAutoUpdate.windowEnd':
if (typeof value !== 'string' || !HHMM_PATTERN.test(value)) {
return 'Time window values must be in 24-hour HH:MM format (e.g. "20:00").'
}
return null
case 'autoUpdate.cooloffHours':
case 'contentAutoUpdate.cooloffHours': {
const num = Number(value)
if (!Number.isInteger(num) || num < 0 || num > 8760) {
return 'Cool-off must be a whole number of hours between 0 and 8760.'
}
return null
}
case 'system.internetStatusTestUrl': {
// Empty clears the setting (reverts to env var / built-in defaults).
if (value === '' || value === undefined || value === null) {
return null
}
if (typeof value !== 'string') {
return 'Test URL must be a string.'
}
try {
const url = new URL(value)
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
return 'Test URL must use http or https.'
}
} catch {
return 'Test URL must be a valid URL (e.g. "https://example.com").'
}
return null
}
case 'contentAutoUpdate.maxBytesPerWindow': {
// Per-window download budget in bytes. 0 = unlimited.
const num = Number(value)
if (!Number.isInteger(num) || num < 0) {
return 'The per-window data cap must be a whole number of bytes (0 = unlimited).'
}
return null
}
case 'ai.contextWindow': {
// "auto" (or empty) hands sizing to ContextWindowService. An explicit
// value is a *cap*, so it only ever lowers the resolved window — but it
// still has to be a real ladder rung, since a value the backend can't
// honour would silently fall back to the default and confuse the user.
if (value === '' || value === 'auto' || value === undefined || value === null) {
return null
}
const num = Number(value)
if (!CONTEXT_LADDER.includes(num as (typeof CONTEXT_LADDER)[number])) {
return `Context window must be "auto" or one of: ${CONTEXT_LADDER.join(', ')}.`
}
return null
}
case 'rag.minRelevance': {
// Empty/'auto' clears the setting and reverts to RAG_MIN_FINAL_SCORE.
// An explicit 0 is a real choice — it turns the floor off — so it is
// deliberately not treated as "unset".
// Trimmed first so the accepted set matches parseMinRelevance's:
// Number(' ') is 0, which would otherwise validate as "floor off"
// for a value the parser reads as unset.
const raw = typeof value === 'string' ? value.trim() : value
if (raw === '' || raw === 'auto' || raw === undefined || raw === null) {
return null
}
const num = Number(raw)
if (!Number.isFinite(num) || num < 0 || num > 1) {
return 'Relevance threshold must be a number between 0 and 1, or empty to use the recommended default.'
}
return null
}
case 'ai.responseStyle': {
// Empty/unset means 'auto'. Anything else has to be a style we know,
// since an unrecognised value silently resolves back to the default
// and would leave the select showing a choice nothing honours.
if (value === '' || value === undefined || value === null) {
return null
}
const styles = RESPONSE_STYLE_PRESETS.map((preset) => preset.value)
if (typeof value !== 'string' || !styles.includes(value as (typeof styles)[number])) {
return `Response style must be one of: ${styles.join(', ')}.`
}
return null
}
case 'ai.keepAlive': {
// Ollama duration format: "10m", "1h", "30s", "-1" (forever), "0" (evict).
if (value === '' || value === undefined || value === null) {
return null
}
if (typeof value !== 'string' || !/^(-?\d+|\d+(\.\d+)?(ms|s|m|h))$/.test(value)) {
return 'Keep-alive must be a duration like "10m", "1h", or "-1" to keep the model loaded indefinitely.'
}
return null
}
default:
return null
}
}