From cfb3b7d418df3e82b97abfc86799a4cb9d6fcafa Mon Sep 17 00:00:00 2001 From: livdebus Date: Thu, 12 Nov 2026 20:10:14 +0100 Subject: [PATCH] fix(voice): route Onyx 5.0.3 STT/TTS to LocalAI Whisper endpoint Replace the broken sed-based entrypoint from 4.8.2 with a runtime Python patch script for Onyx 5.0.3 voice module compatibility. Background ---------- Onyx 5.0.3 refactored the voice subsystem to use raw aiohttp instead of the OpenAI Python SDK. The old /v1 suffix in our LocalAI base URL now causes a double-prefix (https://.../v1/v1/audio/...) and 404s. Additionally, line numbers and constants shifted, making the previous sed patches fail silently. Changes ------- * api_server: override CMD with a startup sequence that runs patch_voice.py before uvicorn. * Add backend/scripts/patch_voice.py that patches: - factory.py: fallback api_base, stt_model and api_key when no voice provider is configured in the Onyx admin UI. - providers/openai.py: DEFAULT_OPENAI_API_BASE (without /v1), realtime model constants, and optional chunked-STT language injection (WHISPER_LANGUAGE=de). * Mount patch script read-only into /app/scripts/. * Env: WHISPER_API_BASE now points to the origin only (https://aido-whisper-ch.livtec.cloud), leaving /v1 path assembly to Onyx code. * Env: WHISPER_MODEL=whisper-1, WHISPER_LANGUAGE=de. Migration --------- The previous entrypoint sed block is removed. No admin UI voice provider configuration is required; the patch is fully transparent. Refs: aido-whisper-ch deployment, Onyx 5.0.3 upgrade --- docker-compose.yml | 165 ++++----------------------------------------- patch_voice.py | 90 +++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 152 deletions(-) create mode 100644 patch_voice.py diff --git a/docker-compose.yml b/docker-compose.yml index 0e402b3..4cb7c3b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,158 +1,14 @@ services: api_server: image: ${ONYX_BACKEND_IMAGE:-onyxdotapp/onyx-backend:${IMAGE_TAG:-latest}} - entrypoint: | - sh -c " - python3 << 'PYEOF' - import os, sys - - def read_file(path): - with open(path, \"r\") as f: - return f.read() - - def write_file(path, content): - with open(path, \"w\") as f: - f.write(content) - - # --- 1. Clean up corrupted factory.py --- - factory_path = \"/app/onyx/voice/factory.py\" - if os.path.exists(factory_path): - src = read_file(factory_path) - - # Fix corrupted api_base line (remove repeated or chains) - if 'api_base = provider.api_base or \\\"https://aido-whisper-ch.livtec.cloud/v1\\\"' in src: - src = src.replace( - 'api_base = provider.api_base or \\\"https://aido-whisper-ch.livtec.cloud/v1\\\" or \\\"https://aido-whisper-ch.livtec.cloud/v1\\\" or \\\"https://aido-whisper-ch.livtec.cloud/v1\\\"', - 'api_base = provider.api_base or \\\"https://aido-whisper-ch.livtec.cloud/v1\\\"' - ) - # Also handle case where it ran twice with different corruption levels - src = src.replace( - 'api_base = provider.api_base or \\\"https://aido-whisper-ch.livtec.cloud/v1\\\" or \\\"https://aido-whisper-ch.livtec.cloud/v1\\\"', - 'api_base = provider.api_base or \\\"https://aido-whisper-ch.livtec.cloud/v1\\\"' - ) - print(\"[OK] factory.py api_base cleaned up\") - - # Ensure api_base fallback is present (idempotent) - if 'api_base = provider.api_base or \\\"https://aido-whisper-ch.livtec.cloud/v1\\\"' not in src: - if 'api_base = provider.api_base' in src: - src = src.replace( - 'api_base = provider.api_base', - 'api_base = provider.api_base or \\\"https://aido-whisper-ch.livtec.cloud/v1\\\"' - ) - print(\"[OK] factory.py api_base fallback applied\") - else: - print(\"[WARN] factory.py: api_base pattern not found\") - else: - print(\"[OK] factory.py api_base already correct\") - - # Fix corrupted stt_model line - if 'stt_model = provider.stt_model or \\\"whisper-1\\\" or \\\"whisper-1\\\"' in src: - src = src.replace( - 'stt_model = provider.stt_model or \\\"whisper-1\\\" or \\\"whisper-1\\\"', - 'stt_model = provider.stt_model or \\\"whisper-1\\\"' - ) - print(\"[OK] factory.py stt_model cleaned up\") - - # Ensure stt_model fallback is present (idempotent) - if 'stt_model = provider.stt_model or \\\"whisper-1\\\"' not in src: - if 'stt_model = provider.stt_model' in src: - src = src.replace( - 'stt_model = provider.stt_model', - 'stt_model = provider.stt_model or \\\"whisper-1\\\"' - ) - print(\"[OK] factory.py stt_model fallback applied\") - else: - print(\"[WARN] factory.py: stt_model pattern not found\") - else: - print(\"[OK] factory.py stt_model already correct\") - - write_file(factory_path, src) - else: - print(\"[FATAL] factory.py not found\") - sys.exit(1) - - # --- 2. Patch openai.py --- - openai_path = \"/app/onyx/voice/providers/openai.py\" - if not os.path.exists(openai_path): - print(\"[FATAL] openai.py not found\") - sys.exit(1) - - src = read_file(openai_path) - - # 2a. Ensure DEFAULT_OPENAI_API_BASE points to your endpoint (idempotent) - if 'DEFAULT_OPENAI_API_BASE = \\\"https://api.openai.com\\\"' in src: - src = src.replace( - 'DEFAULT_OPENAI_API_BASE = \\\"https://api.openai.com\\\"', - 'DEFAULT_OPENAI_API_BASE = \\\"https://aido-whisper-ch.livtec.cloud/v1\\\"' - ) - print(\"[OK] openai.py DEFAULT_OPENAI_API_BASE patched\") - else: - print(\"[OK] openai.py DEFAULT_OPENAI_API_BASE already correct or upstream changed\") - - # 2b. Ensure realtime models are whisper-1 (idempotent) - if 'OPENAI_REALTIME_SESSION_MODEL = \\\"gpt-realtime-1.5\\\"' in src: - src = src.replace( - 'OPENAI_REALTIME_SESSION_MODEL = \\\"gpt-realtime-1.5\\\"', - 'OPENAI_REALTIME_SESSION_MODEL = \\\"whisper-1\\\"' - ) - print(\"[OK] openai.py REALTIME_SESSION_MODEL patched\") - else: - print(\"[OK] openai.py REALTIME_SESSION_MODEL already correct or upstream changed\") - - if 'OPENAI_REALTIME_STT_MODEL = \\\"gpt-realtime-whisper\\\"' in src: - src = src.replace( - 'OPENAI_REALTIME_STT_MODEL = \\\"gpt-realtime-whisper\\\"', - 'OPENAI_REALTIME_STT_MODEL = \\\"whisper-1\\\"' - ) - print(\"[OK] openai.py REALTIME_STT_MODEL patched\") - else: - print(\"[OK] openai.py REALTIME_STT_MODEL already correct or upstream changed\") - - # 2c. Inject language parameter into SDK transcribe call (THE CRITICAL FIX) - if \"WHISPER_LANGUAGE\" not in src: - # Add import os near top if missing - lines = src.split(\"\\n\") - import_idx = next((i for i, l in enumerate(lines) if l.startswith(\"import \") or l.startswith(\"from \")), 0) - if not any(\"import os\" in l for l in lines[:import_idx+5]): - lines.insert(import_idx, \"import os\") - print(\"[OK] openai.py added import os\") - - # Rejoin and find the transcribe method's create() call - src = \"\\n\".join(lines) - - # Pattern: the SDK call inside transcribe() - old_pattern = ''' response = await client.audio.transcriptions.create( - model=self.stt_model, - file=audio_file, - )''' - - new_pattern = ''' response = await client.audio.transcriptions.create( - model=self.stt_model, - file=audio_file, - language=os.environ.get(\"WHISPER_LANGUAGE\", \"de\"), - )''' - - if old_pattern in src: - src = src.replace(old_pattern, new_pattern) - print(\"[OK] openai.py language parameter injected into transcribe()\") - else: - print(\"[WARN] openai.py: could not find exact transcribe() SDK call pattern to inject language\") - print(\"[WARN] STT may default to English auto-detect instead of German\") - else: - print(\"[OK] openai.py language already present\") - - write_file(openai_path, src) - print(\"[DONE] All patches applied successfully\") - - PYEOF - - # Run migrations and start server - alembic upgrade head && echo \"Starting Onyx Api Server\" && exec uvicorn onyx.main:app --host 0.0.0.0 --port 8080 + command: > + /bin/sh -ce " + set -e; + python3 /app/scripts/patch_voice.py; + alembic upgrade head; + echo 'Starting Onyx API Server'; + exec uvicorn onyx.main:app --host 0.0.0.0 --port 8080; " - #command: > - # /bin/sh -c "alembic upgrade head && echo \"Starting Onyx Api Server\" && - # uvicorn onyx.main:app --host 0.0.0.0 --port 8080" - # Check env.template and copy to .env for env vars env_file: - path: .env required: false @@ -190,8 +46,12 @@ services: # Onyx Craft configuration (disabled by default, set ENABLE_CRAFT=true in .env to enable) # Use --include-craft with install script, or manually set in .env file - ENABLE_CRAFT=${ENABLE_CRAFT:-false} - - VOICE_DISABLE_STREAMING_STT=true + # ── LocalAI Whisper overrides ── + - WHISPER_API_BASE=https://aido-whisper-ch.livtec.cloud + - WHISPER_MODEL=whisper-1 - WHISPER_LANGUAGE=de + # Kept for forward-compat, but inert in 5.0.3 voice source + - VOICE_DISABLE_STREAMING_STT=true # PRODUCTION: Uncomment the line below to use if IAM_AUTH is true and you are using iam auth for postgres # volumes: # - ./bundle.pem:/app/bundle.pem:ro @@ -222,6 +82,7 @@ services: - ./api_server_logs:/var/log/onyx # Shared volume for persistent document storage (Craft file-system mode) - ./file-system:/app/file-system + - ./patch_voice.py:/app/scripts/patch_voice.py:ro background: image: ${ONYX_BACKEND_IMAGE:-onyxdotapp/onyx-backend:${IMAGE_TAG:-latest}} diff --git a/patch_voice.py b/patch_voice.py new file mode 100644 index 0000000..133ec89 --- /dev/null +++ b/patch_voice.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python3 +""" +Patch Onyx 5.0.3 voice module to use a LocalAI Whisper endpoint. +Idempotent: safe to run on every container start. +""" +import os + +# ── Configuration from environment ─────────────────────────────────── +LOCALAI_BASE = os.environ.get("WHISPER_API_BASE", "https://aido-whisper-ch.livtec.cloud").rstrip("/") +LOCALAI_MODEL = os.environ.get("WHISPER_MODEL", "whisper-1") +LOCALAI_LANG = os.environ.get("WHISPER_LANGUAGE", "de") + +FACTORY = "/app/onyx/voice/factory.py" +OPENAI = "/app/onyx/voice/providers/openai.py" + +def patch(path, replacements): + with open(path, "r", encoding="utf-8") as f: + content = f.read() + changed = False + for old, new in replacements: + if old in content: + content = content.replace(old, new, 1) + changed = True + print(f"[PATCH] {path}: {old[:60]}...") + else: + print(f"[SKIP ] {path}: {old[:60]}...") + if changed: + with open(path, "w", encoding="utf-8") as f: + f.write(content) + return changed + +# ── 1. factory.py: fallbacks when no voice provider is saved in UI ── +patch(FACTORY, [ + ( + "api_base = provider.api_base", + f"api_base = provider.api_base or '{LOCALAI_BASE}'", + ), + ( + "stt_model = provider.stt_model", + f"stt_model = provider.stt_model or '{LOCALAI_MODEL}'", + ), + ( + "api_key = _extract_sensitive_string(provider.api_key)", + "api_key = _extract_sensitive_string(provider.api_key) or 'dummy-localai-key'", + ), +]) + +# ── 2. openai.py: hardcoded defaults ──────────────────────────────── +patch(OPENAI, [ + ( + 'DEFAULT_OPENAI_API_BASE = "https://api.openai.com"', + f'DEFAULT_OPENAI_API_BASE = "{LOCALAI_BASE}"', + ), + ( + 'OPENAI_REALTIME_SESSION_MODEL = "gpt-realtime-1.5"', + f'OPENAI_REALTIME_SESSION_MODEL = "{LOCALAI_MODEL}"', + ), + ( + 'OPENAI_REALTIME_STT_MODEL = "gpt-realtime-whisper"', + f'OPENAI_REALTIME_STT_MODEL = "{LOCALAI_MODEL}"', + ), +]) + +# ── 3. Best-effort language injection (optional for LocalAI) ───────── +with open(OPENAI, "r", encoding="utf-8") as f: + lines = f.readlines() + +if not any("WHISPER_LANGUAGE" in ln for ln in lines): + if not any(ln.startswith("import os") for ln in lines): + lines.insert(0, "import os\n") + + injected = False + for i, line in enumerate(lines): + if "add_field('model'" in line or 'add_field("model"' in line: + indent = line[: len(line) - len(line.lstrip())] + lines.insert( + i + 1, + indent + f"form.add_field('language', os.environ.get('WHISPER_LANGUAGE', '{LOCALAI_LANG}'))\n", + ) + injected = True + break + + if injected: + with open(OPENAI, "w", encoding="utf-8") as f: + f.writelines(lines) + print(f"[PATCH] {OPENAI}: injected language field") + else: + print(f"[WARN ] {OPENAI}: chunked STT language injection skipped (likely auto-detect)") + +print("[DONE ] Voice patches applied.") \ No newline at end of file