in livekit code examples ``` def replace_words(as...
# ask-ai
b
in livekit code examples
Copy code
def replace_words(assistant: VoicePipelineAgent, text: str | AsyncIterable[str]):
    return tokenize.utils.replace_words(
        text=text, replacements={"livekit": r"<<l|aɪ|v|k|ɪ|t|>>"}
    )

agent = VoicePipelineAgent(
    ...
    before_tts_cb=replace_words,
)
this example for before_tts_cb and
Copy code
async def truncate_context(assistant: VoicePipelineAgent, chat_ctx: llm.ChatContext):
    if len(chat_ctx.messages) > 15:
        chat_ctx.messages = chat_ctx.messages[-15:]

agent = VoicePipelineAgent(
    ...
    before_llm_cb=truncate_context,
)
for before llm callback my question why is the function for llm callback async but for tts it is not async, also can we give async and sync both as per our needs to both of these callbacks
t
Thanks for reaching out, Mohammed. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The difference in using async versus sync callbacks in LiveKit's VoicePipelineAgent depends on the nature of the operation being performed. For example: • The
before_tts_cb
callback is synchronous because it processes text directly and does not involve asynchronous operations like waiting for external data or events. • The
before_llm_cb
callback is asynchronous because it may involve operations that require awaiting, such as handling large datasets or interacting with external systems. In general, you can define both synchronous and asynchronous callbacks for these hooks, depending on your specific needs. However, the implementation should align with the expected behavior of the callback. For instance, if the callback involves asynchronous operations (e.g., streaming data or making API calls), it should be defined as
async
. Conversely, if it only involves immediate, non-blocking operations, it can be synchronous. Sources: Modifying LLM output before TTS | LiveKit Docs