Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Make summary, title, chat and event temperatures configurable (#411)

Murtaza Nasirf3f1b0d3c69a
diff --git a/docs/admin-guide/prompts.md b/docs/admin-guide/prompts.md
index d052e246..736b42b5 100644
--- a/docs/admin-guide/prompts.md
+++ b/docs/admin-guide/prompts.md
@@ -40,6 +40,10 @@ The **Default Title Prompt** card sets the instructions for AI-generated recordi
Title prompts use the same precedence as summary prompts: tag, then folder, then the user's own title prompt, then this default, then the built-in default. Group administrators can set a title prompt on group tags and folders as well.
+## Generation Temperature
+
+In the Default Prompts tab, you can also set the sampling temperature for four kinds of requests to the language model: summaries, titles, chat and event extraction. A value saved here applies to every user. Without a saved value, the built-in default is used: 0.5 for summaries, 0.7 for titles, 0.7 for chat and 0.2 for event extraction. Below each field, the value in use is displayed, with a note when it is the default. To restore the default, use the reset button beside the field, or clear the field and save. Values must lie between 0 and 2. Output is more consistent at lower values, for example 0 for repeatable summaries with a local model. A change takes effect with the next request, without a restart. The temperatures for Inquire and speaker identification are fixed and cannot be changed here, and no temperature is sent to models that do not accept one, such as GPT-5.
+
## Understanding the LLM Prompt Structure
The expandable "View Full LLM Prompt Structure" section reveals how your prompt fits into the complete instruction sent to the AI. This technical view shows the system prompt, your custom prompt, and the transcript integration.
diff --git a/src/api/admin.py b/src/api/admin.py
index c85af91f..55defc95 100644
--- a/src/api/admin.py
+++ b/src/api/admin.py
@@ -750,6 +750,54 @@ def admin_get_user_transcription_stats():
# --- Transcript Template Routes ---
+@admin_bp.route('/admin/llm-temperatures', methods=['GET'])
+@login_required
+def admin_get_llm_temperatures():
+ """Effective sampling temperatures, with their source (#411)."""
+ if not current_user.is_admin:
+ return jsonify({'error': 'Unauthorized'}), 403
+ from src.services.llm_settings import temperatures_status
+ return jsonify({'temperatures': temperatures_status()})
+
+
+@admin_bp.route('/admin/llm-temperatures', methods=['POST'])
+@login_required
+def admin_set_llm_temperatures():
+ """Save or clear admin temperatures. Body: {"values": {"summary": 0.2, "chat": null, ...}}.
+
+ A number between 0 and 2 is saved; null or an empty string clears the admin
+ value, so the built-in default applies again.
+ """
+ if not current_user.is_admin:
+ return jsonify({'error': 'Unauthorized'}), 403
+ from src.services.llm_settings import TEMPERATURES, parse_temperature, setting_key, temperatures_status
+ values = (request.get_json(silent=True) or {}).get('values')
+ if not isinstance(values, dict) or not values:
+ return jsonify({'error': 'values must be an object of temperatures'}), 400
+ unknown = [k for k in values if k not in TEMPERATURES]
+ if unknown:
+ return jsonify({'error': f"Unknown temperature: {', '.join(unknown)}"}), 400
+ parsed = {}
+ for kind, raw in values.items():
+ if raw is None or (isinstance(raw, str) and not raw.strip()):
+ parsed[kind] = None
+ continue
+ value = parse_temperature(raw)
+ if value is None or isinstance(raw, bool):
+ return jsonify({'error': f'{kind} temperature must be a number between 0 and 2', 'field': kind}), 400
+ parsed[kind] = value
+ for kind, value in parsed.items():
+ key = setting_key(kind)
+ if value is None:
+ SystemSetting.query.filter_by(key=key).delete()
+ else:
+ SystemSetting.set_setting(key, str(value),
+ description=f'Sampling temperature for {kind} requests (#411)',
+ setting_type='string')
+ db.session.commit()
+ return jsonify({'success': True, 'temperatures': temperatures_status()})
+
+
@admin_bp.route('/admin/settings', methods=['GET'])
@login_required
def admin_get_settings():
diff --git a/src/api/recordings.py b/src/api/recordings.py
index 9ab34519..d47035f6 100644
--- a/src/api/recordings.py
+++ b/src/api/recordings.py
@@ -13,6 +13,7 @@ import threading
import subprocess
import uuid
from datetime import datetime, timedelta, timezone
+from src.services.llm_settings import get_temperature
from src.services.job_queue import job_queue
from flask import Blueprint, render_template, request, redirect, url_for, flash, jsonify, send_file, Response, current_app, make_response
from flask_login import login_required, current_user
@@ -3405,13 +3406,16 @@ Note: This is an incognito recording - no data is stored on the server.
current_app.logger.info(f"[Incognito Chat] User {user_id} sending message")
+ # Resolved here, in the request context, before the response streams.
+ chat_temperature = get_temperature('chat')
+
def generate():
"""Stream the chat response."""
try:
response = chat_client.chat.completions.create(
model=chat_model,
messages=messages,
- temperature=0.7,
+ temperature=chat_temperature,
stream=True
)
@@ -4097,6 +4101,9 @@ Additional context and notes about the meeting:
user_id = current_user.id
app = current_app._get_current_object()
+ # Resolved here, in the request context, before the response streams.
+ chat_temperature = get_temperature('chat')
+
def generate():
# Push app context for entire generator execution
# This is needed because call_chat_completion checks budget which requires db access
@@ -4106,7 +4113,7 @@ Additional context and notes about the meeting:
# Enable streaming with user_id for budget enforcement
stream = call_chat_completion(
messages=messages,
- temperature=0.7,
+ temperature=chat_temperature,
max_tokens=int(os.environ.get("CHAT_MAX_TOKENS", "2000")),
stream=True,
user_id=user_id,
diff --git a/src/services/llm_settings.py b/src/services/llm_settings.py
new file mode 100644
index 00000000..20d10eb5
--- /dev/null
+++ b/src/services/llm_settings.py
@@ -0,0 +1,83 @@
+"""Sampling temperatures for the LLM requests Speakr makes (#411).
+
+Each kind of request has a temperature. The value saved by an administrator in
+the Default Prompts tab (SystemSetting ``llm_temperature_<kind>``) applies when
+it is set; otherwise the built-in default applies, unchanged from earlier
+releases. A saved value that is not a number between 0 and 2 is ignored with a
+warning. Values are read on every request, so a change in the admin settings
+applies to the next request without a restart.
+
+Inquire and speaker identification keep fixed temperatures, tuned per step.
+"""
+
+import logging
+
+logger = logging.getLogger(__name__)
+
+MIN_TEMPERATURE = 0.0
+MAX_TEMPERATURE = 2.0
+
+# kind -> built-in default
+TEMPERATURES = {
+ 'summary': 0.5,
+ 'title': 0.7,
+ 'chat': 0.7,
+ 'event': 0.2,
+}
+
+
+def setting_key(kind):
+ return f'llm_temperature_{kind}'
+
+
+def parse_temperature(value):
+ """A float in [0, 2], or None when the value is empty or invalid."""
+ if value is None:
+ return None
+ if isinstance(value, str):
+ value = value.strip()
+ if not value:
+ return None
+ try:
+ number = float(value)
+ except (TypeError, ValueError):
+ return None
+ if number != number or not (MIN_TEMPERATURE <= number <= MAX_TEMPERATURE): # NaN or out of range
+ return None
+ return number
+
+
+def _admin_value(kind):
+ try:
+ from src.models import SystemSetting
+ setting = SystemSetting.query.filter_by(key=setting_key(kind)).first()
+ return setting.value if setting else None
+ except Exception as e: # no app context or database not ready
+ logger.debug(f"Could not read admin temperature for {kind}: {e}")
+ return None
+
+
+def resolve_temperature(kind):
+ """(value, source) for a kind; source is 'admin' or 'default'."""
+ default = TEMPERATURES[kind]
+ raw_admin = _admin_value(kind)
+ if raw_admin not in (None, ''):
+ value = parse_temperature(raw_admin)
+ if value is not None:
+ return value, 'admin'
+ logger.warning(f"Ignoring admin {kind} temperature {raw_admin!r}: not a number between 0 and 2")
+ return default, 'default'
+
+
+def get_temperature(kind):
+ """The temperature to send with a request of this kind."""
+ return resolve_temperature(kind)[0]
+
+
+def temperatures_status():
+ """Effective value, source and default for every kind."""
+ status = {}
+ for kind, default in TEMPERATURES.items():
+ value, source = resolve_temperature(kind)
+ status[kind] = {'value': value, 'source': source, 'default': default}
+ return status
diff --git a/src/tasks/processing.py b/src/tasks/processing.py
index d3519081..a4335785 100644
--- a/src/tasks/processing.py
+++ b/src/tasks/processing.py
@@ -20,6 +20,7 @@ from datetime import datetime
from flask import current_app
from openai import OpenAI
+from src.services.llm_settings import get_temperature
from src.database import db
from src.models import Recording, Tag, Event, TranscriptChunk, SystemSetting, GroupMembership, RecordingTag, InternalShare, SharedRecordingState, User, NamingTemplate
from src.services.embeddings import process_recording_chunks
@@ -584,13 +585,14 @@ Title:"""
system_message_content += f" Ensure your response is in {user_output_language}."
try:
+ llm_temperature = get_temperature('title') # read before the transaction ends
_end_transaction_before_external_call(recording)
completion = call_llm_completion(
messages=[
{"role": "system", "content": system_message_content},
{"role": "user", "content": prompt_text}
],
- temperature=0.7,
+ temperature=llm_temperature,
# TITLE_MAX_TOKENS lets reasoning-model users (e.g. Kimi K2) raise
# the budget so the model has room for hidden thinking tokens
# before producing the title itself.
@@ -912,13 +914,14 @@ Summarization Instructions:
current_app.logger.debug(f"=== END SUMMARIZATION DEBUG for recording {recording_id} ===")
try:
+ llm_temperature = get_temperature('summary') # read before the transaction ends
_end_transaction_before_external_call(recording)
completion = call_llm_completion(
messages=[
{"role": "system", "content": system_message_content},
{"role": "user", "content": prompt_text}
],
- temperature=0.5,
+ temperature=llm_temperature,
max_tokens=int(os.environ.get("SUMMARY_MAX_TOKENS", "3000")),
user_id=recording.user_id,
operation_type='summarization'
@@ -1182,13 +1185,14 @@ You must respond with valid JSON format only."""
if user_output_language:
system_message_content += f"\n\nLanguage Requirement: You MUST generate ALL event titles, descriptions, and locations in {user_output_language}. This is mandatory."
+ llm_temperature = get_temperature('event') # read before the transaction ends
_end_transaction_before_external_call(recording)
completion = call_llm_completion(
messages=[
{"role": "system", "content": system_message_content},
{"role": "user", "content": event_prompt}
],
- temperature=0.2,
+ temperature=llm_temperature,
response_format={"type": "json_object"},
# EVENT_MAX_TOKENS gives reasoning-model users a knob to raise
# the budget when hidden thinking tokens crowd out the JSON output.
@@ -2831,7 +2835,7 @@ Title:"""
{"role": "system", "content": system_message_content},
{"role": "user", "content": prompt_text}
],
- temperature=0.7,
+ temperature=get_temperature('title'),
# Match the main title-generation path so reasoning-model users
# have a single knob (TITLE_MAX_TOKENS) that covers both flows.
max_tokens=int(os.environ.get("TITLE_MAX_TOKENS", "5000"))
@@ -2906,7 +2910,7 @@ Summarization Instructions:
{"role": "system", "content": system_message_content},
{"role": "user", "content": prompt_text}
],
- temperature=0.5,
+ temperature=get_temperature('summary'),
max_tokens=int(os.environ.get("SUMMARY_MAX_TOKENS", "3000"))
)
diff --git a/static/locales/de.json b/static/locales/de.json
index d69701ac..727abf62 100644
--- a/static/locales/de.json
+++ b/static/locales/de.json
@@ -346,7 +346,20 @@
"title": "Kompatibilität der Stimm-Embeddings"
},
"warning": "Warnung",
- "yes": "Ja"
+ "yes": "Ja",
+ "temperatures": {
+ "heading": "Generierungstemperatur",
+ "description": "Sampling-Temperatur für jede Art von Anfrage an das Sprachmodell, zwischen 0 und 2. Niedrigere Werte ergeben gleichmäßigere Ergebnisse. Bleibt ein Feld leer, gilt der eingebaute Standardwert.",
+ "summary": "Zusammenfassungen",
+ "title": "Titel",
+ "chat": "Chat",
+ "event": "Termine",
+ "reset": "Admin-Wert entfernen",
+ "saved": "Temperaturen gespeichert.",
+ "invalid": "Geben Sie eine Zahl zwischen 0 und 2 ein.",
+ "sourceAdmin": "Aktiv: {{value}} (Admin-Einstellung; Standard {{default}})",
+ "sourceDefault": "Aktiv: {{value}} (Standard)"
+ }
},
"apiTokens": {
"activeTokens": "Aktive Token",
diff --git a/static/locales/en.json b/static/locales/en.json
index f853a15d..b5067d0e 100644
--- a/static/locales/en.json
+++ b/static/locales/en.json
@@ -346,7 +346,20 @@
"title": "Voice embedding compatibility"
},
"warning": "warning",
- "yes": "Yes"
+ "yes": "Yes",
+ "temperatures": {
+ "heading": "Generation temperature",
+ "description": "Sampling temperature for each kind of request to the language model, between 0 and 2. Lower values give more consistent output. Leave a field empty to use the built-in default.",
+ "summary": "Summaries",
+ "title": "Titles",
+ "chat": "Chat",
+ "event": "Events",
+ "reset": "Clear the admin value",
+ "saved": "Temperatures saved.",
+ "invalid": "Enter a number between 0 and 2.",
+ "sourceAdmin": "In use: {{value}} (admin setting; default {{default}})",
+ "sourceDefault": "In use: {{value}} (default)"
+ }
},
"apiTokens": {
"activeTokens": "Active Tokens",
diff --git a/static/locales/es.json b/static/locales/es.json
index b0ec07e1..11349030 100644
--- a/static/locales/es.json
+++ b/static/locales/es.json
@@ -346,7 +346,20 @@
"title": "Compatibilidad de las incrustaciones de voz"
},
"warning": "advertencia",
- "yes": "Sí"
+ "yes": "Sí",
+ "temperatures": {
+ "heading": "Temperatura de generación",
+ "description": "Temperatura de muestreo para cada tipo de solicitud al modelo de lenguaje, entre 0 y 2. Los valores bajos dan resultados más consistentes. Deje un campo vacío para usar el valor predeterminado integrado.",
+ "summary": "Resúmenes",
+ "title": "Títulos",
+ "chat": "Chat",
+ "event": "Eventos",
+ "reset": "Quitar el valor del administrador",
+ "saved": "Temperaturas guardadas.",
+ "invalid": "Introduzca un número entre 0 y 2.",
+ "sourceAdmin": "En uso: {{value}} (ajuste del administrador; predeterminado {{default}})",
+ "sourceDefault": "En uso: {{value}} (predeterminado)"
+ }
},
"apiTokens": {
"activeTokens": "Tokens activos",
diff --git a/static/locales/fr.json b/static/locales/fr.json
index 159100ab..2599f87f 100644
--- a/static/locales/fr.json
+++ b/static/locales/fr.json
@@ -346,7 +346,20 @@
"title": "Compatibilité des empreintes vocales"
},
"warning": "avertissement",
- "yes": "Oui"
+ "yes": "Oui",
+ "temperatures": {
+ "heading": "Température de génération",
+ "description": "Température d’échantillonnage pour chaque type de requête au modèle de langage, entre 0 et 2. Des valeurs plus basses donnent des résultats plus constants. Laissez un champ vide pour utiliser la valeur par défaut intégrée.",
+ "summary": "Résumés",
+ "title": "Titres",
+ "chat": "Chat",
+ "event": "Événements",
+ "reset": "Effacer la valeur de l’administrateur",
+ "saved": "Températures enregistrées.",
+ "invalid": "Saisissez un nombre entre 0 et 2.",
+ "sourceAdmin": "Utilisée : {{value}} (réglage administrateur ; par défaut {{default}})",
+ "sourceDefault": "Utilisée : {{value}} (par défaut)"
+ }
},
"apiTokens": {
"activeTokens": "Jetons actifs",
diff --git a/static/locales/pt-BR.json b/static/locales/pt-BR.json
index 77ff17e4..418e17dd 100644
--- a/static/locales/pt-BR.json
+++ b/static/locales/pt-BR.json
@@ -346,7 +346,20 @@
"title": "Compatibilidade das embeddings de voz"
},
"warning": "aviso",
- "yes": "Sim"
+ "yes": "Sim",
+ "temperatures": {
+ "heading": "Temperatura de geração",
+ "description": "Temperatura de amostragem para cada tipo de solicitação ao modelo de linguagem, entre 0 e 2. Valores menores geram resultados mais consistentes. Deixe um campo vazio para usar o padrão integrado.",
+ "summary": "Resumos",
+ "title": "Títulos",
+ "chat": "Chat",
+ "event": "Eventos",
+ "reset": "Limpar o valor do administrador",
+ "saved": "Temperaturas salvas.",
+ "invalid": "Informe um número entre 0 e 2.",
+ "sourceAdmin": "Em uso: {{value}} (configuração do administrador; padrão {{default}})",
+ "sourceDefault": "Em uso: {{value}} (padrão)"
+ }
},
"apiTokens": {
"activeTokens": "Tokens Ativos",
diff --git a/static/locales/ru.json b/static/locales/ru.json
index 97c69a9f..3919146e 100644
--- a/static/locales/ru.json
+++ b/static/locales/ru.json
@@ -346,7 +346,20 @@
"title": "Совместимость голосовых эмбеддингов"
},
"warning": "предупреждение",
- "yes": "Да"
+ "yes": "Да",
+ "temperatures": {
+ "heading": "Температура генерации",
+ "description": "Температура выборки для каждого типа запросов к языковой модели, от 0 до 2. Более низкие значения дают более стабильный результат. Оставьте поле пустым, чтобы использовать встроенное значение по умолчанию.",
+ "summary": "Резюме",
+ "title": "Заголовки",
+ "chat": "Чат",
+ "event": "События",
+ "reset": "Удалить значение администратора",
+ "saved": "Температуры сохранены.",
+ "invalid": "Введите число от 0 до 2.",
+ "sourceAdmin": "Используется: {{value}} (настройка администратора; по умолчанию {{default}})",
+ "sourceDefault": "Используется: {{value}} (по умолчанию)"
+ }
},
"apiTokens": {
"activeTokens": "Активные токены",
diff --git a/static/locales/zh.json b/static/locales/zh.json
index 5b2e5f72..6f3f0d90 100644
--- a/static/locales/zh.json
+++ b/static/locales/zh.json
@@ -346,7 +346,20 @@
"title": "声纹特征兼容性"
},
"warning": "警告",
- "yes": "是"
+ "yes": "是",
+ "temperatures": {
+ "heading": "生成温度",
+ "description": "向语言模型发送各类请求时使用的采样温度,范围为 0 到 2。数值越低,输出越稳定。留空则使用内置默认值。",
+ "summary": "摘要",
+ "title": "标题",
+ "chat": "聊天",
+ "event": "事件",
+ "reset": "清除管理员设置值",
+ "saved": "温度已保存。",
+ "invalid": "请输入 0 到 2 之间的数字。",
+ "sourceAdmin": "当前:{{value}}(管理员设置;默认 {{default}})",
+ "sourceDefault": "当前:{{value}}(默认)"
+ }
},
"apiTokens": {
"activeTokens": "活跃令牌",
diff --git a/templates/admin.html b/templates/admin.html
index d5a2ad44..2b40faea 100644
--- a/templates/admin.html
+++ b/templates/admin.html
@@ -1011,6 +1011,42 @@
</div>
</div>
+ <!-- Generation temperatures (#411): admin value, else the built-in
+ default, per kind of request. -->
+ <div class="mt-6 bg-[var(--bg-tertiary)] p-6 rounded-lg border border-[var(--border-primary)]">
+ <h4 class="text-md font-medium text-[var(--text-secondary)] mb-1" v-text="t('adminDashboard.temperatures.heading')"></h4>
+ <p class="text-sm text-[var(--text-muted)] mb-4" v-text="t('adminDashboard.temperatures.description')"></p>
+ <div class="grid grid-cols-2 lg:grid-cols-4 gap-3">
+ <div v-for="kind in temperatureKinds" :key="kind">
+ <label :for="'temperature-' + kind" class="block text-sm font-medium text-[var(--text-secondary)] mb-1" v-text="t('adminDashboard.temperatures.' + kind)"></label>
+ <div class="flex items-center gap-1">
+ <input :id="'temperature-' + kind" type="number" min="0" max="2" step="0.1"
+ v-model="temperatureInputs[kind]"
+ :placeholder="temperatureStatus[kind] ? String(temperatureStatus[kind].value) : ''"
+ class="w-full px-2 py-1 text-sm border border-[var(--border-secondary)] rounded-md bg-[var(--bg-input)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-[var(--border-focus)]">
+ <button v-if="temperatureStatus[kind] && temperatureStatus[kind].source === 'admin'"
+ type="button" @click="resetTemperature(kind)"
+ class="btn btn--icon btn--sm" :title="t('adminDashboard.temperatures.reset')" :aria-label="t('adminDashboard.temperatures.reset')">
+ <i class="fas fa-undo"></i>
+ </button>
+ </div>
+ <p v-if="temperatureStatus[kind]" class="mt-1 text-xs text-[var(--text-muted)]"
+ v-text="temperatureSourceText(kind)"></p>
+ </div>
+ </div>
+ <div class="flex justify-end items-center mt-4">
+ <button @click="saveTemperatures" :disabled="isSavingTemperatures" class="btn btn--primary">
+ <i v-if="isSavingTemperatures" class="fas fa-spinner fa-spin mr-2"></i>
+ <i v-else class="fas fa-save mr-2"></i>
+ ${ isSavingTemperatures ? t('adminDashboard.saving') : t('buttons.saveChanges') }
+ </button>
+ </div>
+ <div v-if="temperatureSaveMessage" class="mt-4">
+ <div :class="temperatureSaveError ? 'bg-[var(--bg-danger-light)] text-[var(--text-danger-strong)]' : 'bg-[var(--bg-success-light)] text-[var(--text-success-strong)]'"
+ class="p-3 rounded-md text-sm" v-text="temperatureSaveMessage"></div>
+ </div>
+ </div>
+
<!-- Contextual speaker labelling: guidance used when a recording has
no voice embeddings and speakers are named from the transcript,
constrained to the user's saved profiles. Only this guidance is
@@ -2196,6 +2232,10 @@ Summarization Instructions:
// Managed by the dedicated contextual-speaker-prompt editor
// in the Default Prompts tab.
'admin_default_contextual_speaker_prompt',
+ // Managed by the generation temperature editor in the
+ // Default Prompts tab (#411).
+ 'llm_temperature_summary', 'llm_temperature_title',
+ 'llm_temperature_chat', 'llm_temperature_event',
]);
const displayedSettings = computed(() =>
(settings.value || []).filter(s => !HIDDEN_SETTING_KEYS.has(s.key))
@@ -3614,6 +3654,7 @@ Summarization Instructions:
contextualSpeakerPrompt.value = cspSetting ? (cspSetting.value || '') : '';
const titleSetting = settingsData.find(s => s.key === 'admin_default_title_prompt');
defaultTitlePrompt.value = titleSetting ? (titleSetting.value || '') : '';
+ loadTemperatures();
} catch (error) {
console.error('Error loading default prompt:', error);
promptSaveMessage.value = t('adminDashboard.errors.failedToLoadPrompt');
@@ -3713,6 +3754,84 @@ Summarization Instructions:
promptSaveError.value = false;
};
+ // ---- Generation temperatures (#411) ----
+ const temperatureKinds = ['summary', 'title', 'chat', 'event'];
+ const temperatureStatus = ref({});
+ const temperatureInputs = ref({ summary: '', title: '', chat: '', event: '' });
+ const isSavingTemperatures = ref(false);
+ const temperatureSaveMessage = ref('');
+ const temperatureSaveError = ref(false);
+
+ const applyTemperatureStatus = (status) => {
+ temperatureStatus.value = status || {};
+ const inputs = {};
+ temperatureKinds.forEach(kind => {
+ const st = temperatureStatus.value[kind];
+ inputs[kind] = st && st.source === 'admin' ? String(st.value) : '';
+ });
+ temperatureInputs.value = inputs;
+ };
+
+ const loadTemperatures = async () => {
+ try {
+ const response = await fetch('/admin/llm-temperatures');
+ if (!response.ok) return;
+ const data = await response.json();
+ applyTemperatureStatus(data.temperatures);
+ } catch (error) {
+ console.error('Error loading temperatures:', error);
+ }
+ };
+
+ const temperatureSourceText = (kind) => {
+ const st = temperatureStatus.value[kind];
+ if (!st) return '';
+ const key = 'adminDashboard.temperatures.source' + st.source.charAt(0).toUpperCase() + st.source.slice(1);
+ return t(key, { value: st.value, default: st.default });
+ };
+
+ const postTemperatures = async (values) => {
+ isSavingTemperatures.value = true;
+ temperatureSaveMessage.value = '';
+ temperatureSaveError.value = false;
+ try {
+ const response = await fetch('/admin/llm-temperatures', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ 'X-CSRFToken': document.querySelector('meta[name="csrf-token"]').content
+ },
+ body: JSON.stringify({ values })
+ });
+ const data = await response.json().catch(() => ({}));
+ if (!response.ok) throw new Error(data.error || 'save failed');
+ applyTemperatureStatus(data.temperatures);
+ temperatureSaveMessage.value = t('adminDashboard.temperatures.saved');
+ setTimeout(() => { temperatureSaveMessage.value = ''; }, 3000);
+ } catch (error) {
+ console.error('Error saving temperatures:', error);
+ temperatureSaveMessage.value = t('adminDashboard.temperatures.invalid');
+ temperatureSaveError.value = true;
+ } finally {
+ isSavingTemperatures.value = false;
+ }
+ };
+
+ const saveTemperatures = () => {
+ const values = {};
+ temperatureKinds.forEach(kind => {
+ const raw = temperatureInputs.value[kind];
+ values[kind] = raw === '' || raw === null || raw === undefined ? null : raw;
+ });
+ return postTemperatures(values);
+ };
+
+ const resetTemperature = (kind) => {
+ const values = {};
+ values[kind] = null;
+ return postTemperatures(values);
+ };
+
const saveDefaultTitlePrompt = async () => {
isSavingTitlePrompt.value = true;
titlePromptSaveMessage.value = '';
@@ -4088,6 +4207,8 @@ Summarization Instructions:
hintsSaveError,
contextualSpeakerPrompt,
defaultTitlePrompt, isSavingTitlePrompt, titlePromptSaveMessage, titlePromptSaveError,
+ temperatureKinds, temperatureStatus, temperatureInputs, isSavingTemperatures, temperatureSaveMessage, temperatureSaveError,
+ temperatureSourceText, saveTemperatures, resetTemperature,
isSavingContextualPrompt,
contextualPromptSaveMessage,
contextualPromptSaveError,
diff --git a/tests/test_llm_temperatures.py b/tests/test_llm_temperatures.py
new file mode 100644
index 00000000..d4bf9915
--- /dev/null
+++ b/tests/test_llm_temperatures.py
@@ -0,0 +1,207 @@
+"""Configurable sampling temperatures (#411).
+
+Each kind of request (summary, title, chat, event) is sent with the admin value
+from the Default Prompts tab, else the built-in default. An invalid saved value
+is ignored and the default is used.
+"""
+
+import ast
+import os
+import sys
+import uuid
+from unittest.mock import patch, MagicMock
+
+import pytest
+from flask import g
+from flask.testing import FlaskClient
+
+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+
+from src.app import app, db
+from src.models import User, SystemSetting
+from src.services import llm_settings as ls
+
+app.config["WTF_CSRF_ENABLED"] = False
+ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+
+
+class _Client(FlaskClient):
+ def open(self, *args, **kwargs):
+ g.pop('_login_user', None)
+ return super().open(*args, **kwargs)
+
+
+def ctx():
+ with app.app_context():
+ yield
+ db.session.rollback()
+ for kind in ls.TEMPERATURES:
+ SystemSetting.query.filter_by(key=ls.setting_key(kind)).delete()
+ db.session.commit()
+
+
+def _admin_value(kind, value):
+ SystemSetting.set_setting(ls.setting_key(kind), value, setting_type='string')
+ db.session.commit()
+
+
+# ---------------------------------------------------------------- resolution
+
+def test_defaults_are_unchanged_from_earlier_releases(ctx):
+ assert {k: ls.get_temperature(k) for k in ls.TEMPERATURES} == {'summary': 0.5, 'title': 0.7, 'chat': 0.7, 'event': 0.2}
+ assert ls.resolve_temperature('summary') == (0.5, 'default')
+
+
+def test_admin_value_overrides_the_default(ctx):
+ _admin_value('chat', '0.3')
+ assert ls.resolve_temperature('chat') == (0.3, 'admin')
+ assert ls.resolve_temperature('title') == (0.7, 'default')
+
+
+def test_environment_variables_are_not_read(ctx, monkeypatch):
+ monkeypatch.setenv('SUMMARY_TEMPERATURE', '0')
+ assert ls.resolve_temperature('summary') == (0.5, 'default')
+
+
[email protected]('raw', ['abc', '-0.1', '2.5', 'nan', ' ', ''])
+def test_invalid_admin_values_fall_back_to_the_default(ctx, raw):
+ _admin_value('event', raw)
+ assert ls.resolve_temperature('event') == (0.2, 'default')
+
+
[email protected]('raw,expected', [('0', 0.0), ('2', 2.0), ('1.25', 1.25), (0.3, 0.3), (' 0.5 ', 0.5)])
+def test_bounds_are_inclusive(raw, expected):
+ assert ls.parse_temperature(raw) == expected
+
+
+def test_an_admin_change_applies_to_the_next_request(ctx):
+ assert ls.get_temperature('summary') == 0.5
+ _admin_value('summary', '0.1')
+ assert ls.get_temperature('summary') == 0.1
+
+
+# ---------------------------------------------------------------- call sites
+
+CALL_SITES = {
+ os.path.join('src', 'tasks', 'processing.py'): {
+ '_generate_ai_title': 'title',
+ 'generate_summary_only_task': 'summary',
+ 'extract_events_from_transcript': 'event',
+ '_generate_incognito_title': 'title',
+ 'generate_incognito_summary': 'summary',
+ },
+ os.path.join('src', 'api', 'recordings.py'): {
+ 'chat_incognito': 'chat',
+ 'chat_with_transcription': 'chat',
+ },
+}
+
+
+def _temperature_kinds(func):
+ kinds = []
+ for node in ast.walk(func):
+ if isinstance(node, ast.Call) and getattr(node.func, 'id', None) == 'get_temperature':
+ kinds.append(node.args[0].value)
+ return kinds
+
+
[email protected]('path', list(CALL_SITES))
+def test_every_call_site_sends_its_configured_temperature(path):
+ tree = ast.parse(open(os.path.join(ROOT, path), encoding='utf-8').read())
+ funcs = {n.name: n for n in ast.walk(tree) if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef))}
+ for name, kind in CALL_SITES[path].items():
+ assert _temperature_kinds(funcs[name]) == [kind], name
+ passed = [kw.value for n in ast.walk(funcs[name]) if isinstance(n, ast.Call)
+ for kw in n.keywords if kw.arg == 'temperature']
+ assert passed, f"{name} sends no temperature"
+ for value in passed:
+ # either get_temperature(kind) inline, or a variable assigned from it
+ ok = (isinstance(value, ast.Call) and getattr(value.func, 'id', None) == 'get_temperature') or (
+ isinstance(value, ast.Name) and any(
+ isinstance(a, ast.Assign) and any(getattr(t, 'id', None) == value.id for t in a.targets)
+ and isinstance(a.value, ast.Call) and getattr(a.value.func, 'id', None) == 'get_temperature'
+ for a in ast.walk(funcs[name])))
+ assert ok, f"{name} sends temperature={ast.unparse(value)}"
+ literal = [n.lineno for n in ast.walk(tree)
+ if isinstance(n, ast.keyword) and n.arg == 'temperature' and isinstance(n.value, ast.Constant)]
+ assert literal == [], f"hard-coded temperature in {path} at lines {literal}"
+
+
+def test_incognito_title_and_summary_use_the_resolved_values(ctx):
+ from src.tasks import processing
+ _admin_value('title', '0.15')
+ _admin_value('summary', '0')
+ completion = MagicMock()
+ completion.choices = [MagicMock(message=MagicMock(content='A title'))]
+ seen = []
+
+ def fake(*args, **kwargs):
+ seen.append(kwargs.get('temperature'))
+ return completion
+
+ fake_client = MagicMock()
+ fake_client.chat.completions.create.side_effect = fake
+ with patch.object(processing, 'client', fake_client):
+ processing._generate_incognito_title('Some transcript text about budgets.')
+ processing.generate_incognito_summary('Some transcript text about budgets.')
+ assert seen == [0.15, 0.0]
+
+
+# ---------------------------------------------------------------- admin API
+
+def _client(user):
+ c = _Client(app, app.response_class)
+ with c.session_transaction() as sess:
+ sess["_user_id"] = str(user.id)
+ sess["_fresh"] = True
+ return c
+
+
+def users(ctx):
+ s = uuid.uuid4().hex[:8]
+ admin = User(username=f"tA_{s}", email=f"ta_{s}@local.test", password="x", is_admin=True)
+ plain = User(username=f"tU_{s}", email=f"tu_{s}@local.test", password="x")
+ db.session.add_all([admin, plain])
+ db.session.commit()
+ yield admin, plain
+ db.session.delete(db.session.get(User, admin.id))
+ db.session.delete(db.session.get(User, plain.id))
+ db.session.commit()
+
+
+def test_api_requires_an_admin(users):
+ _, plain = users
+ c = _client(plain)
+ assert c.get('/admin/llm-temperatures').status_code == 403
+ assert c.post('/admin/llm-temperatures', json={'values': {'summary': 0}}).status_code == 403
+
+
+def test_api_reports_values_and_sources(users):
+ admin, _ = users
+ _admin_value('chat', '0.9')
+ data = _client(admin).get('/admin/llm-temperatures').get_json()['temperatures']
+ assert data['chat'] == {'value': 0.9, 'source': 'admin', 'default': 0.7}
+ assert data['summary'] == {'value': 0.5, 'source': 'default', 'default': 0.5}
+
+
+def test_api_saves_and_clears(users):
+ admin, _ = users
+ c = _client(admin)
+ r = c.post('/admin/llm-temperatures', json={'values': {'summary': 0, 'event': '0.1'}})
+ assert r.status_code == 200, r.get_data(as_text=True)
+ assert ls.resolve_temperature('summary') == (0.0, 'admin')
+ assert ls.resolve_temperature('event') == (0.1, 'admin')
+ r = c.post('/admin/llm-temperatures', json={'values': {'summary': None}})
+ assert r.status_code == 200
+ assert ls.resolve_temperature('summary') == (0.5, 'default')
+ assert ls.resolve_temperature('event') == (0.1, 'admin')
+
+
[email protected]('values', [{'summary': 3}, {'summary': 'hot'}, {'summary': True}, {'nope': 0.1}, {}])
+def test_api_rejects_invalid_input_and_saves_nothing(users, values):
+ admin, _ = users
+ r = _client(admin).post('/admin/llm-temperatures', json={'values': values})
+ assert r.status_code == 400
+ assert SystemSetting.query.filter(SystemSetting.key.like('llm_temperature_%')).count() == 0