Skip to main content
This notebook demonstrates how to build a low-latency voice assistant using Mercury 2 for real-time intelligence combined with ElevenLabs’ speech-to-text and text-to-speech models. We walk you through how to:
  1. Convert text to speech using ElevenLabs TTS
  2. Convert audio to text using ElevenLabs speech-to-text
  3. Generate realtime responses with Mercury 2
  4. Optimize latency using Mercury 2 streaming

Run the Voice Assistant

Directly via Google Colab or download the .ipynb to run it locally in Jupyter

Dependencies

Install packages and import the libraries required for the Mercury 2 SDK, ElevenLabs, and audio playback:

API Keys

Set up your API keys for both ElevenLabs and Inception Labs.
  1. Create a .env in this directory
  2. Add your API keys to the .env:
    • INCEPTION_API_KEY
    • ELEVENLABS_API_KEY
The keys will be automatically loaded from the .env file.

Initialize Clients

Create the ElevenLabs and Inception (Mercury 2) clients:

Create Input Audio

We can simulate a user audio input using ElevenLabs text-to-speech model:

Transcribe Speech into Text

We then transcribe the audio input using ElevenLabs’ speech-to-text model. We’ll also measure the transcription latency:

Get a Response from Mercury 2

We send the transcribed text to Mercury 2 and measure the response time. We’re using mercury-2 for fast reasoning responses:

Real-time with Streaming

Improve response latency by using Mercury 2 streaming. This allows us to receive the first tokens more quickly, reducing user-perceived latency:
We can also stream the text-to-speech response to reduce silence:

Streaming Mercury 2 Directly to TTS

We’ve optimized Mercury 2 streaming and TTS separately. Now we can stream the model response and convert it to audio as soon as we have complete sentences, demarcated by !, ., ?.
In this sentence-by-sentence approach, each chunk is synthesized independently, so the audio can sound less natural than a fully context-aware streaming setup. Check out how you can set up Mercury 2 with an ElevenLabs Agent for streaming.