give me livekit agents code, to use fallback for t...
# ask-ai
b
give me livekit agents code, to use fallback for tts and stt
t
Thanks for reaching out, Mohammed. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
LiveKit provides FallbackAdapter implementations for both TTS (Text-to-Speech) and STT (Speech-to-Text) services. These adapters manage multiple service instances and include automatic failover capabilities. Here are the key features: • STT FallbackAdapter: ◦ Automatically switches between STT instances if failures occur. ◦ Marks failed providers as unhealthy and stops sending requests to them. ◦ Periodically checks failed providers for recovery. ◦ Tracks provider status with availability change events. • TTS FallbackAdapter: ◦ Handles multiple TTS instances with automatic failover. ◦ Resubmits failed requests to backup providers. ◦ Continues using backup providers until the primary recovers. ◦ Monitors provider health and availability in the background. Both adapters include configurable retry policies, attempt timeouts, recovery mechanisms, and availability status tracking through events. Sources: livekit-agents/livekit/agents/voice/agent_activity.py | Events and error handling | LiveKit Docs | Events and error handling | LiveKit Docs
b
give me the code itself, how to use fallback in livekit agents
t
Hey there! Here's a friendly guide on how you can use the FallbackAdapter for TTS and STT in LiveKit agents. Let’s make sure your setup is smooth and reliable! For TTS:
Copy code
from livekit.agents.tts import FallbackAdapter
from livekit.plugins.elevenlabs import TTS as ElevenLabsTTS
from livekit.plugins.groq import TTS as GroqTTS

tts_instances = [
    ElevenLabsTTS(...),  # Just configure these as you need
    GroqTTS(...)         # Same here, tailor to your setup
]

# Now, create the FallbackAdapter
tts_fallback = FallbackAdapter(
    tts=tts_instances,
    max_retry_per_tts=2,
    sample_rate=48000  # Optional, adjust if you need a specific sample rate
)
For STT:
Copy code
from livekit.agents.stt import FallbackAdapter
from livekit.plugins.assemblyai import STT as AssemblyAISTT
from livekit.plugins.deepgram import STT as DeepgramSTT

stt_instances = [
    AssemblyAISTT(...),   # Configure these as you like
    DeepgramSTT(...)      # Same here, just set them up as needed
]

# Let’s create the FallbackAdapter for STT
stt_fallback = FallbackAdapter(
    stt=stt_instances,
    attempt_timeout=10.0,  # How long to wait for each attempt
    max_retry_per_stt=1,    # How many times to retry per STT instance
    retry_interval=5       # Time between retries
)
Event Handling: Want to keep an eye on availability changes? Here’s a simple way to do that:
Copy code
def on_availability_changed(event):
    # This will let you know when STT or TTS availability changes
    print(f"STT: {event.stt.label} is {'available' if event.available else 'unavailable'}")

stt_fallback.on("stt_availability_changed", on_availability_changed)
tts_fallback.on("tts_availability_changed", on_availability_changed)
These examples should help you add some solid redundancy and failover for both TTS and STT in your LiveKit agent apps. If you have any questions or want to chat more about it, just let me know!