What is session.on method do
# ask-ai
w
What is session.on method do
t
Thanks for reaching out, Arthur. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The session.on method is your go-to for attaching event handlers in a LiveKit session. It’s super handy because it lets you listen for and react to different events, like: • User state changes: For example,
session.on("user_state_changed", callback)
helps you keep track of when someone starts or stops speaking. • Agent state changes: For example,
session.on("agent_state_changed", callback)
lets you monitor changes like when an agent is initializing or speaking. • Transcription events: For example,
session.on("user_input_transcribed", callback)
is great for handling finalized speech transcriptions from users. • Conversation events: For example,
session.on("conversation_item_added", callback)
helps you catch new messages as they come into the conversation. Using this method is key to creating interactive and responsive apps with LiveKit. If you have any questions or need a hand setting it up, just let me know! Sources: Turn detection and interruptions | LiveKit Docs | basics/repeater.py | tests/test_agent_session.py
w
how can I use it with metrics like STTMetrics, etc?
t
To use session.on with metrics like STTMetrics, here’s a friendly step-by-step guide to help you out: 1. Import Required Modules: First, bring in the necessary modules from LiveKit:
Copy code
from livekit.agents import AgentSession, MetricsCollectedEvent
from livekit.agents.metrics import STTMetrics
2. Implement the Event Handler: Next, create a function that listens for the
metrics_collected
event and handles the `STTMetrics`:
Copy code
@session.on("metrics_collected")
def on_stt_metrics_collected(ev: MetricsCollectedEvent):
    if isinstance(ev.metrics, STTMetrics):
        print(f"STT Metrics - Audio Duration: {ev.metrics.audio_duration}s")
        print(f"Total Duration: {ev.metrics.duration}s")
3. Log or Process Metrics: You can also use the handy
log_metrics
function for nicely formatted logging:
Copy code
from livekit.agents.metrics import log_metrics

@session.on("metrics_collected")
def on_stt_metrics_collected(ev: MetricsCollectedEvent):
    log_metrics(ev.metrics)
4. Aggregate Metrics (Optional): If you want to gather metrics over the whole session, the
UsageCollector
is your friend:
Copy code
from livekit.agents.metrics import UsageCollector

usage_collector = UsageCollector()

@session.on("metrics_collected")
def on_stt_metrics_collected(ev: MetricsCollectedEvent):
    usage_collector.collect(ev.metrics)

async def log_usage():
    summary = usage_collector.get_summary()
    print(f"Usage Summary: {summary}")

session.add_shutdown_callback(log_usage)
This way, you can easily handle and work with metrics like STTMetrics. If anything’s unclear or you’d like to see more examples, just let me know—I’m here to help!
w
What is the difference between the metrics_collected_event and the STT mertics event?
t
The metrics_collected_event and STT (Speech-to-Text) metrics events in LiveKit actually have different roles, so let me break it down for you: • metrics_collected_event: This is a general event that the
AgentSession
sends out whenever it gathers any metrics. It can include info from different parts like LLM, TTS, STT, and VAD. Think of it as a way to log and gather overall usage stats throughout the session. • STT Metrics Event: This one’s all about Speech-to-Text specifically. It gives you details such as: Basically, it zooms in on how well the speech recognition and audio processing are performing. ◦ `audio_duration`: How long the audio input is, in seconds. ◦ `duration`: How much time it took to process the audio (this is 0 if it’s streaming STT). ◦ `streamed`: Whether the STT is running in streaming mode. Here are the key differences to keep in mind: • Scope: The
metrics_collected_event
covers a wide range of metrics, while the STT metrics events are all about speech-to-text. • Content:
metrics_collected_event
pulls in data from multiple components, whereas STT metrics focus just on transcription performance. • Usage: Use
metrics_collected_event
when you want general metrics, and turn to STT metrics events when you’re specifically monitoring speech-to-text functionality. If anything’s unclear or you want to dive deeper, just let me know—I’m happy to help! Sources: Logs, metrics, and telemetry | LiveKit Docs | metrics/send-metrics-to-3p/send_metrics_to_3p.py | livekit-agents/livekit/agents/voice/agent_activity.py
w
So is metrics_collected_events base class and STTmetrics its derived class?
t
No, metrics_collected_event isn’t a base class for STTMetrics. Think of metrics_collected_event as an event class that holds different types of metrics data, including STTMetrics, LLMMetrics, and TTSMetrics. Each metric class like STTMetrics stands on its own and doesn’t inherit from metrics_collected_event. If you want me to explain it more or have any other questions, just let me know!