Add a second observer to the 30-observer.py example
This commit is contained in:
@@ -19,6 +19,9 @@ from pipecat.frames.frames import (
|
|||||||
BotStoppedSpeakingFrame,
|
BotStoppedSpeakingFrame,
|
||||||
EndFrame,
|
EndFrame,
|
||||||
Frame,
|
Frame,
|
||||||
|
LLMFullResponseEndFrame,
|
||||||
|
LLMFullResponseStartFrame,
|
||||||
|
LLMTextFrame,
|
||||||
StartInterruptionFrame,
|
StartInterruptionFrame,
|
||||||
)
|
)
|
||||||
from pipecat.observers.base_observer import BaseObserver
|
from pipecat.observers.base_observer import BaseObserver
|
||||||
@@ -71,6 +74,38 @@ class DebugObserver(BaseObserver):
|
|||||||
logger.info(f"🤖 BOT STOP SPEAKING: {src} {arrow} {dst} at {time_sec:.2f}s")
|
logger.info(f"🤖 BOT STOP SPEAKING: {src} {arrow} {dst} at {time_sec:.2f}s")
|
||||||
|
|
||||||
|
|
||||||
|
class LLMLogObserver(BaseObserver):
|
||||||
|
"""Observer to log LLM activity to the console.
|
||||||
|
|
||||||
|
Logs all frame instances of:
|
||||||
|
- LLMFullResponseStartFrame (only from LLM service)
|
||||||
|
- LLMTextFrame
|
||||||
|
- LLMFullResponseEndFrame (only from LLM service)
|
||||||
|
|
||||||
|
This allows you to track when the LLM starts responding, what it generates, and when it finishes.
|
||||||
|
Log format: [LLM EVENT]: [details] at [timestamp]s
|
||||||
|
"""
|
||||||
|
|
||||||
|
async def on_push_frame(
|
||||||
|
self,
|
||||||
|
src: FrameProcessor,
|
||||||
|
dst: FrameProcessor,
|
||||||
|
frame: Frame,
|
||||||
|
direction: FrameDirection,
|
||||||
|
timestamp: int,
|
||||||
|
):
|
||||||
|
time_sec = timestamp / 1_000_000_000
|
||||||
|
|
||||||
|
# Only log start/end frames from OpenAILLMService
|
||||||
|
if isinstance(frame, (LLMFullResponseStartFrame, LLMFullResponseEndFrame)):
|
||||||
|
if isinstance(src, OpenAILLMService):
|
||||||
|
event = "START" if isinstance(frame, LLMFullResponseStartFrame) else "END"
|
||||||
|
logger.info(f"🧠 LLM {event} RESPONSE at {time_sec:.2f}s")
|
||||||
|
# Log all LLMTextFrames
|
||||||
|
elif isinstance(frame, LLMTextFrame):
|
||||||
|
logger.info(f"🧠 LLM GENERATING: {frame.text!r} at {time_sec:.2f}s")
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
(room_url, token) = await configure(session)
|
(room_url, token) = await configure(session)
|
||||||
@@ -122,7 +157,7 @@ async def main():
|
|||||||
enable_metrics=True,
|
enable_metrics=True,
|
||||||
enable_usage_metrics=True,
|
enable_usage_metrics=True,
|
||||||
report_only_initial_ttfb=True,
|
report_only_initial_ttfb=True,
|
||||||
observers=[DebugObserver()],
|
observers=[DebugObserver(), LLMLogObserver()],
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user