Repository navigation
Expand file tree
/
Copy pathbasic_agent.py
More file actions
55 lines (40 loc) · 1.77 KB
/
Copy pathbasic_agent.py
File metadata and controls
55 lines (40 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""A minimal voice agent on Dialog-RSN-1, with one tool.
uv sync --group examples
cp .env.example .env # add POLY_API_KEY and ELEVEN_API_KEY
uv run lk agent console examples/basic_agent.py
Or run it against a LiveKit server with `uv run examples/basic_agent.py dev`.
"""
from datetime import datetime
from dotenv import load_dotenv
from livekit.agents import Agent, AgentServer, AgentSession, JobContext, RunContext, cli
from livekit.agents.llm import function_tool
from livekit.plugins import elevenlabs, polyai
load_dotenv()
class Assistant(Agent):
def __init__(self) -> None:
super().__init__(
instructions=(
"You are a friendly voice assistant. Keep replies to one or two short "
"sentences. Start the call by saying: Hi, how can I help?"
)
)
@function_tool
async def current_time(self, context: RunContext) -> str:
"""Get the current local time."""
return datetime.now().strftime("%H:%M")
server = AgentServer()
@server.rtc_session()
async def entrypoint(ctx: JobContext) -> None:
session = AgentSession(
# Dialog-RSN-1 hears the caller, decides when they've finished, and streams the reply
# as text. No STT, turn detector or separate LLM.
llm=polyai.realtime.RealtimeModel(),
# Dialog-RSN-1 replies in text, so a TTS speaks each reply as it streams.
# Sarah works on the ElevenLabs free tier.
tts=elevenlabs.TTS(voice_id="EXAVITQu4vr4xnSDxMaL", model="eleven_flash_v2_5"),
)
await session.start(agent=Assistant(), room=ctx.room)
# the greeting comes from the instructions: Dialog-RSN-1 ignores per-reply instructions
await session.generate_reply()
if __name__ == "__main__":
cli.run_app(server)