Skip to content

Commit 0c8a6c8

Browse files
authored
Merge pull request #32 from livekit-examples/kat/dpro-563-upgrade-python-example-to-agents-13
1.3 update
2 parents 3121b12 + 168d892 commit 0c8a6c8

File tree

3 files changed

+20
-26
lines changed

3 files changed

+20
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,4 @@ You can also self-host LiveKit instead of using LiveKit Cloud. See the [self-hos
139139

140140
## License
141141

142-
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
142+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ description = "Simple voice AI assistant built with LiveKit Agents for Python"
99
requires-python = ">=3.9"
1010

1111
dependencies = [
12-
"livekit-agents[silero,turn-detector]~=1.2",
12+
"livekit-agents[silero,turn-detector]~=1.3",
1313
"livekit-plugins-noise-cancellation~=0.2",
1414
"python-dotenv",
1515
]

src/agent.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import logging
22

33
from dotenv import load_dotenv
4+
from livekit import rtc
45
from livekit.agents import (
56
Agent,
7+
AgentServer,
68
AgentSession,
79
JobContext,
810
JobProcess,
9-
MetricsCollectedEvent,
10-
RoomInputOptions,
11-
WorkerOptions,
1211
cli,
1312
inference,
14-
metrics,
13+
room_io,
1514
)
1615
from livekit.plugins import noise_cancellation, silero
1716
from livekit.plugins.turn_detector.multilingual import MultilingualModel
@@ -48,11 +47,18 @@ def __init__(self) -> None:
4847
# return "sunny with a temperature of 70 degrees."
4948

5049

50+
server = AgentServer()
51+
52+
5153
def prewarm(proc: JobProcess):
5254
proc.userdata["vad"] = silero.VAD.load()
5355

5456

55-
async def entrypoint(ctx: JobContext):
57+
server.setup_fnc = prewarm
58+
59+
60+
@server.rtc_session()
61+
async def my_agent(ctx: JobContext):
5662
# Logging setup
5763
# Add any other context you want in all log entries here
5864
ctx.log_context_fields = {
@@ -91,21 +97,6 @@ async def entrypoint(ctx: JobContext):
9197
# llm=openai.realtime.RealtimeModel(voice="marin")
9298
# )
9399

94-
# Metrics collection, to measure pipeline performance
95-
# For more information, see https://docs.livekit.io/agents/build/metrics/
96-
usage_collector = metrics.UsageCollector()
97-
98-
@session.on("metrics_collected")
99-
def _on_metrics_collected(ev: MetricsCollectedEvent):
100-
metrics.log_metrics(ev.metrics)
101-
usage_collector.collect(ev.metrics)
102-
103-
async def log_usage():
104-
summary = usage_collector.get_summary()
105-
logger.info(f"Usage: {summary}")
106-
107-
ctx.add_shutdown_callback(log_usage)
108-
109100
# # Add a virtual avatar to the session, if desired
110101
# # For other providers, see https://docs.livekit.io/agents/models/avatar/
111102
# avatar = hedra.AvatarSession(
@@ -118,9 +109,12 @@ async def log_usage():
118109
await session.start(
119110
agent=Assistant(),
120111
room=ctx.room,
121-
room_input_options=RoomInputOptions(
122-
# For telephony applications, use `BVCTelephony` for best results
123-
noise_cancellation=noise_cancellation.BVC(),
112+
room_options=room_io.RoomOptions(
113+
audio_input=room_io.AudioInputOptions(
114+
noise_cancellation=lambda params: noise_cancellation.BVCTelephony()
115+
if params.participant.kind == rtc.ParticipantKind.PARTICIPANT_KIND_SIP
116+
else noise_cancellation.BVC(),
117+
),
124118
),
125119
)
126120

@@ -129,4 +123,4 @@ async def log_usage():
129123

130124

131125
if __name__ == "__main__":
132-
cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint, prewarm_fnc=prewarm))
126+
cli.run_app(server)

0 commit comments

Comments
 (0)