|
| 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 | +fromsignalimportSIGINT,SIGTERM |
| 5 | +importasyncio |
| 6 | +importtime |
| 7 | +fromdeepgram.utilsimportverboselogs |
| 8 | +fromdeepgramimport ( |
| 9 | +DeepgramClient, |
| 10 | +DeepgramClientOptions, |
| 11 | +AgentWebSocketEvents, |
| 12 | +SettingsOptions, |
| 13 | +) |
| 14 | +TTS_TEXT="Hello, this is a text to speech example using Deepgram." |
| 15 | +globalwarning_notice |
| 16 | +warning_notice=True |
| 17 | +asyncdefmain(): |
| 18 | +try: |
| 19 | +loop=asyncio.get_event_loop() |
| 20 | +forsignalin (SIGTERM,SIGINT): |
| 21 | +loop.add_signal_handler( |
| 22 | +signal, |
| 23 | +lambda:asyncio.create_task(shutdown(signal,loop,dg_connection)), |
| 24 | + ) |
| 25 | +# example of setting up a client config. logging values: WARNING, VERBOSE, DEBUG, SPAM |
| 26 | +config:DeepgramClientOptions=DeepgramClientOptions( |
| 27 | +options={ |
| 28 | +"keepalive":"true", |
| 29 | +"microphone_record":"true", |
| 30 | +"speaker_playback":"true", |
| 31 | + }, |
| 32 | +# verbose=verboselogs.DEBUG, |
| 33 | + ) |
| 34 | +# Initialize Deepgram client - API key should be set in DEEPGRAM_API_KEY environment variable |
| 35 | +# For production testing, make sure your API key has proper permissions |
| 36 | +deepgram:DeepgramClient=DeepgramClient("",config) |
| 37 | +print("Initialized Deepgram client for production API testing") |
| 38 | +# Create a websocket connection to Deepgram |
| 39 | +dg_connection=deepgram.agent.asyncwebsocket.v("1") |
| 40 | +asyncdefon_open(self,open,**kwargs): |
| 41 | +print(f"\n\n{open}\n\n") |
| 42 | +asyncdefon_binary_data(self,data,**kwargs): |
| 43 | +globalwarning_notice |
| 44 | +ifwarning_notice: |
| 45 | +print("Received binary data") |
| 46 | +print("You can do something with the binary data here") |
| 47 | +print("OR") |
| 48 | +print( |
| 49 | +"If you want to simply play the audio, set speaker_playback to true in the options for DeepgramClientOptions" |
| 50 | + ) |
| 51 | +warning_notice=False |
| 52 | +asyncdefon_welcome(self,welcome,**kwargs): |
| 53 | +print(f"\n\n{welcome}\n\n") |
| 54 | +asyncdefon_settings_applied(self,settings_applied,**kwargs): |
| 55 | +print(f"\n\n{settings_applied}\n\n") |
| 56 | +asyncdefon_conversation_text(self,conversation_text,**kwargs): |
| 57 | +print(f"\n\n{conversation_text}\n\n") |
| 58 | +asyncdefon_user_started_speaking(self,user_started_speaking,**kwargs): |
| 59 | +print(f"\n\n{user_started_speaking}\n\n") |
| 60 | +asyncdefon_agent_thinking(self,agent_thinking,**kwargs): |
| 61 | +print(f"\n\n{agent_thinking}\n\n") |
| 62 | +asyncdefon_agent_started_speaking(self,agent_started_speaking,**kwargs): |
| 63 | +print(f"\n\n{agent_started_speaking}\n\n") |
| 64 | +asyncdefon_agent_audio_done(self,agent_audio_done,**kwargs): |
| 65 | +print(f"\n\n{agent_audio_done}\n\n") |
| 66 | +asyncdefon_close(self,close,**kwargs): |
| 67 | +print(f"\n\n{close}\n\n") |
| 68 | +asyncdefon_error(self,error,**kwargs): |
| 69 | +print(f"\n\n{error}\n\n") |
| 70 | +asyncdefon_unhandled(self,unhandled,**kwargs): |
| 71 | +print(f"\n\n{unhandled}\n\n") |
| 72 | +dg_connection.on(AgentWebSocketEvents.Open,on_open) |
| 73 | +dg_connection.on(AgentWebSocketEvents.AudioData,on_binary_data) |
| 74 | +dg_connection.on(AgentWebSocketEvents.Welcome,on_welcome) |
| 75 | +dg_connection.on(AgentWebSocketEvents.SettingsApplied,on_settings_applied) |
| 76 | +dg_connection.on(AgentWebSocketEvents.ConversationText,on_conversation_text) |
| 77 | +dg_connection.on( |
| 78 | +AgentWebSocketEvents.UserStartedSpeaking,on_user_started_speaking |
| 79 | + ) |
| 80 | +dg_connection.on(AgentWebSocketEvents.AgentThinking,on_agent_thinking) |
| 81 | +dg_connection.on( |
| 82 | +AgentWebSocketEvents.AgentStartedSpeaking,on_agent_started_speaking |
| 83 | + ) |
| 84 | +dg_connection.on(AgentWebSocketEvents.AgentAudioDone,on_agent_audio_done) |
| 85 | +dg_connection.on(AgentWebSocketEvents.Close,on_close) |
| 86 | +dg_connection.on(AgentWebSocketEvents.Error,on_error) |
| 87 | +dg_connection.on(AgentWebSocketEvents.Unhandled,on_unhandled) |
| 88 | +# connect to websocket |
| 89 | +options=SettingsOptions() |
| 90 | +options.agent.think.provider.type="open_ai" |
| 91 | +options.agent.think.provider.model="gpt-4o-mini" |
| 92 | +options.agent.think.prompt="You are a helpful AI assistant." |
| 93 | +options.greeting="Hello, this is a text to speech example using Deepgram." |
| 94 | +options.agent.listen.provider.keyterms= ["hello","goodbye"] |
| 95 | +options.agent.listen.provider.model="nova-3" |
| 96 | +options.agent.listen.provider.type="deepgram" |
| 97 | +options.agent.speak.provider.type="deepgram" |
| 98 | +options.agent.speak.provider.model="aura-2-thalia-en" |
| 99 | +options.agent.language="en" |
| 100 | +# Add tags for production testing |
| 101 | +options.tags= ["production-test","sdk-example","agent-websocket","tags-validation"] |
| 102 | +print(f"Using tags:{options.tags}") |
| 103 | +# Print the full options being sent |
| 104 | +print("Options being sent to API:") |
| 105 | +print(options.to_json()) |
| 106 | +print("\n\n✅ Connection established with tags!") |
| 107 | +print(f"✅ Tags being used:{options.tags}") |
| 108 | +print("\n🎤 You can now speak into your microphone...") |
| 109 | +print("The agent will respond using the production API with tags.") |
| 110 | +print("Press Ctrl+C to stop.\n\n") |
| 111 | +ifawaitdg_connection.start(options)isFalse: |
| 112 | +print("Failed to start connection") |
| 113 | +return |
| 114 | +# wait until cancelled |
| 115 | +try: |
| 116 | +whileTrue: |
| 117 | +awaitasyncio.sleep(1) |
| 118 | +exceptasyncio.CancelledError: |
| 119 | +# This block will be executed when the shutdown coroutine cancels all tasks |
| 120 | +pass |
| 121 | +finally: |
| 122 | +awaitdg_connection.finish() |
| 123 | +print("Finished") |
| 124 | +exceptValueErrorase: |
| 125 | +print(f"Invalid value encountered:{e}") |
| 126 | +exceptExceptionase: |
| 127 | +print(f"An unexpected error occurred:{e}") |
| 128 | +asyncdefshutdown(signal,loop,dg_connection): |
| 129 | +print(f"Received exit signal{signal.name}...") |
| 130 | +awaitdg_connection.finish() |
| 131 | +tasks= [tfortinasyncio.all_tasks()iftisnotasyncio.current_task()] |
| 132 | + [task.cancel()fortaskintasks] |
| 133 | +print(f"Cancelling{len(tasks)} outstanding tasks") |
| 134 | +awaitasyncio.gather(*tasks,return_exceptions=True) |
| 135 | +loop.stop() |
| 136 | +print("Shutdown complete.") |
| 137 | +asyncio.run(main()) |