How can I have my agent hangup the call when I say...
# ask-ai
b
How can I have my agent hangup the call when I say I am done talking?
g
To have your agent hang up the call when you indicate you're done talking, you can implement a function call that detects the intent to end the session and then executes the necessary steps to disconnect the call. According to LiveKit documentation, you can define a function that listens for specific phrases indicating the end of a conversation, such as "goodbye" or "end the session". Once detected, you can use the LiveKit API to remove the participant or delete the room, which will effectively hang up the call. Here's a basic example in Python using the LiveKit API:
Copy code
async def hang_up():
    await lk_api.room.remove_participant(
        api.RoomParticipantIdentity(
            room=ctx.room.name, identity=participant.identity
        )
    )

@agent.on("agent_speech_committed")
def on_speech_committed(msg: llm.ChatMessage):
    if "bye" in msg.content.lower():
        asyncio.create_task(hang_up())
Would you like more details on implementing this in a specific programming language or environment? Sources: - LiveKit Slack Discussion 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
b
@millions-hair-24956 What is that "lk_api" in the code example?
g
It could be possible that "lk_api" in the code example refers to an instance of the
LiveKitAPI
class from the LiveKit Python SDK. This class is used to interact with LiveKit's server APIs, allowing you to manage rooms, participants, and other functionalities. According to the LiveKit API documentation, you would typically initialize it with your server URL, API key, and API secret. Here's a brief example of how you might initialize `lk_api`:
Copy code
from livekit.api.livekit_api import LiveKitAPI

lk_api = LiveKitAPI(
    url="your_livekit_server_url",
    api_key="your_api_key",
    api_secret="your_api_secret"
)
Would you like more detailed information on setting up and using
LiveKitAPI
? Sources: - LiveKit API documentation 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
b
@millions-hair-24956 Where does "api.RoomParticipantIdentity" come from?
g
The
api.RoomParticipantIdentity
is likely a reference to a class or function within the LiveKit API that is used to specify the identity of a participant in a room. According to the LiveKit documentation, this is a common pattern for identifying participants when performing actions such as removing them from a room. If you need more detailed information on how to use
RoomParticipantIdentity
or its equivalent in your specific SDK or programming language, please let me know. Sources: - LiveKit documentation 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
b
@millions-hair-24956 What import do I need in order for RoomParticipantIdentity to be available?
g
I'm not able to find any data sources to answer your question. You might want to specify the programming language or SDK you are using. If this doesn't help, please reach out to the Livekit support team for further assistance. 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
b
@millions-hair-24956 I'm using python
g
Though I'm not entirely certain, it might be that
RoomParticipantIdentity
is part of the
livekit.protocol.room
module in the LiveKit Python SDK. You would typically import it like this:
Copy code
from livekit.protocol.room import RoomParticipantIdentity
This import should make
RoomParticipantIdentity
available for use in your Python code. Would you like more detailed guidance on using this in your application? Sources: - https://docs.livekit.io/python/livekit/protocol/room.html - https://docs.livekit.io/python/livekit/protocol/room.html#sidebar Note: I wasn’t able to find highly relevant data sources, but above are a few potentially relevant links. 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
b
@millions-hair-24956 Ok, now I'd like to wait for the agent to finish talking before hanging up. How can I do that?
g
It seems likely that you can wait for the agent to finish talking before hanging up by listening for the
agent_speech_committed
event. This event indicates that the agent has completed its speech. You can then proceed to hang up the call. Here's a basic example in Python:
Copy code
async def hang_up():
    await lk_api.room.remove_participant(
        api.RoomParticipantIdentity(
            room=ctx.room.name, identity=participant.identity
        )
    )

@agent.on("agent_speech_committed")
def on_speech_committed(msg: llm.ChatMessage):
    if "bye" in msg.content.lower():
        asyncio.create_task(hang_up())
This code listens for the
agent_speech_committed
event and checks if the message content includes "bye". If it does, it triggers the
hang_up
function. Would you like more detailed guidance on implementing this? Sources: - https://livekit-users.slack.com/archives/C07FY8WHGPM/p1731045723.892919 - https://livekit-users.slack.com/archives/C07FRP66XGE/p1733329965.344829 Note: I wasn’t able to find highly relevant data sources, but above are a few potentially relevant links. 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
b
@millions-hair-24956 That's what I'm doing but it is still hanging up before the speech is sent.
g
It seems likely that the issue might be related to the timing of the function call or the way the speech completion is being detected. To ensure that the agent finishes speaking before hanging up, you might want to explicitly wait for the speech to complete using an asynchronous approach. Here's a refined example:
Copy code
async def hang_up():
    await lk_api.room.remove_participant(
        api.RoomParticipantIdentity(
            room=ctx.room.name, identity=participant.identity
        )
    )

@agent.on("agent_speech_committed")
async def on_speech_committed(msg: llm.ChatMessage):
    if "bye" in msg.content.lower():
        # Wait for the agent to finish speaking
        await asyncio.sleep(1)  # Adjust the sleep time as needed
        await hang_up()
This example uses
asyncio.sleep
to introduce a delay, ensuring the agent's speech is fully sent before executing the hang-up function. You may need to adjust the sleep duration based on your specific use case. Would you like more detailed guidance on this approach? Sources: - https://docs.livekit.io/agents/voice-agent/voice-pipeline/#Interruption-handling - https://livekit-users.slack.com/archives/C07FRP66XGE/p1733329965.344829 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
b
@millions-hair-24956 Ok, I tried that and got this error. Cannot register an async callback with
.on()
. Use
asyncio.create_task
within your synchronous callback instead.
g
It seems likely that the error is due to attempting to register an asynchronous function directly with the
.on()
method, which expects a synchronous callback. To resolve this, you can use
asyncio.create_task
within a synchronous callback to handle asynchronous operations. Here's how you can modify your code:
Copy code
async def hang_up():
    await lk_api.room.remove_participant(
        api.RoomParticipantIdentity(
            room=ctx.room.name, identity=participant.identity
        )
    )

@agent.on("agent_speech_committed")
def on_speech_committed(msg: llm.ChatMessage):
    if "bye" in msg.content.lower():
        # Use asyncio.create_task to handle the async hang_up function
        asyncio.create_task(async_hang_up())

async def async_hang_up():
    # Wait for the agent to finish speaking
    await asyncio.sleep(1)  # Adjust the sleep time as needed
    await hang_up()
This approach uses a synchronous callback for the event listener and creates an asynchronous task to handle the hang-up process. Would you like more detailed guidance on this approach? Sources: - https://livekit-users.slack.com/archives/C07FY8WHGPM/p1731966007.671569 - https://livekit-users.slack.com/archives/C07FY8WHGPM/p1731612787.009399 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
b
@millions-hair-24956 Ok, that seems to work, thanks
g
I'm glad to hear that it worked! If you have any more questions or need further assistance with LiveKit or anything else, feel free to ask. 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.