how do you make the agent hang up when the user sa...
# ask-ai
f
how do you make the agent hang up when the user says goodbye
t
Thanks for reaching out, Kees. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To make the agent hang up when the user says goodbye, you can implement a function tool that handles this action. Here is an example of how it can be done:
Copy code
@function_tool
async def end_call(self, context: RunContext):
    """When the user says goodbye, call this function to end the conversation."""
    # interrupt any existing generation
    self.session.interrupt()

    # generate a goodbye message and hang up
    # awaiting it will ensure the message is played out before returning
    await self.session.generate_reply(
        instructions="say a brief goodbye to the user", 
        allow_interruptions=False
    )

    # Delete the room to end the call
    job_ctx = get_job_context()
    await job_ctx.api.room.delete_room(api.DeleteRoomRequest(room=job_ctx.room.name))
This function interrupts any ongoing speech, generates a final goodbye message, and deletes the room to end the call. You can configure the agent to call this function when it detects the user saying goodbye. Sources: Tree View: | dse-workbench
f
what are the imports required?
t
To implement the
end_call
function in LiveKit, you will need the following imports:
Copy code
from livekit import api, rtc
from livekit.agents import get_job_context
These imports allow you to access the LiveKit API for ending a call by deleting the room and obtaining the current job context. Sources: Agents telephony integration | LiveKit Docs
f
what about @function_tool and RunContext?
t
To use
@function_tool
and
RunContext
in LiveKit, you need to import them as follows:
Copy code
from livekit.agents import function_tool, RunContext
The
@function_tool
decorator is used to define tools callable by the LLM, while
RunContext
provides access to the current session, function call, speech handle, and user data within your tool implementation. Sources: Tool definition and use | LiveKit Docs