I want to run a sentiment analysis on each sentenc...
# ask-ai
n
I want to run a sentiment analysis on each sentence as it's being streamed from the agent response. What hooks can I listen to as the transcript is being generated?
t
Thanks for reaching out, rohit. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To help you run sentiment analysis on each sentence as it's being streamed, here are some helpful hooks and methods you can use: • Frontend (Client-Side): You can use the
RoomEvent.TranscriptionReceived
event to get transcription segments in real time. Just a heads-up, this method is deprecated and will soon be replaced by text streams on the
lk.transcription
topic. Another great option is to use
registerTextStreamHandler
to listen to text streams and handle interim or final transcriptions smoothly. • Agent-Side (Python): You might want to implement the
RecognitionHooks
protocol to catch transcription events. The
on_interim_transcript
method is perfect for interim results, while
on_final_transcript
works well for final results. • React Applications: For real-time transcription, the
useTranscriptions
hook is a handy tool to access live transcriptions effortlessly. If you'd like, I’m here to provide detailed examples or walk you through any of these methods. Just let me know how I can support you! Sources: hooks/use-transcriber.ts | Text and transcriptions | LiveKit Docs | Text and transcriptions | LiveKit Docs | Docs::Agents | Session recording and transcripts | LiveKit Docs
n
how does on_interim_transcript work
t
The
on_interim_transcript
function is designed to handle partial transcription results during real-time speech-to-text (STT) processing. Let me walk you through how it works: • It kicks in whenever an
INTERIM_TRANSCRIPT
event comes in from the STT engine, like AssemblyAI, Google, or Deepgram. • If you're using external transcription (such as through a Realtime LLM), it smartly skips sending out the STT-based transcription to prevent any duplicates. • When it’s active, it sends out a
UserInputTranscribed
event with the transcript and
isFinal: false
, letting you know the transcription is still in progress and might be updated shortly. • It also updates the internal state (like
audioInterimTranscript
) to help with real-time UI features, such as live captions or detecting interruptions. • The interim transcript is then combined with finalized transcripts, giving you a smooth, continuously updated view of what the user is saying. This setup really helps provide timely updates while keeping accuracy through a two-step transcription process (interim → final). If you'd like, I’m here to share more details or examples—just let me know! Sources: livekit-plugins/livekit-plugins-cartesia/livekit/plugins/cartesia/stt.py | Speechmatics STT integration guide | LiveKit Docs
n
does elevenlabs have this
t
I'm happy to let you know that ElevenLabs does support interim transcription events in LiveKit through the
on_interim_transcript
callback. The ElevenLabs STT plugin is set up with
interim_results=True
, which allows it to emit
INTERIM_TRANSCRIPT
events as speech is being recognized. To get this working, you can implement the
on_interim_transcript
method within the
RecognitionHooks
interface. This method will receive a
SpeechEvent
object that contains the interim transcription data, accessible via
ev.alternatives[0].text
. Here's a simple example:
Copy code
def on_interim_transcript(self, ev: stt.SpeechEvent, *, speaking: bool | None):
    interim_text = ev.alternatives[0].text
    # Handle real-time transcription updates
If you have any questions or would like some help setting this up, please don’t hesitate to reach out—I’m here to assist! Sources: agents/src/stt/stt.ts | Working with plugins | LiveKit Docs | livekit-agents/livekit/agents/inference/stt.py
n
what about for tts
t
Absolutely! ElevenLabs supports text-to-speech (TTS) in LiveKit, which allows you to create really natural-sounding voice AI applications. It works through a streaming WebSocket connection that converts text into human-like speech in real time. You have two great options to choose from: • Chunked synthesis: This mode synthesizes the full text and returns it as audio all at once. • Streamed synthesis: Here, text is processed bit by bit and sent as audio frames in real time, which is perfect for interactive applications. Getting started is pretty straightforward: 1. First, install the plugin:
pip install "livekit-agents[elevenlabs]~=1.2"
(Python)
pnpm add @livekit/agents-plugin-elevenlabs@1.x
(Node.js) 2. Next, set your ElevenLabs API key as the environment variable
ELEVEN_API_KEY
. 3. Then, initialize TTS with your preferred settings, like
voice_id
,
model
, and
language
. If you’d like, I’m here to help you with the implementation or provide any more details you might need—just let me know! Sources: Docs::Agents | agents/src/inference/tts.ts | livekit-plugins/livekit-plugins-elevenlabs/README.md | Example Applications | livekit/livekit_composite | DeepWiki
n
yeah but does elevenlabs tts have a realtime transcript hoook I can use
t
I'm glad you're exploring ElevenLabs TTS in LiveKit! It supports real-time transcript forwarding that's perfectly synchronized with the generated speech, though it doesn't use a traditional webhook. To get this going, you can simply enable TTS-aligned transcription by setting
use_tts_aligned_transcript=True
in your
AgentSession
configuration. If you're working with Python, you have a neat way to access word-level timing data by implementing the
transcription_node
method. This will give you
TimedString
objects with
start_time
and
end_time
for each word. Just a heads-up, this feature is still experimental, so it might evolve in future updates. For those using Node.js, synchronized transcript forwarding is available, but direct access to word-level timing data isn’t supported just yet. If you'd like, I’m here to help you set this up or clarify anything further—just let me know! Sources: ElevenLabs TTS integration guide | LiveKit Docs | Docs::Agents | Docs::Agents | examples/other/text-to-speech/elevenlabs_tts.py | ElevenLabs TTS integration guide | LiveKit Docs
n
can the agent see this data?
t
I'm happy to help you with this! LiveKit agents can indeed access real-time TTS-aligned transcript data from ElevenLabs when you're using the Python SDK. To get this going, you'll want to set up your
AgentSession
with
use_tts_aligned_transcript=True
. This setting makes sure you receive word-level timing information that's perfectly synced with the speech playback. In your agent code, you can handle this data by implementing the
transcription_node
method. Here's a simple example to guide you:
Copy code
class MyAgent(Agent):
    async def transcription_node(
        self, text: AsyncIterable[str | TimedString], model_settings: ModelSettings
    ) -> AsyncGenerator[str | TimedString, None]:
        async for chunk in text:
            if isinstance(chunk, TimedString):
                logger.info(f"TimedString: '{chunk}' ({chunk.start_time} - {chunk.end_time})")
            yield chunk
Just a quick note: this feature is still experimental and is currently supported only in Python. If you run into any questions or need a hand with anything else, please don’t hesitate to reach out—I’m here to help! Sources: Text and transcriptions | LiveKit Docs | Docs::Agents | Text and transcriptions | LiveKit Docs | livekit-agents/livekit/agents/inference/tts.py | ElevenLabs TTS integration guide | LiveKit Docs