Skip to content

Commit

Permalink
chore(example): add basic agent and task definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
rektdeckard committed Sep 11, 2024
1 parent 8a53ea1 commit 0078b84
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
.env.local
6 changes: 6 additions & 0 deletions agent/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIVEKIT_URL=<your LiveKit server URL>
LIVEKIT_API_KEY=<your API Key>
LIVEKIT_API_SECRET=<your API Secret>
ELEVEN_API_KEY=<your ElevenLabs API key>
DEEPGRAM_API_KEY=<your Deepgram API key>
OPENAI_API_KEY=<your OpenAI API key>
2 changes: 2 additions & 0 deletions agent/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env.local
venv/
56 changes: 56 additions & 0 deletions agent/agent.py
Original file line number Diff line number Diff line change
@@ -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))
5 changes: 5 additions & 0 deletions agent/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
73 changes: 73 additions & 0 deletions taskfile.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0078b84

Please sign in to comment.