how can I prevent LLM response being sent as TTS t...
# ask-ai
a
how can I prevent LLM response being sent as TTS to user, and intercept LLM response and send custom TTS response to user?
t
Thanks for reaching out, Hiren. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! You can totally intercept LLM responses and send custom TTS responses to users in LiveKit by using these handy methods: • Using
before_tts_cb
Callback:
This is the easiest way! Just add a callback to tweak the text before it goes to the TTS engine—perfect for simple text changes. • Overriding the
llm_node
Method:
If you want more control, create your own agent class and override this method to catch and modify the LLM output before it hits TTS. • Overriding the
tts_node
Method:
For even more advanced control, you can intercept text right at the TTS level to shape how the speech sounds. • Using Structured Output: This lets you separate TTS instructions from the actual response, giving you cool options to adjust tone, emotion, and intonation. If you'd like, I’m happy to walk you through the detailed steps for any of these—just let me know! Sources: livekit-agents/livekit/agents/voice/generation.py | Pipeline nodes and hooks | LiveKit Docs | chain-of-thought-tts/README.mdoc | pipeline-tts/tts_node.py
a
When there are 2 users in the same room, can I publish this TTS audio for any specific user?
t
If you want to publish TTS audio for a specific user in a LiveKit room, here are a few ways you can do it: • Targeting Specific Participants: You can set up the agent to handle audio from and respond to a particular participant by using the
participant
parameter in the RoomIO constructor. • Publishing Audio "On Behalf Of" a Participant: Try using the
ATTRIBUTE_PUBLISH_ON_BEHALF
attribute to show that the audio is being published for a specific participant. • Subscription Control: Although TTS audio goes out to the whole room, you can still configure the agent to respond only to certain participants. If you'd like, I can walk you through the detailed steps for any of these options—just let me know! Sources: plugins/neuphonic/src/tts.ts | Voice Assistants | livekit/livekit_composite | DeepWiki | Text-to-speech (TTS) integrations | LiveKit Docs | examples/other/text-to-speech/cartesia_tts.py | How to set a fixed participant for agent audio processing
a
Yes please give me step by step details like how can I target specific participant to publish TTS output
t
Here’s a friendly step-by-step guide to help you target a specific participant and publish TTS output for them in a LiveKit room: 1. Connect to the Room:
await ctx.connect(auto_subscribe=AutoSubscribe.SUBSCRIBE_NONE)
This makes sure you connect to the room without automatically subscribing to all tracks, giving you more control. 2. Set Up TTS: Start by initializing the TTS engine with the model and voice you want:
tts = openai.TTS(model="tts-1", voice="nova")
3. Create an Audio Source and Track: Next, set up an audio source and create a local audio track like this:
Copy code
source = rtc.AudioSource(tts.sample_rate, tts.num_channels)
track = rtc.LocalAudioTrack.create_audio_track("agent-mic", source)
4. Configure Publish Options: Set the track source to
SOURCE_MICROPHONE
so it’s clear where the audio is coming from:
Copy code
options = rtc.TrackPublishOptions()
options.source = rtc.TrackSource.SOURCE_MICROPHONE
5. Set Target Participant: Here’s a key step — use the
ATTRIBUTE_PUBLISH_ON_BEHALF
attribute to specify exactly who should receive the audio:
Copy code
ctx.room.local_participant.set_attributes({
    ATTRIBUTE_PUBLISH_ON_BEHALF: "target-user-identity"
})
6. Publish the Track: Now, publish the audio track to the room:
Copy code
publication = await ctx.room.local_participant.publish_track(track, options)
await publication.wait_for_subscription()
7. Synthesize and Publish Audio: Finally, generate the TTS audio and send it to the audio source like this:
Copy code
text = "Hello, this message is for you!"
async for output in tts.synthesize(text):
    await source.capture_frame(output.frame)
If anything’s unclear or you’d like a hand with implementation, just let me know — I’m here to help!
a
So, here's the thing, when user1 connects to the VoicePipelineAgent over a specific livekit URL, his audio track will be used to capture his query.. that query will be processed in LLM but response won't be sent to him but the User2 who's there connected in the room, that user will submit the response for User1, which will first go to LLM and that response will be generated as TTS and will be sent to User 1. Give me a code example meeting this scenario. User2 should get all the transcription as well as LLM's response as chat messages which he'll submit his response from Chat, which will be processed by LLM and that response will be sent to User1
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!