Skip to content

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

Python

API v1 chat uses the admin chat temperature and the user's output language; /users/me reports unset auto-summarization as enabled (#412 audit S8, S10)

Murtaza Nasirb2694a6cc873
diff --git a/src/api/api_v1.py b/src/api/api_v1.py
index 52b4e4a1..33ace045 100644
--- a/src/api/api_v1.py
+++ b/src/api/api_v1.py
@@ -791,7 +791,8 @@ def get_current_user():
'extract_events': bool(current_user.extract_events),
'auto_speaker_labelling': bool(current_user.auto_speaker_labelling),
'auto_speaker_labelling_threshold': current_user.auto_speaker_labelling_threshold,
- 'auto_summarization': bool(current_user.auto_summarization),
+ # None means enabled, as in processing (#412 audit S10).
+ 'auto_summarization': current_user.auto_summarization is not False,
'show_timestamps_simple_view': bool(current_user.show_timestamps_simple_view),
'editor_autosave': bool(current_user.editor_autosave),
'notify_email_on_completion': bool(current_user.notify_email_on_completion),
@@ -2514,6 +2515,9 @@ Transcript:
Notes: {recording.notes or 'None'}
"""
+ # Same output language as web chat (#412 audit S8).
+ if current_user.output_language:
+ system_prompt += f"\nPlease provide all your responses in {current_user.output_language}.\n"
# Build messages
messages = [{"role": "system", "content": system_prompt}]
@@ -2521,7 +2525,9 @@ Notes: {recording.notes or 'None'}
messages.append({"role": "user", "content": user_message})
try:
- completion = call_chat_completion(messages, user_id=current_user.id)
+ # Same admin chat temperature as web chat (#412 audit S8).
+ from src.services.llm_settings import get_temperature
+ completion = call_chat_completion(messages, temperature=get_temperature('chat'), user_id=current_user.id)
reply = completion.choices[0].message.content
return jsonify({
diff --git a/tests/test_api_v1_chat_settings.py b/tests/test_api_v1_chat_settings.py
new file mode 100644
index 00000000..626a7022
--- /dev/null
+++ b/tests/test_api_v1_chat_settings.py
@@ -0,0 +1,80 @@
+"""API v1 chat and /users/me follow the same settings as the web app (#412 audit).
+
+S8: API v1 chat used a fixed 0.7 temperature and ignored the user's output
+language; web chat uses the admin chat temperature and the output language.
+S10: /users/me reported auto_summarization None (never set, which processing
+treats as enabled) as False.
+
+SHARED-DB: the user, recording, token and setting are removed afterwards.
+"""
+
+import os
+import secrets
+import sys
+from unittest.mock import MagicMock, patch
+
+import pytest
+
+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+
+from src.app import app, db
+from src.models import APIToken, Recording, SystemSetting, User
+from src.services import llm_settings as ls
+from src.utils.token_auth import hash_token
+
+
+def setup():
+ with app.app_context():
+ name = f"v1chat_{secrets.token_hex(4)}"
+ u = User(username=name, email=f"{name}@local.test", password="x", output_language="German")
+ db.session.add(u)
+ db.session.commit()
+ r = Recording(user_id=u.id, title="Sync", status="COMPLETED", original_filename="s.wav",
+ transcription="Speaker 1: We move the launch to next quarter.")
+ plain = f"test-token-{secrets.token_urlsafe(16)}"
+ t = APIToken(user_id=u.id, token_hash=hash_token(plain), name="v1chat")
+ db.session.add_all([r, t])
+ db.session.commit()
+ old = SystemSetting.get_setting(ls.setting_key("chat"), None)
+ SystemSetting.set_setting(ls.setting_key("chat"), "0.2", setting_type="string")
+ db.session.commit()
+ ids = (u.id, r.id, t.id)
+ yield ids, plain
+ with app.app_context():
+ if old is None:
+ SystemSetting.query.filter_by(key=ls.setting_key("chat")).delete()
+ else:
+ SystemSetting.set_setting(ls.setting_key("chat"), old, setting_type="string")
+ for model, oid in zip((APIToken, Recording, User), reversed(ids)):
+ obj = db.session.get(model, oid)
+ if obj is not None:
+ db.session.delete(obj)
+ db.session.commit()
+
+
+def test_v1_chat_uses_admin_temperature_and_output_language(setup):
+ (uid, rid, _), plain = setup
+ reply = MagicMock()
+ reply.choices = [MagicMock(message=MagicMock(content="Ja."))]
+ with patch("src.services.llm.chat_client", object()), \
+ patch("src.services.llm.call_chat_completion", return_value=reply) as call:
+ with app.test_client() as c:
+ resp = c.post(f"/api/v1/recordings/{rid}/chat", json={"message": "When is the launch?"},
+ headers={"Authorization": f"Bearer {plain}"})
+ assert resp.status_code == 200, resp.get_data(as_text=True)[:300]
+ messages = call.call_args.args[0]
+ assert call.call_args.kwargs["temperature"] == 0.2
+ assert "Please provide all your responses in German." in messages[0]["content"]
+
+
+def test_users_me_reports_unset_auto_summarization_as_enabled(setup):
+ (uid, _, _), plain = setup
+ with app.app_context():
+ u = db.session.get(User, uid)
+ u.auto_summarization = None
+ db.session.commit()
+ with app.test_client() as c:
+ body = c.get("/api/v1/users/me", headers={"Authorization": f"Bearer {plain}"}).get_json()
+ prefs = body.get("preferences", body)
+ assert prefs["auto_summarization"] is True