Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions examples/bank-ivr/ivr_navigator_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,12 @@ async def dtmf_session(ctx: JobContext) -> None:
)
logger.info(f"==> User request: {user_request}")

usage_collector = metrics.UsageCollector()

@session.on("metrics_collected")
def _on_metrics_collected(ev: MetricsCollectedEvent) -> None:
metrics.log_metrics(ev.metrics)
usage_collector.collect(ev.metrics)

async def log_usage() -> None:
summary = usage_collector.get_summary()
logger.info(f"Usage: {summary}")
logger.info(f"Usage: {session.usage}")

ctx.add_shutdown_callback(log_usage)

Expand Down
6 changes: 1 addition & 5 deletions examples/bank-ivr/ivr_system_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,16 +645,12 @@ async def bank_ivr_session(ctx: JobContext) -> None:
userdata=state,
)

usage_collector = metrics.UsageCollector()

@session.on("metrics_collected")
def _on_metrics(ev: MetricsCollectedEvent) -> None:
metrics.log_metrics(ev.metrics)
usage_collector.collect(ev.metrics)

async def log_usage() -> None:
summary = usage_collector.get_summary()
logger.info("Usage summary: %s", summary)
logger.info("Usage summary: %s", session.usage)

ctx.add_shutdown_callback(log_usage)

Expand Down
6 changes: 1 addition & 5 deletions examples/dtmf/basic_dtmf_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,12 @@ async def entrypoint(ctx: JobContext) -> None:
turn_detection=MultilingualModel(),
)

usage_collector = metrics.UsageCollector()

@session.on("metrics_collected")
def _on_metrics_collected(ev: MetricsCollectedEvent) -> None:
metrics.log_metrics(ev.metrics)
usage_collector.collect(ev.metrics)

async def log_usage() -> None:
summary = usage_collector.get_summary()
logger.info(f"Usage: {summary}")
logger.info(f"Usage: {session.usage}")

ctx.add_shutdown_callback(log_usage)

Expand Down
6 changes: 1 addition & 5 deletions examples/survey/survey_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
cli,
inference,
llm,
metrics,
room_io,
)
from livekit.agents.beta.workflows import GetEmailTask, TaskGroup
Expand Down Expand Up @@ -347,11 +346,8 @@ async def entrypoint(ctx: JobContext):
preemptive_generation=True,
)

usage_collector = metrics.UsageCollector()

async def log_usage():
summary = usage_collector.get_summary()
logger.info(f"Usage: {summary}")
logger.info(f"Usage: {session.usage}")

ctx.add_shutdown_callback(log_usage)

Expand Down
21 changes: 10 additions & 11 deletions examples/voice_agents/basic_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
metrics,
room_io,
)
from livekit.agents.beta.tools import EndCallTool
from livekit.agents.llm import function_tool
from livekit.plugins import silero
from livekit.plugins.turn_detector.multilingual import MultilingualModel
Expand All @@ -35,9 +36,10 @@ def __init__(self) -> None:
"do not use emojis, asterisks, markdown, or other special characters in your responses."
"You are curious and friendly, and have a sense of humor."
"you will speak english to the user",
tools=[EndCallTool()],
)

async def on_enter(self):
async def on_enter(self) -> None:
# when the agent is added to the session, it'll generate a reply
# according to its instructions
# Keep it uninterruptible so the client has time to calibrate AEC (Acoustic Echo Cancellation).
Expand All @@ -48,7 +50,7 @@ async def on_enter(self):
@function_tool
async def lookup_weather(
self, context: RunContext, location: str, latitude: str, longitude: str
):
) -> str:
"""Called when the user asks for weather related information.
Ensure the user's location (city or region) is provided.
When given a location, please estimate the latitude and longitude of the location and
Expand All @@ -68,20 +70,20 @@ async def lookup_weather(
server = AgentServer()


def prewarm(proc: JobProcess):
def prewarm(proc: JobProcess) -> None:
proc.userdata["vad"] = silero.VAD.load()


server.setup_fnc = prewarm


@server.rtc_session()
async def entrypoint(ctx: JobContext):
async def entrypoint(ctx: JobContext) -> None:
# each log entry will include these fields
ctx.log_context_fields = {
"room": ctx.room.name,
}
session = AgentSession(
session: AgentSession = AgentSession(
# Speech-to-text (STT) is your agent's ears, turning the user's speech into text that the LLM can understand
# See all available models at https://docs.livekit.io/agents/models/stt/
stt=inference.STT("deepgram/nova-3", language="multi"),
Expand All @@ -95,6 +97,7 @@ async def entrypoint(ctx: JobContext):
# See more at https://docs.livekit.io/agents/build/turns
turn_detection=MultilingualModel(),
vad=ctx.proc.userdata["vad"],
bargein_detector=inference.BargeinDetector(),
# allow the LLM to generate a response while waiting for the end of turn
# See more at https://docs.livekit.io/agents/build/audio/#preemptive-generation
preemptive_generation=True,
Expand All @@ -105,16 +108,12 @@ async def entrypoint(ctx: JobContext):
)

# log metrics as they are emitted, and total usage after session is over
usage_collector = metrics.UsageCollector()

@session.on("metrics_collected")
def _on_metrics_collected(ev: MetricsCollectedEvent):
def _on_metrics_collected(ev: MetricsCollectedEvent) -> None:
metrics.log_metrics(ev.metrics)
usage_collector.collect(ev.metrics)

async def log_usage():
summary = usage_collector.get_summary()
logger.info(f"Usage: {summary}")
logger.info(f"Usage: {session.usage}")

# shutdown callbacks are triggered when the session is over
ctx.add_shutdown_callback(log_usage)
Expand Down
6 changes: 1 addition & 5 deletions examples/voice_agents/multi_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,12 @@ async def entrypoint(ctx: JobContext):
)

# log metrics as they are emitted, and total usage after session is over
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}")
logger.info(f"Usage: {session.usage}")

ctx.add_shutdown_callback(log_usage)

Expand Down
5 changes: 1 addition & 4 deletions examples/voice_agents/zapier_mcp_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,9 @@ async def entrypoint(ctx: JobContext):
participant = await ctx.wait_for_participant()
logger.info(f"starting voice assistant for participant {participant.identity}")

usage_collector = metrics.UsageCollector()

# Log metrics and collect usage data
# Log metrics as they are collected
def on_metrics_collected(agent_metrics: metrics.AgentMetrics):
metrics.log_metrics(agent_metrics)
usage_collector.collect(agent_metrics)

# Get MCP server URL from environment variable
zapier_mcp_server = os.getenv("ZAPIER_MCP_SERVER")
Expand Down
10 changes: 10 additions & 0 deletions livekit-agents/livekit/agents/inference/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
from .interruption import (
AdaptiveInterruptionDetector,
InterruptionDetectionError,
InterruptionEvent,
InterruptionEventType,
)
from .llm import LLM, LLMModels, LLMStream
from .stt import STT, STTModels
from .tts import TTS, TTSModels
Expand All @@ -10,4 +16,8 @@
"STTModels",
"TTSModels",
"LLMModels",
"AdaptiveInterruptionDetector",
"InterruptionEvent",
"InterruptionDetectionError",
"InterruptionEventType",
]
Loading
Loading