How do I force the agent to speak first and say a ...
# ask-ai
m
How do I force the agent to speak first and say a canned message? For example "Hello, John"
t
Thanks for reaching out, Fotis. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To gently guide the agent to speak first with a friendly message like "Hello, John," you can use the
session.say()
method within your agent's entrypoint or lifecycle hook. Here's a simple way to set it up: Using
session.say()
for a Predefined Message:
• Just add this code in the
entrypoint
function right after the session starts:
Copy code
await session.say("Hello, John")
This will use the Text-to-Speech (TTS) plugin you have configured to warmly deliver the message to your user. You can also include
allow_interruptions=True
if you'd like to let users jump in and interrupt the message. Personalized Greetings: If you'd like to make the greeting feel more personal (like "Hello, John"), you can fetch user-specific info from: • Job Metadata: Pass user details along in the dispatch request and then parse them in the
entrypoint
. • Room Metadata: Access properties like the room's name and other metadata. • Participant Attributes: Pull user details from participant attributes. Here's an example to illustrate:
Copy code
async def entrypoint(ctx: JobContext):
    user_name = "there"
    if ctx.job.metadata:
        metadata = json.loads(ctx.job.metadata)
        user_name = metadata.get("user_name", "there")

    session = AgentSession()
    await session.start(agent=MyAgent(), room=ctx.room)
    await session.say(f"Hello, {user_name}! How can I help you today?", allow_interruptions=True)
If you have any questions or need a hand with the implementation, please don’t hesitate to reach out—I’m here to help! Sources: pipeline-tts/short_replies_only.py | Agent speech and audio | LiveKit Docs | delay-handling/agent.py | examples/warm-transfer/warm_transfer.py | Job lifecycle | LiveKit Docs