big-controller-60015
03/27/2025, 9:18 AMdef 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
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 callbackstall-airline-16756
03/27/2025, 9:18 AMtall-airline-16756
03/27/2025, 9:18 AMbefore_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