In my agent log. I want to log user_input after st...
# ask-ai
m
In my agent log. I want to log user_input after stt extaction User input : [text] agent output after TTS completion Agent Output : [text] how to do this
t
Thanks for reaching out, Rajadurai. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to help you log user input after STT extraction and agent output after TTS completion. Here’s a friendly guide to get you started: • Log User Input: Keep an eye on the
user_input_transcribed
event, which captures the user’s audio transcriptions. It’s best to log the transcription when the
isFinal
flag is true, so you’re capturing the complete input. • Log Agent Output: Watch the
conversation_item_added
event to catch agent responses. This event fires whenever a message is added to the chat history, including the TTS outputs. • Access Full History: You can use
session.history
to pull all user and agent messages together. Saving this log at the end of the session with a shutdown callback can be really handy. • Customize Pipelines: If you want more control, try overriding methods like
stt_node()
and
tts_node()
to add your own logging logic before or after processing. • Enable Synchronized Transcripts: For really precise logging of what was spoken, turn on
use_tts_aligned_transcript=True
in your configuration. This helps ensure your log matches the actual spoken output. These steps should give you a solid and thorough way to log both user and agent interactions. If you’d like, I’m here to walk you through any part or provide examples—just let me know! Sources: Pipeline nodes and hooks | LiveKit docs | Text and transcriptions | LiveKit docs | agents/src/voice/agent_activity.ts | Session recording and transcripts | LiveKit Docs | livekit-plugins/livekit-plugins-sarvam/livekit/plugins/sarvam/stt.py
m
give a python code
t
Thank you for reaching out! Here's a Python code example that gently guides you through logging user input right after the STT extraction and also captures the agent's output once TTS completes:
Copy code
from datetime import datetime
from livekit.agents import AgentSession, ChatMessage
from livekit.plugins import deepgram, openai

async def main():
    session = AgentSession(
        stt=deepgram.STT(),
        llm=openai.LLM(),
        tts=openai.TTS(voice="alloy"),
        use_tts_aligned_transcript=True
    )

    # Log user input after STT
    @session.on("user_input_transcribed")
    def log_user_input(transcript):
        if transcript.is_final:
            timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            with open("user_input_log.txt", "a") as log_file:
                log_file.write(f"[{timestamp}] User Input: {transcript.transcript}\n")

    # Log agent output after TTS
    @session.on("conversation_item_added")
    def log_agent_output(msg: ChatMessage):
        if msg.role == "assistant" and msg.text_content:
            timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            with open("agent_output_log.txt", "a") as log_file:
                log_file.write(f"[{timestamp}] Agent Output: {msg.text_content}\n")

    await session.start()

# Run the session
import asyncio
asyncio.run(main())
This snippet listens for the
user_input_transcribed
event to carefully log what the user says after STT, and the
conversation_item_added
event to capture the agent's responses after TTS. If you have any questions or want to explore this further, please don’t hesitate to ask—I’m here to help! Sources: Text and transcriptions | LiveKit docs | metrics/metrics_tts.py