can I replace all exclamation points with periods ...
# ask-ai
w
can I replace all exclamation points with periods before sending the llm output to tts?
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! If you want to replace all exclamation points with periods in the LLM output before sending it to TTS, here are a few ways you can do it: • Override the
llm_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 Docs
w
walk me through the before_tts_cb method in agents 1.0
t
Hey there! In Agents 1.0, the
before_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:
Copy code
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 Docs
w
show me how to do it with a custom llm_node instead
t
If you want to create a custom
llm_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:
Copy code
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!