Custom TTS
Implement a custom provider when none of the built-in TTS engines meet your requirements. This guide covers the component class, streaming modes, and credentials configuration. You can implement your own custom TTS component as a Python class to integrate with any third-party text-to-speech service. A custom TTS component must subclass theTTSEngine class from
rasa.core.channels.voice_stream.tts.tts_engine.
Your custom TTS component must output audio in the RasaAudioBytes format
and convert it using the engine_bytes_to_rasa_audio_bytes method.
Component methods
Implementname(), from_config_dict(config, format, rasa_language, additional_languages), and get_default_config(rasa_language) for your component. For streaming input, also implement send_text_chunk(), signal_text_done(), and stream_audio(). The base class provides connect(), close_connection(), synthesize(), and audio wrapping; override them when your provider needs custom connection, synthesis, cleanup, or audio conversion behavior.
Optional Methods
You may also override these methods as needed:connect(config: Optional[T]): Establish connection to the TTS engine if necessary. Default implementation does nothing.close_connection(): Custom cleanup when closing connections (e.g., closing websockets or HTTP sessions). Default implementation does nothing.send_text_chunk(text: str): Send text chunks to the TTS system for streaming synthesis. Used withstream_audio()for real-time streaming.signal_text_done(): Signal TTS engine to process any remaining buffered text and prepare to end the stream.stream_audio(): Stream audio output from the TTS engine. Continuously yields audio chunks as they are produced by the engine.
Streaming vs Non-Streaming TTS
The TTSEngine supports both streaming and non-streaming modes:- Non-streaming: Implement only
synthesize()method for simple request-response synthesis - Streaming: Set
streaming_input = Trueand implementsend_text_chunk(),signal_text_done(), andstream_audio()methods for real-time streaming synthesis
Required Class Attributes
You can optionally define these class attributes:required_env_vars: Tuple of required environment variable namesrequired_packages: Tuple of required Python package namesstreaming_input: Boolean indicating if the engine supports streaming input (defaults toFalse)
Example Implementation
Here’s an example of an implementation for TTS streaming:custom_tts.py
Configure a custom TTS component
To use a custom TTS component, configure its module path and settings in yourcredentials.yml file. The module path follows the format path.to.module.ClassName. For example, a class MyTTS in addons/custom_tts.py has module path addons.custom_tts.MyTTS.
Custom TTS Configuration Example
credentials.yml
MyASRConfig or MyTTSConfig) can be passed through the credentials file and will be available in your component via self.config.
Set the MY_TTS_API_KEY environment variable before starting Rasa. The example reads this variable when it opens the provider connection.