Skip to content

Commit e208872

Browse files
chore: migrate to v1 of agents library (#32)
* chore: migrate to v1 of agents library
1 parent 444c58c commit e208872

File tree

3 files changed

+62
-45
lines changed

3 files changed

+62
-45
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ python3 -m venv venv
3939
venv\Scripts\activate
4040
pip install -r requirements.txt
4141
```
42-
</details>
4342

43+
</details>
4444

4545
Set up the environment by copying `.env.example` to `.env.local` and filling in the required values:
4646

@@ -60,7 +60,13 @@ lk app env
6060
Run the agent:
6161

6262
```console
63-
python3 agent.py dev
63+
python3 agent.py console
6464
```
6565

66-
This agent requires a frontend application to communicate with. You can use one of our example frontends in [livekit-examples](https://github.com/livekit-examples/), create your own following one of our [client quickstarts](https://docs.livekit.io/realtime/quickstarts/), or test instantly against one of our hosted [Sandbox](https://cloud.livekit.io/projects/p_/sandbox) frontends.
66+
This agent can use a frontend application to communicate with. You can use one of our example frontends in [livekit-examples](https://github.com/livekit-examples/), create your own following one of our [client quickstarts](https://docs.livekit.io/realtime/quickstarts/), or test instantly against one of our hosted [Sandbox](https://cloud.livekit.io/projects/p_/sandbox) frontends.
67+
68+
Run the agent with the following command when using a frontend application.
69+
70+
```console
71+
python3 agent.py dev
72+
```

agent.py

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,93 @@
22

33
from dotenv import load_dotenv
44
from livekit.agents import (
5+
Agent,
6+
AgentSession,
57
AutoSubscribe,
68
JobContext,
79
JobProcess,
810
WorkerOptions,
911
cli,
10-
llm,
1112
metrics,
13+
RoomInputOptions,
1214
)
13-
from livekit.agents.pipeline import VoicePipelineAgent
1415
from livekit.plugins import (
1516
cartesia,
1617
openai,
1718
deepgram,
1819
noise_cancellation,
1920
silero,
20-
turn_detector,
2121
)
22+
from livekit.plugins.turn_detector.multilingual import MultilingualModel
2223

2324

2425
load_dotenv(dotenv_path=".env.local")
2526
logger = logging.getLogger("voice-agent")
2627

2728

29+
class Assistant(Agent):
30+
def __init__(self) -> None:
31+
# This project is configured to use Deepgram STT, OpenAI LLM and Cartesia TTS plugins
32+
# Other great providers exist like Cerebras, ElevenLabs, Groq, Play.ht, Rime, and more
33+
# Learn more and pick the best one for your app:
34+
# https://docs.livekit.io/agents/plugins
35+
super().__init__(
36+
instructions="You are a voice assistant created by LiveKit. Your interface with users will be voice. "
37+
"You should use short and concise responses, and avoiding usage of unpronouncable punctuation. "
38+
"You were created as a demo to showcase the capabilities of LiveKit's agents framework.",
39+
stt=deepgram.STT(),
40+
llm=openai.LLM(model="gpt-4o-mini"),
41+
tts=cartesia.TTS(),
42+
# use LiveKit's transformer-based turn detector
43+
turn_detection=MultilingualModel(),
44+
)
45+
46+
async def on_enter(self):
47+
# The agent should be polite and greet the user when it joins :)
48+
self.session.generate_reply(
49+
instructions="Hey, how can I help you today?", allow_interruptions=True
50+
)
51+
52+
2853
def prewarm(proc: JobProcess):
2954
proc.userdata["vad"] = silero.VAD.load()
3055

3156

3257
async def entrypoint(ctx: JobContext):
33-
initial_ctx = llm.ChatContext().append(
34-
role="system",
35-
text=(
36-
"You are a voice assistant created by LiveKit. Your interface with users will be voice. "
37-
"You should use short and concise responses, and avoiding usage of unpronouncable punctuation. "
38-
"You were created as a demo to showcase the capabilities of LiveKit's agents framework."
39-
),
40-
)
41-
4258
logger.info(f"connecting to room {ctx.room.name}")
4359
await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)
4460

4561
# Wait for the first participant to connect
4662
participant = await ctx.wait_for_participant()
4763
logger.info(f"starting voice assistant for participant {participant.identity}")
4864

49-
# This project is configured to use Deepgram STT, OpenAI LLM and Cartesia TTS plugins
50-
# Other great providers exist like Cerebras, ElevenLabs, Groq, Play.ht, Rime, and more
51-
# Learn more and pick the best one for your app:
52-
# https://docs.livekit.io/agents/plugins
53-
agent = VoicePipelineAgent(
65+
usage_collector = metrics.UsageCollector()
66+
67+
# Log metrics and collect usage data
68+
def on_metrics_collected(agent_metrics: metrics.AgentMetrics):
69+
metrics.log_metrics(agent_metrics)
70+
usage_collector.collect(agent_metrics)
71+
72+
session = AgentSession(
5473
vad=ctx.proc.userdata["vad"],
55-
stt=deepgram.STT(),
56-
llm=openai.LLM(model="gpt-4o-mini"),
57-
tts=cartesia.TTS(),
58-
# use LiveKit's transformer-based turn detector
59-
turn_detector=turn_detector.EOUModel(),
6074
# minimum delay for endpointing, used when turn detector believes the user is done with their turn
6175
min_endpointing_delay=0.5,
6276
# maximum delay for endpointing, used when turn detector does not believe the user is done with their turn
6377
max_endpointing_delay=5.0,
64-
# enable background voice & noise cancellation, powered by Krisp
65-
# included at no additional cost with LiveKit Cloud
66-
noise_cancellation=noise_cancellation.BVC(),
67-
chat_ctx=initial_ctx,
6878
)
6979

70-
usage_collector = metrics.UsageCollector()
80+
# Trigger the on_metrics_collected function when metrics are collected
81+
session.on("metrics_collected", on_metrics_collected)
7182

72-
@agent.on("metrics_collected")
73-
def on_metrics_collected(agent_metrics: metrics.AgentMetrics):
74-
metrics.log_metrics(agent_metrics)
75-
usage_collector.collect(agent_metrics)
76-
77-
agent.start(ctx.room, participant)
78-
79-
# The agent should be polite and greet the user when it joins :)
80-
await agent.say("Hey, how can I help you today?", allow_interruptions=True)
83+
await session.start(
84+
room=ctx.room,
85+
agent=Assistant(),
86+
room_input_options=RoomInputOptions(
87+
# enable background voice & noise cancellation, powered by Krisp
88+
# included at no additional cost with LiveKit Cloud
89+
noise_cancellation=noise_cancellation.BVC(),
90+
),
91+
)
8192

8293

8394
if __name__ == "__main__":

requirements.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
livekit-agents>=0.12.17,<1.0.0
2-
livekit-plugins-openai>=0.11.2,<1.0.0
3-
livekit-plugins-cartesia>=0.4.10,<1.0.0
4-
livekit-plugins-deepgram>=0.7.0,<1.0.0
5-
livekit-plugins-silero>=0.7.5,<1.0.0
1+
livekit-agents~=1.0
2+
livekit-plugins-openai~=1.0
3+
livekit-plugins-cartesia~=1.0
4+
livekit-plugins-deepgram~=1.0
5+
livekit-plugins-silero~=1.0
66
# optional, only when using LiveKit's turn detection model
7-
livekit-plugins-turn-detector>=0.4.3,<1.0.0
7+
livekit-plugins-turn-detector~=1.0
88
# optional, only if background voice & noise cancellation is needed
99
livekit-plugins-noise-cancellation>=0.2.0,<1.0.0
1010
python-dotenv~=1.0

0 commit comments

Comments
 (0)