11import logging
22
33from dotenv import load_dotenv
4+ from livekit import rtc
45from 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)
1615from livekit .plugins import noise_cancellation , silero
1716from 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+
5153def 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
131125if __name__ == "__main__" :
132- cli .run_app (WorkerOptions ( entrypoint_fnc = entrypoint , prewarm_fnc = prewarm ) )
126+ cli .run_app (server )
0 commit comments