Skip to content
Open
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
51 changes: 28 additions & 23 deletions livekit-agents/livekit/agents/voice/agent_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -1823,6 +1823,7 @@ def on_end_of_turn(self, info: _EndOfTurnInfo) -> bool:
content=[info.new_transcript],
transcript_confidence=info.transcript_confidence,
)
user_message.metrics = self._init_metrics_from_end_of_turn(info)
self._agent._chat_ctx.items.append(user_message)
self._session._conversation_item_added(user_message)

Expand Down Expand Up @@ -1877,6 +1878,11 @@ async def _user_turn_completed_task(
transcript_confidence=info.transcript_confidence,
)

metrics_report: llm.MetricsReport = self._init_metrics_from_end_of_turn(info)

if user_message is not None:
user_message.metrics = metrics_report

if isinstance(self.llm, llm.RealtimeModel):
if self.llm.capabilities.turn_detection:
return
Expand Down Expand Up @@ -1936,6 +1942,7 @@ async def _user_turn_completed_task(
return

on_user_turn_completed_delay = time.perf_counter() - start_time
metrics_report["on_user_turn_completed_delay"] = on_user_turn_completed_delay

if isinstance(self.llm, llm.RealtimeModel):
# ignore stt transcription for realtime model
Expand All @@ -1953,29 +1960,6 @@ async def _user_turn_completed_task(
self._session._conversation_item_added(user_message)
return

metrics_report: llm.MetricsReport = {}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being down here, we lose the opportunity to attach metrics for early returns above

if self.stt:
metrics_report["stt_metadata"] = {
"model_name": self.stt.model,
"model_provider": self.stt.provider,
}
if info.started_speaking_at is not None:
metrics_report["started_speaking_at"] = info.started_speaking_at

if info.stopped_speaking_at is not None:
metrics_report["stopped_speaking_at"] = info.stopped_speaking_at

if info.transcription_delay is not None:
metrics_report["transcription_delay"] = info.transcription_delay

if info.end_of_turn_delay is not None:
metrics_report["end_of_turn_delay"] = info.end_of_turn_delay

metrics_report["on_user_turn_completed_delay"] = on_user_turn_completed_delay

if user_message is not None:
user_message.metrics = metrics_report

speech_handle: SpeechHandle | None = None
if preemptive := self._preemptive_generation:
# make sure the on_user_turn_completed didn't change some request parameters
Expand Down Expand Up @@ -3401,6 +3385,27 @@ def _fallback_to_vad_interruption(self) -> None:
"falling back to VAD-based interruption"
)

def _init_metrics_from_end_of_turn(self, info: _EndOfTurnInfo) -> llm.MetricsReport:
metrics_report: llm.MetricsReport = {}
if self.stt:
metrics_report["stt_metadata"] = {
"model_name": self.stt.model,
"model_provider": self.stt.provider,
}
if info.started_speaking_at is not None:
metrics_report["started_speaking_at"] = info.started_speaking_at

if info.stopped_speaking_at is not None:
metrics_report["stopped_speaking_at"] = info.stopped_speaking_at

if info.transcription_delay is not None:
metrics_report["transcription_delay"] = info.transcription_delay

if info.end_of_turn_delay is not None:
metrics_report["end_of_turn_delay"] = info.end_of_turn_delay

return metrics_report

# move them to the end to avoid shadowing the same named modules for mypy
@property
def vad(self) -> vad.VAD | None:
Expand Down