Build voice agents with Speech Engine
mainThe Speech Engine allows you to build server-side voice agents. Your server acts as a WebSocket endpoint that ElevenLabs connects to. ElevenLabs sends real-time user transcripts, and your server responds by streaming LLM responses back for text-to-speech synthesis.
Note: Speech Engine is async-only and requires using AsyncElevenLabs.
import asyncio
from openai import AsyncOpenAI
from elevenlabs import AsyncElevenLabs
openai_client = AsyncOpenAI()
elevenlabs = AsyncElevenLabs()
async def main():
engine = await elevenlabs.speech_engine.get("seng_123")
async def on_transcript(transcript, session):
stream = await openai_client.responses.create(
model="gpt-4o",
input=[
{"role": "assistant" if m.role == "agent" else m.role, "content": m.content}
for m in transcript
],
stream=True,
)
await session.send_response(stream)
async def on_init(conversation_id, session):
print(f"Session started: {conversation_id}")
async def on_close(session):
print(f"Session ended: {session.conversation_id}")
async def on_error(err, session):
print(f"Error: {err}")
await engine.serve(
port=3001,
debug=True,
on_init=on_init,
on_transcript=on_transcript,
on_close=on_close,
on_error=on_error,
)
asyncio.run(main())