1
+ # Copyright 2024 Deepgram SDK contributors. All Rights Reserved.
2
+ # Use of this source code is governed by a MIT license that can be found in the LICENSE file.
3
+ # SPDX-License-Identifier: MIT
4
+
5
+ """
6
+ Example demonstrating Voice Agent with multiple TTS providers for fallback support.
7
+
8
+ This example shows how to configure multiple TTS providers in an array format,
9
+ allowing the agent to automatically fall back to alternative providers if the
10
+ primary provider fails or is unavailable.
11
+ """
12
+
13
+ from deepgram .utils import verboselogs
14
+
15
+ from deepgram import (
16
+ DeepgramClient ,
17
+ DeepgramClientOptions ,
18
+ AgentWebSocketEvents ,
19
+ SettingsOptions ,
20
+ Speak ,
21
+ Endpoint ,
22
+ Header ,
23
+ )
24
+
25
+ global warning_notice
26
+ warning_notice = True
27
+
28
+
29
+ def main ():
30
+ try :
31
+ print ("Starting Voice Agent with Fallback TTS Providers..." )
32
+
33
+ config : DeepgramClientOptions = DeepgramClientOptions (
34
+ options = {
35
+ "keepalive" : "true" ,
36
+ "microphone_record" : "true" ,
37
+ "speaker_playback" : "true" ,
38
+ },
39
+ verbose = verboselogs .SPAM ,
40
+ )
41
+
42
+ deepgram : DeepgramClient = DeepgramClient ("" , config )
43
+ dg_connection = deepgram .agent .websocket .v ("1" )
44
+
45
+ def on_open (self , open , ** kwargs ):
46
+ print (f"\n \n { open } \n \n " )
47
+
48
+ def on_binary_data (self , data , ** kwargs ):
49
+ global warning_notice
50
+ if warning_notice :
51
+ print ("Received binary data from TTS provider" )
52
+ print ("Audio will be played automatically with speaker_playback=true" )
53
+ warning_notice = False
54
+
55
+ def on_welcome (self , welcome , ** kwargs ):
56
+ print (f"\n \n { welcome } \n \n " )
57
+
58
+ def on_settings_applied (self , settings_applied , ** kwargs ):
59
+ print (f"\n \n Settings Applied - Multiple TTS providers configured:\n { settings_applied } \n \n " )
60
+
61
+ def on_conversation_text (self , conversation_text , ** kwargs ):
62
+ print (f"\n \n { conversation_text } \n \n " )
63
+
64
+ def on_user_started_speaking (self , user_started_speaking , ** kwargs ):
65
+ print (f"\n \n { user_started_speaking } \n \n " )
66
+
67
+ def on_agent_thinking (self , agent_thinking , ** kwargs ):
68
+ print (f"\n \n { agent_thinking } \n \n " )
69
+
70
+ def on_agent_started_speaking (self , agent_started_speaking , ** kwargs ):
71
+ print (f"\n \n { agent_started_speaking } \n \n " )
72
+
73
+ def on_agent_audio_done (self , agent_audio_done , ** kwargs ):
74
+ print (f"\n \n { agent_audio_done } \n \n " )
75
+
76
+ def on_close (self , close , ** kwargs ):
77
+ print (f"\n \n { close } \n \n " )
78
+
79
+ def on_error (self , error , ** kwargs ):
80
+ print (f"\n \n Error (may trigger TTS fallback): { error } \n \n " )
81
+
82
+ def on_unhandled (self , unhandled , ** kwargs ):
83
+ print (f"\n \n { unhandled } \n \n " )
84
+
85
+ # Register event handlers
86
+ dg_connection .on (AgentWebSocketEvents .Open , on_open )
87
+ dg_connection .on (AgentWebSocketEvents .AudioData , on_binary_data )
88
+ dg_connection .on (AgentWebSocketEvents .Welcome , on_welcome )
89
+ dg_connection .on (AgentWebSocketEvents .SettingsApplied , on_settings_applied )
90
+ dg_connection .on (AgentWebSocketEvents .ConversationText , on_conversation_text )
91
+ dg_connection .on (AgentWebSocketEvents .UserStartedSpeaking , on_user_started_speaking )
92
+ dg_connection .on (AgentWebSocketEvents .AgentThinking , on_agent_thinking )
93
+ dg_connection .on (AgentWebSocketEvents .AgentStartedSpeaking , on_agent_started_speaking )
94
+ dg_connection .on (AgentWebSocketEvents .AgentAudioDone , on_agent_audio_done )
95
+ dg_connection .on (AgentWebSocketEvents .Close , on_close )
96
+ dg_connection .on (AgentWebSocketEvents .Error , on_error )
97
+ dg_connection .on (AgentWebSocketEvents .Unhandled , on_unhandled )
98
+
99
+ # Configure TTS providers for fallback support
100
+ print ("Configuring TTS providers with fallback..." )
101
+
102
+ # Primary TTS Provider: Deepgram Aura
103
+ primary_tts = Speak ()
104
+ primary_tts .provider .type = "deepgram"
105
+ primary_tts .provider .model = "aura-2-zeus-en"
106
+
107
+ # Fallback TTS Provider: OpenAI TTS
108
+ fallback_tts = Speak ()
109
+ fallback_tts .provider .type = "open_ai"
110
+ fallback_tts .provider .model = "tts-1"
111
+ fallback_tts .provider .voice = "shimmer"
112
+
113
+ # Configure custom endpoint for OpenAI
114
+ fallback_tts .endpoint = Endpoint ()
115
+ fallback_tts .endpoint .url = "https://api.openai.com/v1/audio/speech"
116
+ fallback_tts .endpoint .headers = [
117
+ Header (key = "authorization" , value = "Bearer {{OPENAI_API_KEY}}" )
118
+ ]
119
+
120
+ # Configure agent settings
121
+ options : SettingsOptions = SettingsOptions ()
122
+ options .agent .think .provider .type = "open_ai"
123
+ options .agent .think .provider .model = "gpt-4o-mini"
124
+ options .agent .think .prompt = (
125
+ "You are a helpful AI assistant with fallback TTS providers for reliable speech output. "
126
+ "If one provider fails, another will automatically take over."
127
+ )
128
+ options .agent .listen .provider .type = "deepgram"
129
+ options .agent .listen .provider .model = "nova-3"
130
+ options .agent .listen .provider .keyterms = ["hello" , "goodbye" , "fallback" ]
131
+
132
+ # Configure multiple TTS providers (array format)
133
+ options .agent .speak = [primary_tts , fallback_tts ]
134
+
135
+ options .agent .language = "en"
136
+ options .greeting = (
137
+ "Hello! I'm your AI assistant with fallback TTS providers. "
138
+ "I can automatically switch between Deepgram and OpenAI for reliable voice output."
139
+ )
140
+
141
+ print ("TTS Providers configured:" )
142
+ print (f"1. Primary: { primary_tts .provider .type } - { primary_tts .provider .model } " )
143
+ print (f"2. Fallback: { fallback_tts .provider .type } - { fallback_tts .provider .model } " )
144
+
145
+ # Start the connection
146
+ if dg_connection .start (options ) is False :
147
+ print ("Failed to start connection" )
148
+ return
149
+
150
+ print ("\n \n === Voice Agent with Fallback TTS Started ===" )
151
+ print ("The agent is now running with primary and fallback TTS providers." )
152
+ print ("If the primary provider fails, it will automatically fall back to the secondary." )
153
+ print ("Press Enter to stop...\n \n " )
154
+ input ()
155
+
156
+ # Close the connection
157
+ dg_connection .finish ()
158
+ print ("Finished - Fallback TTS example completed" )
159
+
160
+ except Exception as e :
161
+ print (f"An unexpected error occurred: { e } " )
162
+
163
+
164
+ if __name__ == "__main__" :
165
+ main ()
0 commit comments