Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

feat(llamacpp): per-model sampling defaults for the local API server

Akarshan Biswasa63949324ab3
diff --git a/extensions/llamacpp-extension/src/index.ts b/extensions/llamacpp-extension/src/index.ts
index 80c83ec32..62312964f 100644
--- a/extensions/llamacpp-extension/src/index.ts
+++ b/extensions/llamacpp-extension/src/index.ts
@@ -180,6 +180,20 @@ const MODEL_PROVIDER_LOCAL_STORAGE_KEY = 'model-provider'
const LLAMACPP_MODEL_SETTINGS_BACKFILL_KEY =
'llamacpp_model_yaml_backfill_v1'
+// Sampling defaults are floats/ints where 0 is a meaningful value (e.g.
+// temperature=0), so unlike ctx_len these coercions keep 0 and only reject
+// blank/non-finite input.
+const coerceFloatSetting = (v: unknown): YamlSettingValue => {
+ if (v === '' || v == null) return null
+ const n = typeof v === 'number' ? v : Number(v)
+ return Number.isFinite(n) ? n : null
+}
+const coerceIntSetting = (v: unknown): YamlSettingValue => {
+ if (v === '' || v == null) return null
+ const n = typeof v === 'number' ? v : Number(v)
+ return Number.isFinite(n) ? Math.floor(n) : null
+}
+
const MODEL_SETTINGS_YAML_MAPPING: Record<
string,
{
@@ -187,6 +201,21 @@ const MODEL_SETTINGS_YAML_MAPPING: Record<
coerce: (v: unknown) => YamlSettingValue
}
> = {
+ // Sampling defaults: persisted to model.yml and emitted into the router
+ // preset so they apply server-side to every request (chat and external API),
+ // overridable per-request. Keys mirror MODEL_SAMPLING_SETTING_KEYS in the
+ // web-app transport.
+ temperature: { yamlKey: 'temperature', coerce: coerceFloatSetting },
+ top_k: { yamlKey: 'top_k', coerce: coerceIntSetting },
+ top_p: { yamlKey: 'top_p', coerce: coerceFloatSetting },
+ min_p: { yamlKey: 'min_p', coerce: coerceFloatSetting },
+ repeat_last_n: { yamlKey: 'repeat_last_n', coerce: coerceIntSetting },
+ repeat_penalty: { yamlKey: 'repeat_penalty', coerce: coerceFloatSetting },
+ presence_penalty: { yamlKey: 'presence_penalty', coerce: coerceFloatSetting },
+ frequency_penalty: {
+ yamlKey: 'frequency_penalty',
+ coerce: coerceFloatSetting,
+ },
ctx_len: {
yamlKey: 'ctx_size',
coerce: (v) => {
diff --git a/extensions/llamacpp-extension/src/preset.test.ts b/extensions/llamacpp-extension/src/preset.test.ts
index 0cab86049..c91acadef 100644
--- a/extensions/llamacpp-extension/src/preset.test.ts
+++ b/extensions/llamacpp-extension/src/preset.test.ts
@@ -112,6 +112,38 @@ describe('generatePreset MTP emission', () => {
expect(ini).not.toContain('spec-draft-model')
})
+ it('emits per-model sampling defaults with CLI-style INI keys', async () => {
+ setupModel('s', {
+ temperature: 0,
+ top_k: 40,
+ top_p: 0.9,
+ min_p: 0.05,
+ repeat_last_n: 64,
+ repeat_penalty: 1.1,
+ presence_penalty: 0.5,
+ frequency_penalty: 0.25,
+ })
+ await generatePreset('/p', '/jan', CONFIG, { supportsMtp: false })
+ const ini = writtenFiles['/p/router.preset.ini']
+ expect(ini).toContain('temperature = 0')
+ expect(ini).toContain('top-k = 40')
+ expect(ini).toContain('top-p = 0.9')
+ expect(ini).toContain('min-p = 0.05')
+ expect(ini).toContain('repeat-last-n = 64')
+ expect(ini).toContain('repeat-penalty = 1.1')
+ expect(ini).toContain('presence-penalty = 0.5')
+ expect(ini).toContain('frequency-penalty = 0.25')
+ })
+
+ it('omits sampling keys that are absent or non-numeric', async () => {
+ setupModel('s', { temperature: 0.7 })
+ await generatePreset('/p', '/jan', CONFIG, { supportsMtp: false })
+ const ini = writtenFiles['/p/router.preset.ini']
+ expect(ini).toContain('temperature = 0.7')
+ expect(ini).not.toContain('top-p')
+ expect(ini).not.toContain('min-p')
+ })
+
it('skips out-of-range spec tunables', async () => {
setupModel('glm', {
mtp: true,
diff --git a/extensions/llamacpp-extension/src/preset.ts b/extensions/llamacpp-extension/src/preset.ts
index 4ce2bd21d..dbeb256b8 100644
--- a/extensions/llamacpp-extension/src/preset.ts
+++ b/extensions/llamacpp-extension/src/preset.ts
@@ -29,6 +29,14 @@ type ModelYaml = ModelConfig & {
mtp_layers?: number
mtp?: boolean
mtp_model_path?: string
+ temperature?: number
+ top_k?: number
+ top_p?: number
+ min_p?: number
+ repeat_last_n?: number
+ repeat_penalty?: number
+ presence_penalty?: number
+ frequency_penalty?: number
spec_draft_n_max?: number
spec_draft_n_min?: number
spec_draft_p_min?: number
@@ -439,6 +447,27 @@ export async function generatePreset(
}
}
+ // Per-model sampling defaults. llama-server applies these as server-side
+ // defaults for every request to the model (chat and external API clients);
+ // a per-request JSON field still overrides them. INI keys are the CLI
+ // long-form names minus dashes.
+ const samplingIniKeys: Array<[keyof ModelYaml, string]> = [
+ ['temperature', 'temperature'],
+ ['top_k', 'top-k'],
+ ['top_p', 'top-p'],
+ ['min_p', 'min-p'],
+ ['repeat_last_n', 'repeat-last-n'],
+ ['repeat_penalty', 'repeat-penalty'],
+ ['presence_penalty', 'presence-penalty'],
+ ['frequency_penalty', 'frequency-penalty'],
+ ]
+ for (const [yamlKey, iniKey] of samplingIniKeys) {
+ const v = mc[yamlKey]
+ if (typeof v === 'number' && Number.isFinite(v)) {
+ lines.push(`${iniKey} = ${v}`)
+ }
+ }
+
if (mc.embedding === true) {
embeddingCount++
lines.push('embeddings = true')
diff --git a/web-app/src/containers/ModelSetting.tsx b/web-app/src/containers/ModelSetting.tsx
index 3a7d3cfdc..1a1add235 100644
--- a/web-app/src/containers/ModelSetting.tsx
+++ b/web-app/src/containers/ModelSetting.tsx
@@ -23,6 +23,20 @@ import { paramsSettings } from '@/lib/predefinedParams'
const MTP_MIN_BUILD = 9193
+// Sampling defaults shown in the model settings sheet. These persist to the
+// model (model.yml → router preset for llamacpp) and act as server-side
+// defaults for the local API server; per-request values still override.
+// `repeat_last_n` is intentionally omitted — it has no paramsSettings entry.
+const SAMPLER_DEFAULT_KEYS = [
+ 'temperature',
+ 'top_p',
+ 'top_k',
+ 'min_p',
+ 'repeat_penalty',
+ 'presence_penalty',
+ 'frequency_penalty',
+] as const
+
function parseBuildNumber(version: unknown): number | null {
if (typeof version !== 'string') return null
const m = version.match(/^b(\d+)$/)
@@ -246,6 +260,45 @@ export function ModelSetting({
{provider.provider === 'llamacpp' && (
<MtpPanel modelId={model.id} provider={provider} />
)}
+ {(provider.provider === 'llamacpp' || provider.provider === 'mlx') && (
+ <div className="space-y-4">
+ <div>
+ <span className="font-medium">
+ {t('common:modelSettings.samplingDefaults.title')}
+ </span>
+ <p className="text-muted-foreground leading-normal text-xs">
+ {t('common:modelSettings.samplingDefaults.description')}
+ </p>
+ </div>
+ {SAMPLER_DEFAULT_KEYS.map((key) => {
+ const def = paramsSettings[key]
+ if (!def) return null
+ const stored = model.settings?.[key]?.controller_props?.value
+ const value = stored ?? def.value
+ return (
+ <div key={key} className="space-y-2">
+ <div className="flex items-start justify-between gap-8">
+ <div className="mb-1 truncate">
+ <span title={def.title} className="font-medium">
+ {def.title}
+ </span>
+ </div>
+ <DynamicControllerSetting
+ title={def.title}
+ description={def.description}
+ controllerType={def.controllerType}
+ controllerProps={{ ...(def.controllerProps ?? {}), value }}
+ onChange={(newValue) => handleSettingChange(key, newValue)}
+ />
+ </div>
+ <p className="text-muted-foreground leading-normal text-xs">
+ {def.description}
+ </p>
+ </div>
+ )
+ })}
+ </div>
+ )}
{fitEnabled && fitCtxSetting && (
<div key="fit_ctx" className="space-y-2">
<div className="flex items-start justify-between gap-8">
diff --git a/web-app/src/locales/en/common.json b/web-app/src/locales/en/common.json
index 378b8bdbe..4d36b87a8 100644
--- a/web-app/src/locales/en/common.json
+++ b/web-app/src/locales/en/common.json
@@ -245,6 +245,10 @@
"modelSettings": {
"title": "Model Settings - {{modelId}}",
"description": "Configure model settings to optimize performance and behavior.",
+ "samplingDefaults": {
+ "title": "Default sampling parameters",
+ "description": "Used by the local API server (and new chats) when a request doesn't set them. Per-request values always override these."
+ },
"mtp": {
"section": "Multi-Token Prediction (MTP)",
"enable": "Enable MTP",