miniature-nail-35141
08/02/2025, 1:43 AMtall-airline-16756
08/02/2025, 1:43 AMtall-airline-16756
08/02/2025, 1:44 AMclass LanguageSwitcherAgent(Agent):
def __init__(self):
super().__init__(
instructions="You are a helpful assistant communicating through voice. You can switch to a different language if asked.",
stt=deepgram.STT(model="nova-2-general", language="en"),
llm=openai.LLM(model="gpt-4o"),
tts=elevenlabs.TTS(model="eleven_turbo_v2_5", language="en"),
vad=silero.VAD.load()
)
self.current_language = "en"
self.greetings = {
"en": "Hello! I'm now speaking in English. How can I help you today?",
"es": "¡Hola! Ahora estoy hablando en español. ¿Cómo puedo ayudarte hoy?",
"fr": "Bonjour! Je parle maintenant en français. Comment puis-je vous aider aujourd'hui?",
"de": "Hallo! Ich spreche jetzt Deutsch. Wie kann ich Ihnen heute helfen?",
"it": "Ciao! Ora sto parlando in italiano. Come posso aiutarti oggi?"
}
async def _switch_language(self, language_code: str):
if language_code == self.current_language:
await self.session.say(f"I'm already speaking in {self.language_names[language_code]}.")
return
if self.tts is not None:
self.tts.update_options(language=language_code)
if self.stt is not None:
deepgram_language = self.deepgram_language_codes.get(language_code, language_code)
self.stt.update_options(language=deepgram_language)
self.current_language = language_code
await self.session.say(self.greetings[language_code])
This approach ensures that your agent provides clear, localized instructions and responses when switching languages.miniature-nail-35141
08/02/2025, 1:48 AMtall-airline-16756
08/02/2025, 1:49 AM_switch_language method to include an update to the instructions using the update_instructions() method. This way, the agent’s behavior stays perfectly in sync with the language you’ve chosen.
Here’s a simple example to illustrate:
async def _switch_language(self, language_code: str):
"""Helper method to switch the language and update instructions"""
if language_code == self.current_language:
await self.session.say(f"I'm already speaking in {self.language_names[language_code]}.")
return
if self.tts is not None:
self.tts.update_options(language=language_code)
if self.stt is not None:
deepgram_language = self.deepgram_language_codes.get(language_code, language_code)
self.stt.update_options(language=deepgram_language)
# Update agent instructions for the new language
new_instructions = f"""
You are a helpful assistant communicating through voice in {self.language_names[language_code]}.
You can switch to a different language if asked.
Don't use any unpronounceable characters.
"""
await self.update_instructions(new_instructions)
self.current_language = language_code
await self.session.say(self.greetings[language_code])
Here are a few key things to keep in mind:
• Dynamic Instruction Updates: Use the update_instructions() method to set fresh instructions that match the language you’ve picked.
• Language-specific Instructions: Tailor the instructions for each language so they’re clear and relevant.
• STT and TTS Configuration: Make sure the Speech-to-Text and Text-to-Speech parts are updated to handle the new language smoothly.
This approach helps keep your agent’s instructions and behavior perfectly aligned with the language you want. If you have any questions or want me to walk you through it further, just let me know—I’m here to help!
Sources: basics/change_agent_instructions.py | livekit-agents/livekit/agents/voice/generation.py