is there a way to have a sticky fallabck adapter? ...
# ask-ai
t
is there a way to have a sticky fallabck adapter? so assuming main option files for this reuqest we fallback to option 2 and any subsequentr request til end of session we go to option 2. so voice doesn't change again
t
Thanks for reaching out, Joseph. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you asked about LiveKit's sticky fallback adapter mechanism! It's designed to make sure your session smoothly switches to a backup option (like option 2) whenever the primary service hits a snag. This handy feature works for STT, TTS, and LLM services. Here’s a friendly overview of how it operates: • If the primary service runs into trouble, the adapter kindly marks it as unavailable and switches over to the next available service (such as option 2). • This fallback choice sticks with you for the whole session, so all your following requests use the secondary option until the primary service is back up. • Behind the scenes, a recovery mechanism keeps an eye on the failed service and brings it back online when possible, but your session will stay on the fallback option until you decide to reset it. If you'd like, I’m here to walk you through the detailed implementation steps or share some code examples—just let me know! Sources: livekit-agents/livekit/agents/stt/__init__.py | Operational Guidance & Best Practices | livekit/livekit_composite | DeepWik...
t
yes pls show me how to do this
t
I'd love to guide you through implementing a sticky fallback adapter for session-level fallback. Let's take it step by step: 1. Install Required Libraries: First, make sure you have the necessary LiveKit libraries installed in your environment. This will set the foundation for everything else. 2. Configure the FallbackAdapter: Here’s a helpful example of how you can configure the FallbackAdapter for your LLM services:
Copy code
from livekit.agents.llm import FallbackAdapter

# Define primary and secondary services
primary_llm = ...  # Your primary LLM service
secondary_llm = ...  # Your secondary LLM service

# Create the fallback adapter
llm_fallback = FallbackAdapter(
    llm=[primary_llm, secondary_llm],
    attempt_timeout=5.0,           # Timeout for each attempt
    max_retry_per_llm=0,          # No retries, fallback immediately
    retry_interval=0.5,           # Interval between retries
    retry_on_chunk_sent=False     # Do not retry after chunks are sent
)
3. Monitor Service Availability: It’s a good idea to keep an eye on service availability. You can do this by handling events like so:
Copy code
# Monitor availability changes
def on_availability_changed(event):
    print(f"LLM {event.llm.label} availability: {event.available}")

llm_fallback.on("llm_availability_changed", on_availability_changed)
4. Handle Recovery: One of the great things is that the adapter automatically monitors and recovers any failed services for you. So, you don’t need to worry about manually resetting the fallback state during your session. 5. Use the Adapter in Your Application: Finally, integrate the fallback adapter into your application logic to ensure all requests are routed through it smoothly. For example:
Copy code
# Use the fallback adapter for generating responses
response = llm_fallback.generate(prompt="Your input prompt")
If you’d like, I’m here to help with more examples or guidance, especially for STT or TTS services. Just let me know!
t
no but i wanna do this for tts. so assuming that option1 fails. even if somewhere in the session option1 is back up io donm't wanna go back to option 1 (to keep voice consistyent)
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!