Hello I have built a voice agent pipeline using A...
# ask-ai
d
Hello I have built a voice agent pipeline using AgentSession But I want to use a custom TTS which accepts text and converts to speech. How do I implement that in the pipeline?
t
Thanks for reaching out, Harsha. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Here’s how you can easily add a custom TTS to your AgentSession pipeline: • Using a Supported TTS Plugin: If your TTS provider is already supported (like Cartesia or ElevenLabs), just initialize the AgentSession with that plugin. For example:
Copy code
from livekit.agents import AgentSession
from livekit.plugins import cartesia

session = AgentSession(
    tts=cartesia.TTS(model="sonic-2", voice="f786b574-daa5-4673-aa0c-cbe3e8534c02")
)
• Using OpenAI-Compatible Endpoints: If your custom TTS has an OpenAI-compatible API, you can use the openai.TTS plugin with your own base URL like this:
Copy code
from livekit.plugins import openai

tts = openai.TTS(
    model="custom-model",
    voice="custom-voice",
    base_url="http://localhost:8880/v1",
    response_format="wav"
)

session = AgentSession(tts=tts)
• Overriding the tts_node Method: If you want to get a bit more hands-on, you can override the tts_node method in your custom Agent class to tweak the text before it’s turned into speech. Sources: Text-to-speech (TTS) integrations | LiveKit Docs | Resemble AI TTS integration guide | LiveKit Docs | Hume TTS integration guide | LiveKit Docs | LMNT TTS integration guide | LiveKit Docs | pipeline-tts/rime_tts.py
d
I want to use this code as TTS - from sarvamai import SarvamAI from sarvamai.play import save client = SarvamAI(api_subscription_key="sk_gfe5pjqs_8V4aBdsutdrrb3QmMQqyVJgF") # Convert text to speech audio = client.text_to_speech.convert( target_language_code="kn-IN", #text="Hi there! This is Praveen from Expressbee. Would you like to track your shipment? Please tell me your tracking ID", #text="ಇದು ಕಿಮ್ಮನೆ ಲಗ್ಜುರಿ ರೆಸಾರ್ಟ್‌ನ ಅನಿಶಾ ಮಾತನಾಡುತ್ತಿದ್ದೇನೆ. ನಾನು ಇಂದು ನಿಮಗೆ ಹೇಗೆ ಸಹಾಯ ಮಾಡಬಹುದು?", text="This is anisha from kimmane luxury resort. How can i help you today?", #text="ನಮಸ್ಕಾರ, ಕಿಮ್ಮಣೆ Luxury Resort ಗೆ ಸ್ವಾಗತ. ನಾನು ಅನಿಷಾ ಮಾತನಾಡುತ್ತಿದ್ದೇನೆ, ಇಂದು ನಾನು ನಿಮಗೆ ಹೇಗೆ ಸಹಾಯ ಮಾಡಲಿ?", model="bulbul:v3-beta", enable_preprocessing=True, speaker="ritu", # sakshi, harsh, aditya, chirag, ritu speech_sample_rate=16000, ) save(audio, "output2.wav")
t
Hey there! To get your custom TTS code using SarvamAI working smoothly with the LiveKit AgentSession pipeline, here’s a simple way to do it: 1. Create a Custom TTS Node: You’ll want to build a custom TTS class that wraps your SarvamAI logic. This class will take in text and output audio in a format that LiveKit understands (like PCM or WAV).
Copy code
from livekit.agents.voice import TTS

class SarvamAITTS(TTS):
    def __init__(self, api_key):
        self.client = SarvamAI(api_subscription_key=api_key)

    async def stream(self, text, **kwargs):
        audio = self.client.text_to_speech.convert(
            target_language_code="kn-IN",
            text=text,
            model="bulbul:v3-beta",
            enable_preprocessing=True,
            speaker="ritu",
            speech_sample_rate=16000,
        )
        yield audio  # Just make sure this yields audio chunks in the format LiveKit expects
1. Integrate with AgentSession: Then, pass your custom TTS class right into the AgentSession.
Copy code
from livekit.agents import AgentSession

session = AgentSession(
    tts=SarvamAITTS(api_key="sk_gfe5pjqs_8V4aBdsutdrrb3QmMQqyVJgF")
)
1. Test and Debug: Finally, double-check that the audio output matches what your playback setup needs. You might need to tweak the format or sample rate a bit to get everything working perfectly with LiveKit. If you hit any bumps or want a hand with the implementation or testing, just give me a shout—I’m here to help!
d
I get this error- File "/root/kimmane_test.py", line 22, in <module> from livekit.agents.voice import TTS ImportError: cannot import name 'TTS' from 'livekit.agents.voice' (/root/.venv/lib/python3.13/site-packages/livekit/agents/voice/__init__.py)
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!