can i track when fallback is triggered for a Fallb...
# ask-ai
r
can i track when fallback is triggered for a FallbackAdapter?
t
Thanks for reaching out, John. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Yes, you can track when fallback is triggered for a
FallbackAdapter
. The system logs warnings when a TTS (Text-to-Speech) service fails and switches to the next available service. Additionally, it emits an event called
tts_availability_changed
to indicate changes in the availability of the TTS services. These mechanisms allow you to monitor both fallback triggers and service recoveries effectively. Sources: livekit.agents.tts API documentation | livekit.agents.tts API documentation
r
can you give me an example where the fallback adapter is changed to a different provider and it just prints the provider name?
t
Unfortunately, I couldn't find a specific example of a
FallbackAdapter
changing to a different provider and printing the provider name. However, the concept involves logging or emitting events when a fallback occurs. If you have a specific implementation in mind, I can assist further or escalate this to a specialist for more detailed guidance.
r
can you give me any code to show how it is handled?
t
Here is an example of how fallback is handled in a `FallbackAdapter`:
Copy code
from livekit.agents import stt
from livekit.plugins import deepgram, openai, silero, cartesia, playai

# Create a VAD (Voice Activity Detection) instance
vad = silero.VAD.load()

# Create a fallback STT (Speech-to-Text) adapter
# If Deepgram fails, it will fall back to OpenAI
fallback_stt = stt.FallbackAdapter([
    deepgram.STT(),
    stt.StreamAdapter(stt=openai.STT(), vad=vad),
])

# Create a fallback LLM (Language Learning Model) adapter
# If OpenAI fails, it will fall back to Azure
fallback_llm = llm.FallbackAdapter([
    openai.LLM(),
    openai.LLM.with_azure(),
])

# Create a fallback TTS (Text-to-Speech) adapter
# If Cartesia fails, it will fall back to PlayAI
fallback_tts = tts.FallbackAdapter([
    cartesia.TTS(),
    playai.TTS(),
])

# Use these fallback adapters in a voice pipeline agent
agent = VoicePipelineAgent(
    vad=vad,
    stt=fallback_stt,
    llm=fallback_llm,
    tts=fallback_tts,
    chat_ctx=initial_ctx,
    fnc_ctx=fnc_ctx,
)
This example demonstrates how to set up fallback adapters for STT, LLM, and TTS components. Each adapter takes a list of services in priority order, and if the first service fails, it automatically switches to the next one in the list. Sources: Tree View: | dse-workbench
r
nice how do i handle the event?
t
To handle the
tts_availability_changed
event in a
FallbackAdapter
, you can listen for the event and take appropriate actions. Here's how the event is emitted and how you can handle it:
Copy code
# When a TTS instance becomes unavailable
self._tts.emit(
    "tts_availability_changed",
    AvailabilityChangedEvent(tts=tts, available=False),
)

# When a TTS instance recovers and becomes available again
self._tts.emit(
    "tts_availability_changed",
    AvailabilityChangedEvent(tts=tts, available=True),
)
The
AvailabilityChangedEvent
contains the TTS instance and its availability status. You can set up a listener for this event to handle it:
Copy code
def on_availability_changed(event):
    if event.available:
        print(f"TTS provider {event.tts.label} is now available.")
    else:
        print(f"TTS provider {event.tts.label} is now unavailable.")

# Subscribe to the event
fallback_tts._tts.on("tts_availability_changed", on_availability_changed)
This will allow you to monitor and respond to changes in the availability of TTS providers. Sources: livekit.agents.tts API documentation | livekit.agents.tts.fallback_adapter API documentation
r
hero cheers
t
You're very welcome! Glad I could help. If you have any more questions, feel free to ask. Cheers!