how to add agent text response listener?
# ask-ai
b
how to add agent text response listener?
t
Thanks for reaching out, Rongrong. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're exploring how to add an agent text response listener in LiveKit! To do this, you'll want to subscribe to the
lk.transcription
text stream topic. This is the channel where LiveKit Agents share real-time text responses and transcriptions. Let me walk you through the main steps: 1. Connect to the Room: Make sure you use a token with the right permissions, like
room_join
, to connect your client to the LiveKit room. 2. Register a Text Stream Handler: Use the
room.register_text_stream_handler()
method to set up a listener for the
lk.transcription
topic. This handler will activate whenever a text stream is published on that topic. 3. Process Incoming Text Streams: When you receive text chunks, read them using the
TextStreamReader
. You can tell apart user transcriptions from agent responses by checking if the
lk.transcribed_track_id
attribute is present. Here's a quick example in Python to illustrate:
Copy code
from livekit import rtc

async def on_text_received(reader: rtc.TextStreamReader, participant_info: dict):
    async for text in reader:
        print(text)

room.register_text_stream_handler(topic="lk.transcription", handler=on_text_received)
If you'd like, I’m here to help with more details or examples tailored to a specific programming language or platform. Just let me know! Sources: livekit-agents/livekit/agents/voice/room_io/room_io.py | How to Set Up Speech-to-Text Input with Text-Only Output in LiveKit Agents | Text and transcriptions | LiveKit Docs