LiveKit ElevenLabs Plugin

1.5.2 · active · verified Sun Apr 12

livekit-plugins-elevenlabs is a Python plugin for the LiveKit Agent Framework, providing seamless integration for voice synthesis (TTS) and speech-to-text (STT) using ElevenLabs' API. It enables real-time, multimodal AI agents to interact with users through high-quality ElevenLabs voices. The library receives frequent updates, often alongside releases of the core `livekit-agents` framework.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic LiveKit voice agent using `livekit-plugins-elevenlabs` for both Text-to-Speech (TTS) and Speech-to-Text (STT) capabilities. It assumes you have `livekit-agents` and `python-dotenv` installed, and `LIVEKIT_URL`, `LIVEKIT_API_KEY`, `LIVEKIT_API_SECRET`, and `ELEVENLABS_API_KEY` (and `OPENAI_API_KEY` if using OpenAI LLM) set as environment variables. The agent connects to a LiveKit room, uses ElevenLabs for voice interaction, and can be extended with an LLM for conversational AI.

import asyncio
import os
from dotenv import load_dotenv
from livekit.agents import JobContext, WorkerOptions, cli, Agent, AgentSession
from livekit.agents.voice import VoicePipeline
from livekit.plugins import elevenlabs, openai # Assuming openai for LLM, can be replaced

load_dotenv()

async def entrypoint(ctx: JobContext):
    await ctx.connect()

    # Configure ElevenLabs TTS and STT with API key from environment variable
    elevenlabs_tts = elevenlabs.TTS(
        api_key=os.environ.get('ELEVENLABS_API_KEY', '')
    )
    elevenlabs_stt = elevenlabs.STT(
        api_key=os.environ.get('ELEVENLABS_API_KEY', ''),
        model_id="scribe_v2_realtime" # Explicitly specify STT model
    )

    # Initialize an AgentSession with ElevenLabs for speech and OpenAI for LLM (example)
    session = AgentSession(
        tts=elevenlabs_tts,
        stt=elevenlabs_stt,
        llm=openai.LLM(
            api_key=os.environ.get('OPENAI_API_KEY', ''),
            model="gpt-4-turbo", 
            instructions="You are a helpful voice AI assistant."
        )
    )

    await session.start(
        room=ctx.room,
        agent=Agent(instructions="You are a helpful voice AI assistant using ElevenLabs for speech.")
    )

    # Agent generates an initial greeting using ElevenLabs TTS
    await session.generate_reply(instructions="Hello! I am a voice assistant powered by LiveKit and ElevenLabs. How can I help you today?")

if __name__ == "__main__":
    # Set LiveKit and ElevenLabs API keys as environment variables for a runnable example
    # export LIVEKIT_URL="wss://your-livekit-instance.cloud"
    # export LIVEKIT_API_KEY="your_livekit_api_key"
    # export LIVEKIT_API_SECRET="your_livekit_api_secret"
    # export ELEVENLABS_API_KEY="your_elevenlabs_api_key"
    # export OPENAI_API_KEY="your_openai_api_key"
    cli.run_app(
        WorkerOptions(
            entrypoint_fnc=entrypoint,
            num_workers=1 
        )
    )

view raw JSON →