fix(api_server): update voice STT patch for Onyx 5.0.3 image layout

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.
This commit is contained in:
Jannic Debus 2026-11-12 19:58:27 +01:00
parent bc3dd56e00
commit f51a35732c

View file

@ -1,153 +1,76 @@
services: services:
api_server: api_server:
image: ${ONYX_BACKEND_IMAGE:-onyxdotapp/onyx-backend:${IMAGE_TAG:-latest}} image: ${ONYX_BACKEND_IMAGE:-onyxdotapp/onyx-backend:${IMAGE_TAG:-latest}}
entrypoint: | entrypoint: >
sh -c " sh -c "
python3 << 'PYEOF' set -e;
import os, sys
def read_file(path): # ── Resolve voice module paths via Python import ─────────────
with open(path, \"r\") as f: VF=\$(python -c 'import onyx.voice.factory as m; print(m.__file__)' 2>/dev/null || true);
return f.read() VO=\$(python -c 'import onyx.voice.providers.openai as m; print(m.__file__)' 2>/dev/null || true);
def write_file(path, content): # Fallback: try known WORKDIR prefixes
with open(path, \"w\") as f: if [ -z \"\$VF\" ] || [ -z \"\$VO\" ]; then
f.write(content) 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 --- if [ -z \"\$VF\" ] || [ -z \"\$VO\" ]; then
factory_path = \"/app/onyx/voice/factory.py\" echo '[patch] ERROR: voice files not found, skipping all patches';
if os.path.exists(factory_path): else
src = read_file(factory_path) echo \"[patch] factory: \$VF\";
echo \"[patch] openai : \$VO\";
# Fix corrupted api_base line (remove repeated or chains) # ── Config ─────────────────────────────────────────────────
if 'api_base = provider.api_base or \\\"https://aido-whisper-ch.livtec.cloud/v1\\\"' in src: # NOTE: no /v1 suffix. 5.0.3 code appends /v1/audio/... itself.
src = src.replace( LB='https://aido-whisper-ch.livtec.cloud';
'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\\\"', SM='whisper-1';
'api_base = provider.api_base or \\\"https://aido-whisper-ch.livtec.cloud/v1\\\"' WL=\${WHISPER_LANGUAGE:-de};
)
# 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) # ── 1. factory.py: fallback when admin UI has no provider ─
if 'api_base = provider.api_base or \\\"https://aido-whisper-ch.livtec.cloud/v1\\\"' not in src: sed -i \"s|api_base = provider.api_base$|api_base = provider.api_base or '$LB'|\" \"\$VF\";
if 'api_base = provider.api_base' in src: sed -i \"s|stt_model = provider.stt_model$|stt_model = provider.stt_model or '$SM'|\" \"\$VF\";
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 # ── 2. openai.py: default base URL (no /v1!) ──────────────
if 'stt_model = provider.stt_model or \\\"whisper-1\\\" or \\\"whisper-1\\\"' in src: sed -i \"s|DEFAULT_OPENAI_API_BASE = \\\"https://api.openai.com\\\"|DEFAULT_OPENAI_API_BASE = \\\"$LB\\\"|\" \"\$VO\";
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) # ── 3. openai.py: neutralise WS realtime models ───────────
if 'stt_model = provider.stt_model or \\\"whisper-1\\\"' not in src: sed -i \"s|OPENAI_REALTIME_SESSION_MODEL = \\\"gpt-realtime-1.5\\\"|OPENAI_REALTIME_SESSION_MODEL = \\\"$SM\\\"|\" \"\$VO\";
if 'stt_model = provider.stt_model' in src: sed -i \"s|OPENAI_REALTIME_STT_MODEL = \\\"gpt-realtime-whisper\\\"|OPENAI_REALTIME_STT_MODEL = \\\"$SM\\\"|\" \"\$VO\";
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) # ── 4. Language injection for chunked STT ─────────────────
else: if grep -q 'def transcribe' \"\$VO\" \&\& ! grep -q 'WHISPER_LANGUAGE' \"\$VO\"; then
print(\"[FATAL] factory.py not found\") sed -i '1i import os' \"\$VO\";
sys.exit(1) # 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 --- echo '[patch] All voice patches applied.';
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) # ── 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) # ── Migrations + server ──────────────────────────────────────
if 'DEFAULT_OPENAI_API_BASE = \\\"https://api.openai.com\\\"' in src: alembic upgrade head \&\& echo 'Starting Onyx Api Server' \&\& exec uvicorn onyx.main:app --host 0.0.0.0 --port 8080
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: > #command: >
# /bin/sh -c "alembic upgrade head && echo \"Starting Onyx Api Server\" && # /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) # 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 # Use --include-craft with install script, or manually set in .env file
- ENABLE_CRAFT=${ENABLE_CRAFT:-false} - ENABLE_CRAFT=${ENABLE_CRAFT:-false}
- VOICE_DISABLE_STREAMING_STT=true # Voice / STT
- WHISPER_LANGUAGE=de - 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 # PRODUCTION: Uncomment the line below to use if IAM_AUTH is true and you are using iam auth for postgres
# volumes: # volumes:
# - ./bundle.pem:/app/bundle.pem:ro # - ./bundle.pem:/app/bundle.pem:ro