transports: allow sending metrics

This commit is contained in:
Aleix Conchillo Flaqué
2024-06-06 14:06:01 -07:00
parent 390582d7f3
commit a1f1d1995c
3 changed files with 19 additions and 2 deletions

View File

@@ -241,7 +241,7 @@ class StopInterruptionFrame(SystemFrame):
@dataclass @dataclass
class MetricsFrame(SystemFrame): class MetricsFrame(SystemFrame):
"""Emitted by processor who can compute metrics like latencies. """Emitted by processor that can compute metrics like latencies.
""" """
ttfb: Mapping[str, float] ttfb: Mapping[str, float]

View File

@@ -20,6 +20,7 @@ from pipecat.processors.frame_processor import FrameDirection, FrameProcessor
from pipecat.frames.frames import ( from pipecat.frames.frames import (
AudioRawFrame, AudioRawFrame,
CancelFrame, CancelFrame,
MetricsFrame,
SpriteFrame, SpriteFrame,
StartFrame, StartFrame,
EndFrame, EndFrame,
@@ -87,6 +88,9 @@ class BaseOutputTransport(FrameProcessor):
def send_message(self, frame: TransportMessageFrame): def send_message(self, frame: TransportMessageFrame):
pass pass
def send_metrics(self, frame: MetricsFrame):
pass
def write_frame_to_camera(self, frame: ImageRawFrame): def write_frame_to_camera(self, frame: ImageRawFrame):
pass pass
@@ -166,6 +170,8 @@ class BaseOutputTransport(FrameProcessor):
self._set_camera_images(frame.images) self._set_camera_images(frame.images)
elif isinstance(frame, TransportMessageFrame): elif isinstance(frame, TransportMessageFrame):
self.send_message(frame) self.send_message(frame)
elif isinstance(frame, MetricsFrame):
self.send_metrics(frame)
else: else:
future = asyncio.run_coroutine_threadsafe( future = asyncio.run_coroutine_threadsafe(
self._internal_push_frame(frame), self.get_event_loop()) self._internal_push_frame(frame), self.get_event_loop())

View File

@@ -27,6 +27,7 @@ from pipecat.frames.frames import (
Frame, Frame,
ImageRawFrame, ImageRawFrame,
InterimTranscriptionFrame, InterimTranscriptionFrame,
MetricsFrame,
SpriteFrame, SpriteFrame,
StartFrame, StartFrame,
TranscriptionFrame, TranscriptionFrame,
@@ -638,6 +639,16 @@ class DailyOutputTransport(BaseOutputTransport):
def send_message(self, frame: DailyTransportMessageFrame): def send_message(self, frame: DailyTransportMessageFrame):
self._client.send_message(frame) self._client.send_message(frame)
def send_metrics(self, frame: MetricsFrame):
ttfb = [{"name": n, "time": t} for n, t in frame.ttfb.items()]
message = DailyTransportMessageFrame(message={
"type": "pipecat-metrics",
"metrics": {
"ttfb": ttfb
},
})
self._client.send_message(message)
def write_raw_audio_frames(self, frames: bytes): def write_raw_audio_frames(self, frames: bytes):
self._client.write_raw_audio_frames(frames) self._client.write_raw_audio_frames(frames)
@@ -711,7 +722,7 @@ class DailyTransport(BaseTransport):
# DailyTransport # DailyTransport
# #
@property @ property
def participant_id(self) -> str: def participant_id(self) -> str:
return self._client.participant_id return self._client.participant_id