Merge pull request #32 from livekit-examples/kat/dpro-563-upgrade-python-example-to-agents-13
1.3 update
This commit is contained in:
+1
-1
@@ -9,7 +9,7 @@ description = "Simple voice AI assistant built with LiveKit Agents for Python"
|
||||
requires-python = ">=3.9"
|
||||
|
||||
dependencies = [
|
||||
"livekit-agents[silero,turn-detector]~=1.2",
|
||||
"livekit-agents[silero,turn-detector]~=1.3",
|
||||
"livekit-plugins-noise-cancellation~=0.2",
|
||||
"python-dotenv",
|
||||
]
|
||||
|
||||
+18
-24
@@ -1,17 +1,16 @@
|
||||
import logging
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from livekit import rtc
|
||||
from livekit.agents import (
|
||||
Agent,
|
||||
AgentServer,
|
||||
AgentSession,
|
||||
JobContext,
|
||||
JobProcess,
|
||||
MetricsCollectedEvent,
|
||||
RoomInputOptions,
|
||||
WorkerOptions,
|
||||
cli,
|
||||
inference,
|
||||
metrics,
|
||||
room_io,
|
||||
)
|
||||
from livekit.plugins import noise_cancellation, silero
|
||||
from livekit.plugins.turn_detector.multilingual import MultilingualModel
|
||||
@@ -48,11 +47,18 @@ class Assistant(Agent):
|
||||
# return "sunny with a temperature of 70 degrees."
|
||||
|
||||
|
||||
server = AgentServer()
|
||||
|
||||
|
||||
def prewarm(proc: JobProcess):
|
||||
proc.userdata["vad"] = silero.VAD.load()
|
||||
|
||||
|
||||
async def entrypoint(ctx: JobContext):
|
||||
server.setup_fnc = prewarm
|
||||
|
||||
|
||||
@server.rtc_session()
|
||||
async def my_agent(ctx: JobContext):
|
||||
# Logging setup
|
||||
# Add any other context you want in all log entries here
|
||||
ctx.log_context_fields = {
|
||||
@@ -91,21 +97,6 @@ async def entrypoint(ctx: JobContext):
|
||||
# llm=openai.realtime.RealtimeModel(voice="marin")
|
||||
# )
|
||||
|
||||
# Metrics collection, to measure pipeline performance
|
||||
# For more information, see https://docs.livekit.io/agents/build/metrics/
|
||||
usage_collector = metrics.UsageCollector()
|
||||
|
||||
@session.on("metrics_collected")
|
||||
def _on_metrics_collected(ev: MetricsCollectedEvent):
|
||||
metrics.log_metrics(ev.metrics)
|
||||
usage_collector.collect(ev.metrics)
|
||||
|
||||
async def log_usage():
|
||||
summary = usage_collector.get_summary()
|
||||
logger.info(f"Usage: {summary}")
|
||||
|
||||
ctx.add_shutdown_callback(log_usage)
|
||||
|
||||
# # Add a virtual avatar to the session, if desired
|
||||
# # For other providers, see https://docs.livekit.io/agents/models/avatar/
|
||||
# avatar = hedra.AvatarSession(
|
||||
@@ -118,9 +109,12 @@ async def entrypoint(ctx: JobContext):
|
||||
await session.start(
|
||||
agent=Assistant(),
|
||||
room=ctx.room,
|
||||
room_input_options=RoomInputOptions(
|
||||
# For telephony applications, use `BVCTelephony` for best results
|
||||
noise_cancellation=noise_cancellation.BVC(),
|
||||
room_options=room_io.RoomOptions(
|
||||
audio_input=room_io.AudioInputOptions(
|
||||
noise_cancellation=lambda params: noise_cancellation.BVCTelephony()
|
||||
if params.participant.kind == rtc.ParticipantKind.PARTICIPANT_KIND_SIP
|
||||
else noise_cancellation.BVC(),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -129,4 +123,4 @@ async def entrypoint(ctx: JobContext):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint, prewarm_fnc=prewarm))
|
||||
cli.run_app(server)
|
||||
|
||||
Reference in New Issue
Block a user