From 0078b84fb6e9af262db682a106618ebb937e90e2 Mon Sep 17 00:00:00 2001 From: rektdeckard Date: Wed, 11 Sep 2024 17:16:26 -0600 Subject: [PATCH] chore(example): add basic agent and task definitions --- .gitignore | 1 + agent/.env.example | 6 ++++ agent/.gitignore | 2 ++ agent/agent.py | 56 ++++++++++++++++++++++++++++++++ agent/requirements.txt | 5 +++ taskfile.yaml | 73 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 143 insertions(+) create mode 100644 agent/.env.example create mode 100644 agent/.gitignore create mode 100644 agent/agent.py create mode 100644 agent/requirements.txt create mode 100644 taskfile.yaml diff --git a/.gitignore b/.gitignore index e43b0f98..d9fbb4b2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .DS_Store +.env.local diff --git a/agent/.env.example b/agent/.env.example new file mode 100644 index 00000000..f79070fa --- /dev/null +++ b/agent/.env.example @@ -0,0 +1,6 @@ +LIVEKIT_URL= +LIVEKIT_API_KEY= +LIVEKIT_API_SECRET= +ELEVEN_API_KEY= +DEEPGRAM_API_KEY= +OPENAI_API_KEY= diff --git a/agent/.gitignore b/agent/.gitignore new file mode 100644 index 00000000..819989b0 --- /dev/null +++ b/agent/.gitignore @@ -0,0 +1,2 @@ +.env.local +venv/ diff --git a/agent/agent.py b/agent/agent.py new file mode 100644 index 00000000..aae2c8a7 --- /dev/null +++ b/agent/agent.py @@ -0,0 +1,56 @@ +import asyncio + +from dotenv import load_dotenv +from livekit import rtc +from livekit.agents import AutoSubscribe, JobContext, WorkerOptions, cli, llm +from livekit.agents.voice_assistant import VoiceAssistant +from livekit.plugins import deepgram, openai, silero + +load_dotenv(dotenv_path=".env.local") + + +async def entrypoint(ctx: JobContext): + initial_ctx = llm.ChatContext().append( + role="system", + text=( + "You are a voice assistant created by LiveKit. Your interface with users will be voice. " + "You should use short and concise responses, and avoiding usage of unpronouncable punctuation. " + "You were created as a demo to showcase the capabilities of LiveKit's agents framework, " + "as well as the ease of development of realtime AI prototypes. You are currently running in a " + "LiveKit Sandbox, which is an environment that allows developers to instantly deploy prototypes " + "of their realtime AI applications to share with others." + ), + ) + + await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY) + + assistant = VoiceAssistant( + vad=silero.VAD.load(), + stt=deepgram.STT(), + llm=openai.LLM(), + tts=openai.TTS(), + chat_ctx=initial_ctx, + ) + assistant.start(ctx.room) + + # listen to incoming chat messages, only required if you'd like the agent to + # answer incoming messages from Chat + chat = rtc.ChatManager(ctx.room) + + async def answer_from_text(txt: str): + chat_ctx = assistant.chat_ctx.copy() + chat_ctx.append(role="user", text=txt) + stream = assistant.llm.chat(chat_ctx=chat_ctx) + await assistant.say(stream) + + @chat.on("message_received") + def on_chat_received(msg: rtc.ChatMessage): + if msg.message: + asyncio.create_task(answer_from_text(msg.message)) + + await asyncio.sleep(1) + await assistant.say("Hey, how can I help you today?", allow_interruptions=True) + + +if __name__ == "__main__": + cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint)) diff --git a/agent/requirements.txt b/agent/requirements.txt new file mode 100644 index 00000000..a1760441 --- /dev/null +++ b/agent/requirements.txt @@ -0,0 +1,5 @@ +livekit-agents>=0.8.5 +livekit-plugins-openai>=0.8.0 +livekit-plugins-deepgram>=0.6.4 +livekit-plugins-silero>=0.6.3 +python-dotenv~=1.0 diff --git a/taskfile.yaml b/taskfile.yaml new file mode 100644 index 00000000..28d172c0 --- /dev/null +++ b/taskfile.yaml @@ -0,0 +1,73 @@ +version: "3" + +output: prefixed + +dotenv: + - "ui/.env.local" + - "agent/.env.local" + +tasks: + install: + desc: "Bootstrap application for local development" + deps: + - install_ui + - install_agent + + install_sandbox: + desc: "Bootstrap application for sandbox environment" + deps: + - install_agent + + install_ui: + dir: "frontend" + interactive: true + cmds: + - "pnpm install" + + install_agent: + dir: "agent" + cmds: + - "python3 -m venv venv" + - cmd: "source venv/bin/activate" + platforms: + - darwin + - linux + - cmd: "powershell venv/Scripts/Activate.ps1" + platforms: + - windows + - cmd: "venv/bin/python3 -m pip install -r requirements.txt" + platforms: + - darwin + - linux + - cmd: "venv/Scripts/python -m pip install -r requirements.txt" + platforms: + - windows + + dev: + interactive: true + deps: + - dev_ui + - dev_agent + + dev_ui: + dir: "frontend" + cmds: + - "pnpm dev" + + dev_agent: + dir: "agent" + cmds: + - cmd: "source venv/bin/activate" + platforms: + - darwin + - linux + - cmd: "powershell venv/Scripts/Activate.ps1" + platforms: + - windows + - cmd: "venv/bin/python3 agent.py start" + platforms: + - darwin + - linux + - cmd: "venv/Scripts/python agent.py start" + platforms: + - windows