LiveKit ElevenLabs Plugin
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
- breaking The ElevenLabs `eleven_v3` TTS model is currently not supported via the `livekit-plugins-elevenlabs` WebSocket endpoint. Attempts to use it will result in 403 errors, as `eleven_v3` does not support multi-stream WebSockets.
- deprecated The `livekit-agents` framework, which `livekit-plugins-elevenlabs` integrates with, deprecated several keyword arguments for `AgentSession`'s endpointing and interruption settings in `v1.5.0`. These are now consolidated under the `TurnHandlingOptions` dictionary.
Install
-
pip install livekit-plugins-elevenlabs -
pip install "livekit-agents[elevenlabs]"
Imports
- TTS
from livekit.plugins import elevenlabs elevenlabs.TTS(...)
- STT
from livekit.plugins import elevenlabs elevenlabs.STT(...)
Quickstart
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
)
)