how do I do a find and replace for words from the ...
# ask-ai
w
how do I do a find and replace for words from the llm before they are sent to the tts? is there a way to have the tts not be called if the llm message is a certain special string (ex. [null])
t
Thanks for reaching out, Drew. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey, could you help me understand how you’re currently putting together the LLM and TTS in your setup? Like, are you using a particular API or SDK? And do you get to manage the message processing before it goes to the TTS? Just want to make sure I’m on the same page!
w
Is there a way in livekit agents to apply modifications to the llm output before it is sent to the tts node?
t
Absolutely! LiveKit agents let you tweak the LLM output before it gets sent to the TTS node. How you do this depends on which version of the LiveKit Agents framework you're using: • Version 0.x: You can use the
before_tts_cb
parameter in the
VoicePipelineAgent
. Think of it as a handy callback function that lets you adjust the LLM output before it goes to the TTS engine. • Version 1.x: Here, you’d override the
tts_node
or
llm_node
in the pipeline, which gives you even more flexibility to modify the text before synthesis. For instance, you might want to swap out special tokens like
<tool_call>
or casual phrases like "lol" with something that sounds better when spoken by TTS. If you'd like, I’m happy to share more details or examples to help you get started! Sources: chain-of-thought-tts/README.mdoc | Agents 0.x migration guide | LiveKit Docs | Pipeline nodes and hooks | LiveKit Docs | Modifying LLM output before TTS | LiveKit Docs | pipeline-llm/replacing_llm_output.py
w
if the llm outputs [null], can I avoid calling the tts node at all?
t
I totally get where you're coming from! While the TTS node in LiveKit can’t be directly "skipped" when the LLM output is something like
[null]
, there’s a neat workaround. You can actually catch and tweak the LLM output before it gets to the TTS node. By customizing the
llm_node
in your agent, you can spot specific strings (like
[null]
) and stop them from moving forward. Here’s a couple of ideas you might find helpful: • Swap out
[null]
for an empty string or some other text that fits better. • Simply yield nothing or skip that part of the stream, so the TTS stays quiet when you want it to. This way, you have control over when speech synthesis happens based on what the LLM outputs. If you want, I’m happy to share more details or some examples to get you going! Sources: pipeline-llm/replacing_llm_output.py | Modifying LLM output before TTS | LiveKit Docs | Voice pipeline nodes | LiveKit Docs | livekit-agents/livekit/agents/voice/io.py