LiveKit Silero Plugin

1.5.2 · active · verified Sat Apr 11

The livekit-plugins-silero library provides a Voice Activity Detection (VAD) plugin for the LiveKit Agent Framework. It leverages the Silero VAD model to accurately detect speech versus silence, which is crucial for natural turn-taking in voice AI applications and for optimizing Speech-to-Text (STT) resource usage. The current version is 1.5.2, released as part of the LiveKit Agents framework, which follows a rapid release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `livekit-plugins-silero` into a LiveKit Agent. It sets up an `AgentServer` with a `prewarm` function to load the Silero VAD model efficiently once per process. The `my_agent` entrypoint then retrieves the prewarmed VAD instance and initializes an `AgentSession` with it, enabling voice activity detection for the agent. Remember to download model weights before running.

import asyncio
import os

from livekit.agents import AgentServer, AgentSession, JobContext, JobProcess, cli
from livekit.plugins import silero

# Ensure LiveKit credentials are set up as environment variables
# LIVEKIT_URL, LIVEKIT_API_KEY, LIVEKIT_API_SECRET

server = AgentServer()

def prewarm(proc: JobProcess):
    # Load the VAD model once per process for faster job startup
    print("Prewarming Silero VAD model...")
    proc.userdata["vad"] = silero.VAD.load()
    print("Silero VAD model prewarmed.")

server.setup_fnc = prewarm

@server.rtc_session(agent_name="my-silero-agent")
async def my_agent(ctx: JobContext):
    print(f"Agent {ctx.agent_name} received job {ctx.job_id}")
    await ctx.connect()

    # Retrieve the prewarmed VAD instance
    vad = ctx.proc.userdata["vad"]

    # Example: Initializing AgentSession with Silero VAD
    session = AgentSession(
        ctx,
        vad=vad,
        # Other components like STT, TTS, LLM would go here
        # stt=...,
        # tts=...,
        # llm=...,
    )

    print("Agent session started with Silero VAD.")
    try:
        await session.start()
        # Keep the agent running, e.g., for a conversation loop
        await asyncio.sleep(600) # Keep alive for 10 minutes
    finally:
        await session.end()
        print("Agent session ended.")

if __name__ == "__main__":
    # Important: Download model weights before first run:
    # python -m livekit.agents.cli download-files
    
    # Set dummy credentials for runnable quickstart if not in environment
    os.environ.setdefault('LIVEKIT_URL', os.environ.get('LIVEKIT_URL', 'wss://your-livekit-server.livekit.cloud'))
    os.environ.setdefault('LIVEKIT_API_KEY', os.environ.get('LIVEKIT_API_KEY', 'SK_XXXXX'))
    os.setdefault('LIVEKIT_API_SECRET', os.environ.get('LIVEKIT_API_SECRET', 'YOUR_SECRET'))

    cli.run_app(server)

view raw JSON →