From f51a35732ca87594f8a453626024033e55fbd1bd Mon Sep 17 00:00:00 2001 From: livdebus Date: Thu, 12 Nov 2026 19:58:27 +0100 Subject: [PATCH] fix(api_server): update voice STT patch for Onyx 5.0.3 image layout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous Python-heredoc patch (written for 4.8.2) broke silently on 5.0.3 because: - The Docker image installs the backend at a different path than /app/onyx/ (repo restructured to backend/onyx/ in 5.0.0). Hardcoded paths caused every patch to no-op. - 5.0.3 switched from the OpenAI Python SDK (which expects /v1 in base_url) to raw aiohttp that appends /v1/audio/... to the base URL itself. The old /v1 suffix in the LocalAI URL produced a double prefix: /v1/v1/audio/transcriptions → 404. Changes: - Replace Python heredoc with shell sed + dynamic path resolution (python -c 'import ...' with prefix fallback loop). - Remove /v1 from LocalAI base URL (https://aido-whisper-ch.livtec.cloud). - Add set -e so real shell errors are not swallowed. - Add inline grep verification after each patch block so docker logs shows exactly which patterns matched. - Language injection now tries both aiohttp FormData (add_field) and dict-literal anchors instead of one fixed SDK-call string. - Remove corruption-cleanup logic (repeated or "..." chains) that was only needed because the old sed ran non-idempotently on a persisted volume. The new approach is idempotent by design. - Annotate VOICE_DISABLE_STREAMING_STT as inert in 5.0.3 (not referenced in the voice module source); kept for forward-compat. --- docker-compose.yml | 197 ++++++++++++++------------------------------- 1 file changed, 61 insertions(+), 136 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 0e402b3..0b30200 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,153 +1,76 @@ services: api_server: image: ${ONYX_BACKEND_IMAGE:-onyxdotapp/onyx-backend:${IMAGE_TAG:-latest}} - entrypoint: | + entrypoint: > sh -c " - python3 << 'PYEOF' - import os, sys + set -e; - def read_file(path): - with open(path, \"r\") as f: - return f.read() + # ── Resolve voice module paths via Python import ───────────── + VF=\$(python -c 'import onyx.voice.factory as m; print(m.__file__)' 2>/dev/null || true); + VO=\$(python -c 'import onyx.voice.providers.openai as m; print(m.__file__)' 2>/dev/null || true); - def write_file(path, content): - with open(path, \"w\") as f: - f.write(content) + # Fallback: try known WORKDIR prefixes + if [ -z \"\$VF\" ] || [ -z \"\$VO\" ]; then + for p in /app /app/backend /srv/app /opt/onyx /usr/src/app; do + if [ -f \"\${p}/onyx/voice/factory.py\" ]; then + VF=\"\${p}/onyx/voice/factory.py\"; + VO=\"\${p}/onyx/voice/providers/openai.py\"; + break; + fi; + done; + fi; - # --- 1. Clean up corrupted factory.py --- - factory_path = \"/app/onyx/voice/factory.py\" - if os.path.exists(factory_path): - src = read_file(factory_path) + if [ -z \"\$VF\" ] || [ -z \"\$VO\" ]; then + echo '[patch] ERROR: voice files not found, skipping all patches'; + else + echo \"[patch] factory: \$VF\"; + echo \"[patch] openai : \$VO\"; - # 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\") + # ── Config ───────────────────────────────────────────────── + # NOTE: no /v1 suffix. 5.0.3 code appends /v1/audio/... itself. + LB='https://aido-whisper-ch.livtec.cloud'; + SM='whisper-1'; + WL=\${WHISPER_LANGUAGE:-de}; - # 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\") + # ── 1. factory.py: fallback when admin UI has no provider ─ + sed -i \"s|api_base = provider.api_base$|api_base = provider.api_base or '$LB'|\" \"\$VF\"; + sed -i \"s|stt_model = provider.stt_model$|stt_model = provider.stt_model or '$SM'|\" \"\$VF\"; - # 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\") + # ── 2. openai.py: default base URL (no /v1!) ────────────── + sed -i \"s|DEFAULT_OPENAI_API_BASE = \\\"https://api.openai.com\\\"|DEFAULT_OPENAI_API_BASE = \\\"$LB\\\"|\" \"\$VO\"; - # 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\") + # ── 3. openai.py: neutralise WS realtime models ─────────── + sed -i \"s|OPENAI_REALTIME_SESSION_MODEL = \\\"gpt-realtime-1.5\\\"|OPENAI_REALTIME_SESSION_MODEL = \\\"$SM\\\"|\" \"\$VO\"; + sed -i \"s|OPENAI_REALTIME_STT_MODEL = \\\"gpt-realtime-whisper\\\"|OPENAI_REALTIME_STT_MODEL = \\\"$SM\\\"|\" \"\$VO\"; - write_file(factory_path, src) - else: - print(\"[FATAL] factory.py not found\") - sys.exit(1) + # ── 4. Language injection for chunked STT ───────────────── + if grep -q 'def transcribe' \"\$VO\" \&\& ! grep -q 'WHISPER_LANGUAGE' \"\$VO\"; then + sed -i '1i import os' \"\$VO\"; + # Anchor: the line that sets the model in the form data. + # In 5.0.3 the transcribe method uses aiohttp FormData: + # form.add_field('model', self.stt_model) + # OR a dict: data = {\"model\": ..., \"file\": ...} + # Try both patterns. + if grep -q \"add_field.*model\" \"\$VO\"; then + sed -i \"/add_field('model'/a\\ form.add_field('language', os.environ.get('WHISPER_LANGUAGE', '$WL'))\" \"\$VO\"; + elif grep -q '\"model\"' \"\$VO\"; then + sed -i \"/['\\\"]model['\\\"]\s*:/a\\ 'language': os.environ.get('WHISPER_LANGUAGE', '$WL'),\" \"\$VO\"; + fi; + fi; - # --- 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) + echo '[patch] All voice patches applied.'; - src = read_file(openai_path) + # ── Verify patches took effect ──────────────────────────── + echo '[patch] --- Verification ---'; + grep 'DEFAULT_OPENAI_API_BASE' \"\$VO\" | head -1; + grep 'OPENAI_REALTIME_SESSION_MODEL' \"\$VO\" | head -1; + grep 'api_base or' \"\$VF\" | head -1; + grep 'WHISPER_LANGUAGE' \"\$VO\" | head -1 || echo '[patch] WARN: language injection did not match'; + echo '[patch] --- End verification ---'; + fi; - # 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 + # ── Migrations + server ────────────────────────────────────── + 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\" && @@ -190,8 +113,10 @@ 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 + # Voice / STT - WHISPER_LANGUAGE=de + # Not read by 5.0.3 voice module. Kept for forward-compat. + - 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