-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathagent.py
70 lines (53 loc) · 1.94 KB
/
agent.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from __future__ import annotations
import logging
from dotenv import load_dotenv
from livekit import rtc
from livekit.agents import (
AutoSubscribe,
JobContext,
WorkerOptions,
cli,
llm,
)
from livekit.agents.multimodal import MultimodalAgent
from livekit.plugins import openai
load_dotenv(dotenv_path=".env.local")
logger = logging.getLogger("my-worker")
logger.setLevel(logging.INFO)
async def entrypoint(ctx: JobContext):
logger.info(f"connecting to room {ctx.room.name}")
await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)
participant = await ctx.wait_for_participant()
run_multimodal_agent(ctx, participant)
logger.info("agent started")
def run_multimodal_agent(ctx: JobContext, participant: rtc.RemoteParticipant):
logger.info("starting multimodal agent")
model = openai.realtime.RealtimeModel(
instructions=(
"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."
),
modalities=["audio", "text"],
)
# create a chat context with chat history, these will be synchronized with the server
# upon session establishment
chat_ctx = llm.ChatContext()
chat_ctx.append(
text="Context about the user: you are talking to a software engineer who's building voice AI applications."
"Greet the user with a friendly greeting and ask how you can help them today.",
role="assistant",
)
agent = MultimodalAgent(
model=model,
chat_ctx=chat_ctx,
)
agent.start(ctx.room, participant)
# to enable the agent to speak first
agent.generate_reply()
if __name__ == "__main__":
cli.run_app(
WorkerOptions(
entrypoint_fnc=entrypoint,
)
)