from voicegateway import inference
def first_resolvable(modality: str, chain: list[str]):
"""Walk the chain, return the first inference instance that builds.
Raises the last error if every model fails.
"""
factory = {
"stt": inference.STT,
"llm": inference.LLM,
"tts": inference.TTS,
}[modality]
last_error: Exception | None = None
for model_id in chain:
try:
return factory(model_id)
except Exception as exc: # noqa: BLE001
last_error = exc
assert last_error is not None
raise last_error
STT_CHAIN = ["deepgram/nova-3", "openai/whisper-1", "local/whisper-large-v3"]
LLM_CHAIN = [
"openai/gpt-4.1-mini",
"groq/llama-3.3-70b-versatile",
"ollama/qwen2.5:3b",
]
TTS_CHAIN = ["cartesia/sonic-3", "elevenlabs/turbo-v2.5", "local/kokoro"]
stt = first_resolvable("stt", STT_CHAIN)
llm = first_resolvable("llm", LLM_CHAIN)
tts = first_resolvable("tts", TTS_CHAIN)