how can I implement silence timeout in LiveKit
# ask-ai
c
how can I implement silence timeout in LiveKit
g
To implement a silence timeout in LiveKit, you can use the Voice Activity Detection (VAD) feature. According to LiveKit Docs, you can configure the
turn_detection
parameter with
server_vad
to detect when a speaker has finished talking. You can adjust parameters like
threshold
,
prefix_padding_ms
, and
silence_duration_ms
to fine-tune the detection sensitivity and silence duration. Additionally, as per a Slack discussion, you can implement a timeout mechanism using asynchronous tasks to handle inactivity. This involves starting a timeout task when no speech is detected and resetting it upon detecting speech. If the timeout elapses, you can trigger an action, such as ending a call or prompting the user. Would you like more detailed guidance on setting this up? Sources: - LiveKit Docs - Slack discussion 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
c
can I have some code for this @millions-hair-24956
g
Certainly! Below is a Python code snippet that demonstrates how to implement a silence timeout using LiveKit's Voice Activity Detection (VAD) and asynchronous tasks. This example sets up a timeout task that triggers an action if no speech is detected for a specified duration.
Copy code
import asyncio
from livekit import rtc
from livekit.agents import vad, VoicePipelineAgent
from livekit.plugins import openai, silero

async def main():
    # Load VAD with custom silence duration
    vad_instance = silero.VAD.load(
        min_speech_duration=0.05,
        min_silence_duration=5,  # 5 seconds of silence
        padding_duration=0.1,
        activation_threshold=0.25,
        sample_rate=16000,
        force_cpu=1,
    )

    # Initialize the VoicePipelineAgent
    agent = VoicePipelineAgent(
        vad=vad_instance,
        stt=openai.STT(),
        llm=openai.LLM(),
        tts=openai.TTS(),
        chat_ctx=None,
        allow_interruptions=True,
    )

    # Define a timeout task
    async def silence_timeout():
        try:
            await asyncio.sleep(5)  # 5 seconds timeout
            print("Silence detected, triggering action...")
            # Trigger an action, e.g., prompt the user
        except asyncio.CancelledError:
            # Task was cancelled due to new speech activity
            pass

    # Start the agent and handle silence detection
    async def handle_silence():
        timeout_task = None

        @agent.on("user_stopped_speaking")
        def on_user_stopped_speaking():
            nonlocal timeout_task
            if timeout_task and not timeout_task.done():
                timeout_task.cancel()
            timeout_task = asyncio.create_task(silence_timeout())

        @agent.on("user_started_speaking")
        def on_user_started_speaking():
            if timeout_task and not timeout_task.done():
                timeout_task.cancel()

    await handle_silence()
    await agent.start(rtc.Room(), rtc.Participant())

if __name__ == "__main__":
    asyncio.run(main())
This code sets up a
VoicePipelineAgent
with a VAD instance configured to detect 5 seconds of silence. It uses asynchronous tasks to manage a timeout that triggers an action when silence is detected. The timeout is reset whenever speech is detected. Sources: - LiveKit Docs - Slack discussion 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.