<@U084APK40NS> before_tts_cb is called multiple ti...
# ask-ai
r
@millions-hair-24956 before_tts_cb is called multiple times, I want to trigger it only when the tts is completed.
g
It seems likely that the
before_tts_cb
callback is designed to be triggered before each text-to-speech synthesis, rather than after completion. This behavior is consistent with its purpose of allowing modifications to the text before it is synthesized. If you want to perform an action after the TTS is completed, you might need to look for an event or callback that is triggered after the TTS process finishes. Based on the LiveKit documentation, there isn't a direct mention of a callback specifically for post-TTS completion. However, you might consider using the
agent_speech_committed
event, which is emitted when the agent's speech is committed, indicating that the TTS process has likely completed. Would you like more details on how to implement this or explore other potential solutions? Sources: - LiveKit documentation 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
r
@millions-hair-24956 so I can modify code which uses that to use agent_speech_committed instead, show with exmaple
g
My understanding is that you can use the
agent_speech_committed
event to perform actions after the TTS process is completed. Here's a basic example of how you might modify your code to use this event:
Copy code
from livekit.agents import VoicePipelineAgent
from livekit.agents.llm import ChatContext, ChatMessage

# Initialize your agent
agent = VoicePipelineAgent(
    vad=silero.VAD.load(),
    stt=deepgram.STT(),
    llm=openai.LLM(),
    tts=cartesia.TTS(),
    chat_ctx=ChatContext(),
    allow_interruptions=True
)

# Define a callback function for when the agent's speech is committed
@agent.on("agent_speech_committed")
def on_agent_speech_committed(msg: ChatMessage):
    print("TTS completed. Speech committed:", msg.content)
    # Add any additional actions you want to perform after TTS completion here

# Start the agent
agent.start(room, participant)
In this example, the
on_agent_speech_committed
function is registered as a callback for the
agent_speech_committed
event. This function will be called whenever the agent's speech is committed, allowing you to perform any necessary actions after the TTS process is completed. Would you like more detailed guidance on this implementation? Sources: - LiveKit documentation 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.