I want my agent to be able to switch languages by ...
# ask-ai
c
I want my agent to be able to switch languages by detecting the one the user is talking in. I want to start with only English and Hindi transfers
t
Thanks for reaching out, Subham. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To make your agent smart enough to detect and switch languages on the fly, you can use speech-to-text (STT) plugins that automatically recognize languages and switch between them seamlessly. Here’s a friendly guide to get you started with English and Hindi: 1. Enable Automatic Language Detection: Try out STT plugins like Gladia, OpenAI, or Deepgram. For instance, Gladia can automatically detect languages when you set the
languages
parameter to
None
, and you can turn on real-time switching with
code_switching=True
. 2. Implement Language Switching: Create an agent that can update STT and text-to-speech (TTS) settings on the go. Just use the
update_options()
method to switch between English and Hindi whenever you need. 3. Optional Multilingual Turn Detection: To make your agent even more responsive, consider using LiveKit’s multilingual turn detector, which works great with both Hindi and English. If you’d like, I can share some detailed code examples or walk you through setting up a specific plugin. Just let me know! Sources: Gladia integration guide | LiveKit Docs | livekit-plugins/livekit-plugins-deepgram/livekit/plugins/deepgram/stt.py | pipeline-tts/elevenlabs_change_language.py | LiveKit turn detector plugin | LiveKit Docs | LiveKit Docs
c
I am using deepgram stt, and elevenlabs tts, will 11labs also change the language of generated voice?
t
Absolutely! ElevenLabs TTS lets you switch languages on the fly for the voice it generates. You just need to use the
update_options(language=language_code)
method to change the language while it’s running. This works great with multilingual models like
eleven_turbo_v2_5
or
eleven_multilingual_v2
. So, if you want to toggle between English and Hindi, for example, you can easily update the language parameter during your session. If you'd like, I can share a code example or help you set it up—just let me know! Sources: pipeline-tts/elevenlabs_tts.py | livekit-plugins/livekit-plugins-elevenlabs/livekit/plugins/elevenlabs/__ini... | ElevenLabs TTS integration guide | LiveKit Docs
c
class Assistant(Agent): def __init__(self) -> None: _# name = "my-first-agent"_ super().__init__( instructions="""You are a helpful voice AI assistant. You eagerly assist users with their questions by providing information from your extensive knowledge. Your responses are concise, to the point, and without any complex formatting or punctuation including emojis, asterisks, or other symbols. You are curious, friendly, and have a sense of humor.""", stt=deepgram.STT(), # llm=openai.LLM(model="gpt-4o-mini"), llm=google.LLM(model="gemini-1.5-flash"), tts=elevenlabs.TTS(_voice_id_="H8bdWZHK2OgZwTN7ponr"), _turn_detection_=MultilingualModel(), ) explain where should I edit?
t
To make your assistant switch languages on the fly, you’ll want to tweak the
tts
setup so it can update the language while running. Here’s a simple way to do that: 1. First, set a default language when you initialize
tts
, like English (
language="en"
). 2. Then, add a method in your assistant class that lets you change the language dynamically using
update_options()
. Something like this:
Copy code
async def switch_language(self, language_code: str):
    if self.tts is not None:
        self.tts.update_options(language=language_code)
Just call this method whenever you want to switch languages during a session. And hey, if anything’s unclear or you want more examples, I’m here to help!
c
how does my stt return the language code/.
t
Hey there! Just a quick heads-up: Deepgram STT can give you the detected language code when you're using the pre-recorded (non-streaming) transcription mode with
detect_language=True
turned on. You’ll find the detected language in the transcription response under
channel.detected_language
, which then shows up in the
language
field inside the
SpeechEvent
. Now, if you’re working in streaming mode, automatic language detection isn’t available. So, you’ll need to set the
language
parameter yourself when you start or update it on the fly using
updateOptions
. If you want, I’m happy to share more details or examples tailored to what you’re working on—just let me know! Sources: livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/stt.py | LiveKit turn detector plugin | LiveKit Docs
c
can I fetch language code from elevenlabs stt upon it's detection? or should I implement it as a tool call?
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!
c
can elevenlabs stt plugin detect language and transcribe for it?