Whats the correct way to have an agent say somethi...
# ask-ai
w
Whats the correct way to have an agent say something as soon as the call starts, not waiting for the caller to speak?
t
Thanks for reaching out, Alex. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To have an agent speak immediately when a call starts, without waiting for the caller to speak, you can use one of the following methods: • Using `session.say()`: This method allows the agent to speak a predefined message immediately. For example:
await agent.say("Hi there, how are you doing today?", allow_interruptions=True)
• Using `session.generate_reply()`: This method enables the agent to generate and speak a response based on specific instructions. For example:
await session.generate_reply(instructions="say hello to the user")
Implementation Examples: 1. *In the Agent Class (
on_enter
method):*`class MyAgent(Agent): async def on_enter(self): self.session.generate_reply(instructions="say hello to the user")` 2. *After the Agent Starts:*`agent.start(ctx.room) await agent.say("Hi there, how are you doing today?", allow_interruptions=True)` 3. For Telephony (Inbound Calls): If it’s an inbound call, you can ensure the agent speaks first:
Copy code
if phone_number is None:
    await session.generate_reply(
        instructions="Greet the user and offer your assistance."
    )
Key Notes: • By default, the agent waits for user input before responding. • To initiate a conversation, use the above methods to greet users at session start. • If using
session.say()
, ensure a TTS plugin is added to your session. Alternatively, use
session.generate_reply()
for a more flexible approach. These methods will allow the agent to proactively greet users as soon as the call begins. Sources: Agent speech and audio | LiveKit Docs | examples/voice_agents/preemptive_generation.py | Agents telephony integration | LiveKit Docs
w
How are the instructions provided to generate_reply used? Are they added alongside the instructions provided in the agent constructor?
t
Hey there! Let me break down how instructions work with session.generate_reply() in a friendly, easy-to-understand way. • For Regular LLM Models: Think of this like adding a little extra flavor to the agent's base instructions. The instructions from
generate_reply()
get added to the original instructions, kind of like mixing two ingredients together. Here's how it looks behind the scenes:
Copy code
if instructions:
    instructions = "\n".join([self._agent.instructions, instructions])
• For RealtimeModel: In this case, the new instructions go straight to the reply task without mixing with the original instructions. Some Cool Things to Know: • When you use
generate_reply()
, the instructions help create a response that gets added to the chat history. But don't worry - the instructions themselves don't stick around in the history. • This means the agent keeps its core personality while still being super flexible for different conversations. Wanna see a quick example? Check this out:
Copy code
await self.session.generate_reply(
    instructions=f"Greet {userdata.user_name} and tell them a joke about being {userdata.age} years old."
)
Pretty neat, right? This approach lets the agent be both consistent and adaptable - just like a good friend! 😊 Sources: Workflows | LiveKit Docs | examples/voice_agents/mcp/mcp-agent.py | Agent speech | LiveKit Docs