wooden-librarian-60399
08/26/2025, 4:09 AMtall-airline-16756
08/26/2025, 4:09 AMtall-airline-16756
08/26/2025, 4:09 AMllm_node in a Custom Agent: You can catch and tweak the LLM output stream by swapping exclamation points for periods. This gives you really precise control over the output.
• Use the before_tts_cb Callback: This lets you change the text right before TTS synthesis with a simple callback function—perfect if you want an easy way to make text tweaks.
• Override the `transcription_node`: Here, you can do a final pass on all transcribed text, including fixing up punctuation.
If you'd like, I’m happy to walk you through the steps for any of these methods—just let me know!
Sources: Modifying LLM output before TTS | LiveKit Docs | chain-of-thought-tts/agent.py | Pipeline nodes and hooks | LiveKit Docswooden-librarian-60399
08/26/2025, 4:13 AMtall-airline-16756
08/26/2025, 4:13 AMbefore_tts_cb callback has been replaced by the tts_node method, which gives you more flexibility to tweak the text before it goes through TTS synthesis. Here’s a friendly walkthrough on how you can set it up:
1. Start by creating your own custom agent class that inherits from Agent.
2. Then, override the tts_node method to preprocess the text stream before sending it off to the TTS engine.
3. Finally, use DefaultAgentImplementation.tts_node to handle the usual TTS synthesis once you’ve made your changes.
Here’s a simple example to get you going:
from livekit.agents import Agent, DefaultAgentImplementation
from livekit.agents.pipeline import stt, llm, tts
from typing import AsyncIterable
class CustomVoiceAgent(Agent):
async def tts_node(
self,
text: AsyncIterable[llm.ChatChunk],
tts: tts.TTS,
model_settings: tts.ModelSettings,
) -> AsyncIterable[tts.SpeechEvent]:
# Preprocess the text before TTS
cleaned_text = self._clean_text_for_tts(text)
# Pass the cleaned text to the default TTS node for synthesis
async for event in DefaultAgentImplementation.tts_node(
self, cleaned_text, tts, model_settings
):
yield event
async def _clean_text_for_tts(
self, text_stream: AsyncIterable[llm.ChatChunk]
) -> AsyncIterable[str]:
"""Replace exclamation points with periods."""
async for chunk in text_stream:
if isinstance(chunk, llm.TextChunk):
# Replace exclamation points with periods
cleaned_text = chunk.text.replace("!", ".")
yield cleaned_text
This way, you can easily swap out exclamation points for periods or do any other text tweaks before the TTS does its magic. If you want me to walk you through anything else or give more examples, just let me know—I’m here to help!
Sources: Agents 0.x migration guide | LiveKit Docs | Agents v0.x migration guide | LiveKit Docs | Voice pipeline nodes | LiveKit Docs | Pipeline nodes and hooks | LiveKit Docswooden-librarian-60399
08/26/2025, 4:14 AMtall-airline-16756
08/26/2025, 4:15 AMllm_node in Agents 1.0 to tweak the LLM output, here’s a simple way to do it:
1. Start by making a custom agent class that inherits from Agent.
2. Then, override the llm_node method so you can catch and modify the LLM output stream.
3. Inside, process each chunk of text to swap out exclamation points for periods before sending it along.
Here’s a quick example to get you going:
from livekit.agents import Agent
from livekit.agents.pipeline import llm
from typing import AsyncIterable
class CustomLLMAgent(Agent):
async def llm_node(
self,
chat_ctx: llm.ChatContext,
tools: llm.ToolRegistry,
model_settings: llm.ModelSettings = None
) -> AsyncIterable[llm.ChatChunk]:
async def process_stream():
async with self.llm.chat(chat_ctx=chat_ctx, tools=tools, tool_choice=None) as stream:
async for chunk in stream:
if chunk is None:
continue
content = getattr(chunk.delta, 'content', None) if hasattr(chunk, 'delta') else str(chunk)
if content is None:
yield chunk
continue
# Replace exclamation points with periods
processed_content = content.replace("!", ".")
if processed_content != content:
if hasattr(chunk, 'delta') and hasattr(chunk.delta, 'content'):
chunk.delta.content = processed_content
else:
chunk = processed_content
yield chunk
return process_stream()
This approach gives you neat control over the LLM’s output, letting you adjust the text stream before it moves on to the next steps. If you want to dive deeper or run into any questions, just give me a shout—I’m here to help!