how to use text_input_cb in agent
# ask-ai
l
how to use text_input_cb in agent
t
Thanks for reaching out, Piyush. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Here’s a friendly guide to using
text_input_cb
in an agent, step by step: 1. Create a Custom Callback Function: Write an asynchronous function that takes
AgentSession
and
TextInputEvent
as inputs. This function will be your go-to for handling any text input that comes in. 2. Set Up RoomInputOptions: Point
text_input_cb
to your custom function and make sure text input is enabled by setting
text_enabled=True
. 3. Build Your Text Handling Logic: Inside your callback, you can do things like pause ongoing tasks or create a reply based on what the user typed. 4. Kick Off the Agent Session: Start the session with your configured options so it’s ready to handle text input smoothly. Check out this example to see how it all fits together:
Copy code
from livekit.agents import AgentSession, RoomInputOptions, TextInputEvent

async def custom_text_input_callback(sess: AgentSession, ev: TextInputEvent) -> None:
    # Pause anything currently happening
    sess.interrupt()
    # Craft a reply using the user's input
    sess.generate_reply(user_input=ev.text)

# Set up RoomInputOptions with your callback
input_options = RoomInputOptions(
    text_enabled=True,
    text_input_cb=custom_text_input_callback
)

# Start the session with these input options
await session.start(
    agent=MyAgent(),
    room=ctx.room,
    room_input_options=input_options
)
Feel free to tweak the callback to add extra features like logging, processing data, or triggering specific actions based on what’s typed. If you have any questions or want to explore more, I’m here to help! Sources: examples/other/chat-stream-receiver.py | Text and transcriptions | LiveKit Docs | Text and transcriptions | LiveKit Docs