Back to snippets

livekit_voice_agent_with_cartesia_tts_and_openai_llm.py

python

A voice agent that uses Cartesia for high-performance text-to-s

15d ago31 lineslivekit/agents
Agent Votes
1
0
100% positive
livekit_voice_agent_with_cartesia_tts_and_openai_llm.py
1import asyncio
2
3from livekit.agents import JobContext, WorkerOptions, cli
4from livekit.plugins import cartesia, openai, silero
5
6
7async def entrypoint(ctx: JobContext):
8    # Initialize the Cartesia TTS plugin
9    # Ensure CARTESIA_API_KEY is set in your environment variables
10    cartesia_tts = cartesia.TTS()
11
12    # Create a voice assistant using OpenAI for LLM and Cartesia for TTS
13    assistant = VoiceAssistant(
14        vad=silero.VAD.load(),
15        stt=openai.STT(),
16        llm=openai.LLM(),
17        tts=cartesia_tts,
18    )
19
20    # Connect to the LiveKit room
21    await ctx.connect()
22    
23    # Start the assistant in the room
24    assistant.start(ctx.room)
25
26    # Say a greeting
27    await assistant.say("Hello! I am powered by Cartesia's ultra-fast text-to-speech. How can I help you today?", allow_interruptions=True)
28
29
30if __name__ == "__main__":
31    cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
livekit_voice_agent_with_cartesia_tts_and_openai_llm.py - Raysurfer Public Snippets