-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathelevenlabs_change_language.py
More file actions
134 lines (104 loc) · 4.29 KB
/
Copy pathelevenlabs_change_language.py
File metadata and controls
134 lines (104 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""
---
title: ElevenLabs Change Language
category: pipeline-tts
tags: [pipeline-tts, elevenlabs, deepgram, openai]
difficulty: intermediate
description: Shows how to use the ElevenLabs TTS model to change the language of the agent.
demonstrates:
- Using the update_options() method to change the language of STT and TTS
- Allowing agents to self-update their own options using function tools
- Accessing session STT/TTS from within an agent
---
"""
import logging
from dotenv import load_dotenv
from livekit.agents import JobContext, JobProcess, Agent, AgentSession, AgentServer, cli, inference, function_tool
from livekit.plugins import deepgram, elevenlabs, silero
load_dotenv()
logger = logging.getLogger("language-switcher")
logger.setLevel(logging.INFO)
class LanguageSwitcherAgent(Agent):
def __init__(self) -> None:
super().__init__(
instructions="""
You are a helpful assistant communicating through voice.
You can switch to a different language if asked.
Don't use any unpronouncable characters.
"""
)
self.current_language = "en"
self.language_names = {
"en": "English",
"es": "Spanish",
"fr": "French",
"de": "German",
"it": "Italian"
}
self.deepgram_language_codes = {
"en": "en",
"es": "es",
"fr": "fr-CA",
"de": "de",
"it": "it"
}
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 on_enter(self):
await self.session.say("Hi there! I can speak in multiple languages including Spanish, French, German, and Italian. Just ask me to switch to any of these languages. How can I help you today?")
async def _switch_language(self, language_code: str) -> None:
"""Helper method to switch the language"""
if language_code == self.current_language:
await self.session.say(f"I'm already speaking in {self.language_names[language_code]}.")
return
# Access TTS and STT from the session
if self.session.tts is not None:
self.session.tts.update_options(language=language_code)
if self.session.stt is not None:
deepgram_language = self.deepgram_language_codes.get(language_code, language_code)
self.session.stt.update_options(language=deepgram_language)
self.current_language = language_code
await self.session.say(self.greetings[language_code])
@function_tool
async def switch_to_english(self):
"""Switch to speaking English"""
await self._switch_language("en")
@function_tool
async def switch_to_spanish(self):
"""Switch to speaking Spanish"""
await self._switch_language("es")
@function_tool
async def switch_to_french(self):
"""Switch to speaking French"""
await self._switch_language("fr")
@function_tool
async def switch_to_german(self):
"""Switch to speaking German"""
await self._switch_language("de")
@function_tool
async def switch_to_italian(self):
"""Switch to speaking Italian"""
await self._switch_language("it")
server = AgentServer()
def prewarm(proc: JobProcess):
proc.userdata["vad"] = silero.VAD.load()
server.setup_fnc = prewarm
@server.rtc_session()
async def entrypoint(ctx: JobContext):
ctx.log_context_fields = {"room": ctx.room.name}
session = AgentSession(
stt=deepgram.STT(model="nova-2-general", language="en"),
llm=inference.LLM(model="openai/gpt-4o"),
tts=elevenlabs.TTS(model="eleven_turbo_v2_5", language="en"),
vad=ctx.proc.userdata["vad"],
preemptive_generation=True,
)
await session.start(agent=LanguageSwitcherAgent(), room=ctx.room)
await ctx.connect()
if __name__ == "__main__":
cli.run_app(server)