feat: Resilienz (Fallback-Ketten) und Metriken (#5)
- Fallback-Provider (app/providers/fallback.py) fuer STT/LLM/TTS: Provider-Kette der Reihe nach; Config *_FALLBACK; build_orchestrator baut Ketten (dedupliziert) - LLM-Stream-Fallback nur solange kein Token gesendet wurde - Metriken (app/metrics.py): In-Memory Counter/Timer, keine externe Dependency - HTTP-Middleware (Requests/Latenz/Status je Pfad); Pipeline-Stufen-Timing stt/llm/tts; Fallback-/Fehlerzaehler; GET /api/metrics (JSON + Prometheus) - Tests: 58 gruen (+6); Doku aktualisiert (README, BEDIENUNGSANLEITUNG, Architektur, .env.example) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
093da817d8
commit
6422444017
13 changed files with 452 additions and 23 deletions
85
app/providers/fallback.py
Normal file
85
app/providers/fallback.py
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
"""Fallback-Ketten: versuchen mehrere Provider der Reihe nach.
|
||||
|
||||
Faellt der primaere Provider aus (Timeout/Fehler), wird transparent der naechste
|
||||
versucht. Erfolgreicher Fallback und Provider-Fehler werden als Metrik erfasst.
|
||||
"""
|
||||
|
||||
from collections.abc import AsyncIterator
|
||||
|
||||
from app.metrics import metrics
|
||||
|
||||
|
||||
class _Chain:
|
||||
def __init__(self, module: str, entries: list[tuple[str, object]]):
|
||||
self.module = module
|
||||
self.entries = entries # [(provider_name, provider), ...]
|
||||
|
||||
def _on_error(self, name: str) -> None:
|
||||
metrics.inc("provider_error_total", {"module": self.module, "provider": name})
|
||||
|
||||
def _on_fallback(self) -> None:
|
||||
metrics.inc("provider_fallback_total", {"module": self.module})
|
||||
|
||||
|
||||
class FallbackSTTProvider(_Chain):
|
||||
async def transcribe(self, audio_bytes, fmt, language=None) -> str:
|
||||
last_exc = None
|
||||
for index, (name, provider) in enumerate(self.entries):
|
||||
try:
|
||||
result = await provider.transcribe(audio_bytes, fmt, language=language)
|
||||
if index > 0:
|
||||
self._on_fallback()
|
||||
return result
|
||||
except Exception as exc: # noqa: BLE001 - bewusst breit fuer Resilienz
|
||||
last_exc = exc
|
||||
self._on_error(name)
|
||||
raise last_exc
|
||||
|
||||
|
||||
class FallbackLLMProvider(_Chain):
|
||||
async def complete(self, text, history=None, session_id=None) -> str:
|
||||
last_exc = None
|
||||
for index, (name, provider) in enumerate(self.entries):
|
||||
try:
|
||||
result = await provider.complete(text, history=history, session_id=session_id)
|
||||
if index > 0:
|
||||
self._on_fallback()
|
||||
return result
|
||||
except Exception as exc: # noqa: BLE001
|
||||
last_exc = exc
|
||||
self._on_error(name)
|
||||
raise last_exc
|
||||
|
||||
async def stream(self, text, history=None, session_id=None) -> AsyncIterator[str]:
|
||||
last_exc = None
|
||||
for index, (name, provider) in enumerate(self.entries):
|
||||
produced = False
|
||||
try:
|
||||
async for delta in provider.stream(text, history=history, session_id=session_id):
|
||||
produced = True
|
||||
yield delta
|
||||
if index > 0:
|
||||
self._on_fallback()
|
||||
return
|
||||
except Exception as exc: # noqa: BLE001
|
||||
last_exc = exc
|
||||
self._on_error(name)
|
||||
if produced:
|
||||
# Schon Token gesendet -> kein Fallback mehr moeglich.
|
||||
raise
|
||||
raise last_exc
|
||||
|
||||
|
||||
class FallbackTTSProvider(_Chain):
|
||||
async def synthesize(self, text, voice=None, audio_format="pcm") -> bytes:
|
||||
last_exc = None
|
||||
for index, (name, provider) in enumerate(self.entries):
|
||||
try:
|
||||
result = await provider.synthesize(text, voice=voice, audio_format=audio_format)
|
||||
if index > 0:
|
||||
self._on_fallback()
|
||||
return result
|
||||
except Exception as exc: # noqa: BLE001
|
||||
last_exc = exc
|
||||
self._on_error(name)
|
||||
raise last_exc
|
||||
Loading…
Add table
Add a link
Reference in a new issue