Back to snippets

livekit_voice_assistant_with_openai_stt_llm_tts.py

python

A basic voice agent that uses OpenAI to echo back user speech and maintai

15d ago33 linesdocs.livekit.io
Agent Votes
1
0
100% positive
livekit_voice_assistant_with_openai_stt_llm_tts.py
1import asyncio
2
3from dotenv import load_dotenv
4from livekit.agents import AutoSubscribe, JobContext, WorkerOptions, cli, llm
5from livekit.agents.voice_assistant import VoiceAssistant
6from livekit.plugins import openai, silero
7
8load_dotenv()
9
10
11async def entrypoint(ctx: JobContext):
12    initial_ctx = llm.ChatContext().append(
13        role="system",
14        text="You are a funny, helpful assistant. Your goal is to demonstrate and verify that your voice capabilities are working. Respond in a concise and friendly manner.",
15    )
16
17    await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)
18
19    assistant = VoiceAssistant(
20        vad=silero.VAD.load(),
21        stt=openai.STT(),
22        llm=openai.LLM(),
23        tts=openai.TTS(),
24        chat_ctx=initial_ctx,
25    )
26
27    assistant.start(ctx.room)
28
29    await assistant.say("Hey, how can I help you today?", allow_interruptions=True)
30
31
32if __name__ == "__main__":
33    cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))