|
| 1 | +import asyncio |
| 2 | + |
| 3 | +from dotenv import load_dotenv |
| 4 | + |
| 5 | +print("Starting async agent v1 connect example script") |
| 6 | +load_dotenv() |
| 7 | +print("Environment variables loaded") |
| 8 | + |
| 9 | +from deepgram import AsyncDeepgramClient |
| 10 | +from deepgram.core.events import EventType |
| 11 | +from deepgram.agent.v1.types import ( |
| 12 | + AgentV1AgentAudioDone, |
| 13 | + AgentV1AgentStartedSpeaking, |
| 14 | + AgentV1AgentThinking, |
| 15 | + AgentV1ConversationText, |
| 16 | + AgentV1Error, |
| 17 | + AgentV1FunctionCallRequest, |
| 18 | + AgentV1InjectionRefused, |
| 19 | + AgentV1PromptUpdated, |
| 20 | + AgentV1ReceiveFunctionCallResponse, |
| 21 | + AgentV1Settings, |
| 22 | + AgentV1SettingsAgent, |
| 23 | + AgentV1SettingsAgentListen, |
| 24 | + AgentV1SettingsAgentListenProvider, |
| 25 | + AgentV1SettingsAgentSpeak, |
| 26 | + AgentV1SettingsAgentSpeakOneItem, |
| 27 | + AgentV1SettingsAgentSpeakOneItemProvider_Deepgram, |
| 28 | + AgentV1SettingsAgentThink, |
| 29 | + AgentV1SettingsAgentThinkProviderZero, |
| 30 | + AgentV1SettingsAudio, |
| 31 | + AgentV1SettingsAudioInput, |
| 32 | + AgentV1SettingsApplied, |
| 33 | + AgentV1SpeakUpdated, |
| 34 | + AgentV1UserStartedSpeaking, |
| 35 | + AgentV1Warning, |
| 36 | + AgentV1Welcome, |
| 37 | +) |
| 38 | +from typing import Union |
| 39 | + |
| 40 | +AgentV1SocketClientResponse = Union[ |
| 41 | + AgentV1ReceiveFunctionCallResponse, |
| 42 | + AgentV1PromptUpdated, |
| 43 | + AgentV1SpeakUpdated, |
| 44 | + AgentV1InjectionRefused, |
| 45 | + AgentV1Welcome, |
| 46 | + AgentV1SettingsApplied, |
| 47 | + AgentV1ConversationText, |
| 48 | + AgentV1UserStartedSpeaking, |
| 49 | + AgentV1AgentThinking, |
| 50 | + AgentV1FunctionCallRequest, |
| 51 | + AgentV1AgentStartedSpeaking, |
| 52 | + AgentV1AgentAudioDone, |
| 53 | + AgentV1Error, |
| 54 | + AgentV1Warning, |
| 55 | + str, |
| 56 | +] |
| 57 | + |
| 58 | +print("Initializing AsyncDeepgramClient") |
| 59 | +client = AsyncDeepgramClient() |
| 60 | +print("AsyncDeepgramClient initialized") |
| 61 | + |
| 62 | + |
| 63 | +async def main() -> None: |
| 64 | + try: |
| 65 | + print("Establishing async agent connection") |
| 66 | + async with client.agent.v1.connect() as agent: |
| 67 | + print("Agent connection context entered") |
| 68 | + |
| 69 | + # Send minimal settings to configure the agent per the latest spec |
| 70 | + print("Creating agent settings configuration") |
| 71 | + settings = AgentV1Settings( |
| 72 | + audio=AgentV1SettingsAudio( |
| 73 | + input=AgentV1SettingsAudioInput( |
| 74 | + encoding="linear16", |
| 75 | + sample_rate=16000, |
| 76 | + ) |
| 77 | + ), |
| 78 | + agent=AgentV1SettingsAgent( |
| 79 | + listen=AgentV1SettingsAgentListen( |
| 80 | + provider=AgentV1SettingsAgentListenProvider( |
| 81 | + type="deepgram", |
| 82 | + model="nova-3", |
| 83 | + smart_format=True, |
| 84 | + ) |
| 85 | + ), |
| 86 | + think=AgentV1SettingsAgentThink( |
| 87 | + provider=AgentV1SettingsAgentThinkProviderZero( |
| 88 | + type="open_ai", |
| 89 | + model="gpt-4o-mini", |
| 90 | + temperature=0.7, |
| 91 | + ) |
| 92 | + ), |
| 93 | + speak=[ |
| 94 | + AgentV1SettingsAgentSpeakOneItem( |
| 95 | + provider=AgentV1SettingsAgentSpeakOneItemProvider_Deepgram( |
| 96 | + type="deepgram", |
| 97 | + model="aura-2-asteria-en", |
| 98 | + ) |
| 99 | + ) |
| 100 | + ], |
| 101 | + ), |
| 102 | + ) |
| 103 | + print("Settings configuration created") |
| 104 | + print(f" - Audio: encoding=linear16, sample_rate=16000") |
| 105 | + print(f" - Listen: type=deepgram, model=nova-3") |
| 106 | + print(f" - Think: type=open_ai, model=gpt-4o-mini") |
| 107 | + print(f" - Speak: type=deepgram, model=aura-2-asteria-en") |
| 108 | + |
| 109 | + print("Sending SettingsConfiguration message") |
| 110 | + await agent.send_agent_v_1_settings(settings) |
| 111 | + print("Settings sent successfully") |
| 112 | + |
| 113 | + def on_message(message: AgentV1SocketClientResponse) -> None: |
| 114 | + if isinstance(message, bytes): |
| 115 | + print("Received audio event") |
| 116 | + print(f"Event body (audio data length): {len(message)} bytes") |
| 117 | + else: |
| 118 | + msg_type = getattr(message, "type", "Unknown") |
| 119 | + print(f"Received {msg_type} event") |
| 120 | + # For transcription events, extract full transcription; otherwise show full event body |
| 121 | + if msg_type == "Results" or (hasattr(message, "type") and str(message.type) == "Results"): |
| 122 | + # Extract transcription from Results event |
| 123 | + if hasattr(message, "channel") and message.channel: |
| 124 | + channel = message.channel |
| 125 | + if hasattr(channel, "alternatives") and channel.alternatives: |
| 126 | + alt = channel.alternatives[0] |
| 127 | + if hasattr(alt, "transcript") and alt.transcript: |
| 128 | + print(f"Full transcription: {alt.transcript}") |
| 129 | + else: |
| 130 | + print(f"Event body: {message}") |
| 131 | + else: |
| 132 | + print(f"Event body: {message}") |
| 133 | + else: |
| 134 | + print(f"Event body: {message}") |
| 135 | + else: |
| 136 | + print(f"Event body: {message}") |
| 137 | + |
| 138 | + print("Registering event handlers") |
| 139 | + agent.on(EventType.OPEN, lambda _: print("Connection opened")) |
| 140 | + agent.on(EventType.MESSAGE, on_message) |
| 141 | + agent.on(EventType.CLOSE, lambda _: print("Connection closed")) |
| 142 | + agent.on(EventType.ERROR, lambda error: print(f"Connection error: {error}")) |
| 143 | + print("Event handlers registered") |
| 144 | + |
| 145 | + # EXAMPLE ONLY: Start listening task and cancel after brief demo |
| 146 | + # In production, you would typically await agent.start_listening() directly |
| 147 | + # which runs until the connection closes or is interrupted |
| 148 | + print("Starting listening task") |
| 149 | + listen_task = asyncio.create_task(agent.start_listening()) |
| 150 | + print("Waiting 3 seconds for events...") |
| 151 | + await asyncio.sleep(3) # EXAMPLE ONLY: Wait briefly to see some events before exiting |
| 152 | + print("Cancelling listening task") |
| 153 | + listen_task.cancel() |
| 154 | + print("Exiting agent connection context") |
| 155 | + except Exception as e: |
| 156 | + print(f"Error occurred: {type(e).__name__}") |
| 157 | + # Log request headers if available |
| 158 | + if hasattr(e, "request_headers"): |
| 159 | + print(f"Request headers: {e.request_headers}") |
| 160 | + elif hasattr(e, "request") and hasattr(e.request, "headers"): |
| 161 | + print(f"Request headers: {e.request.headers}") |
| 162 | + # Log response headers if available |
| 163 | + if hasattr(e, "headers"): |
| 164 | + print(f"Response headers: {e.headers}") |
| 165 | + # Log status code if available |
| 166 | + if hasattr(e, "status_code"): |
| 167 | + print(f"Status code: {e.status_code}") |
| 168 | + # Log body if available |
| 169 | + if hasattr(e, "body"): |
| 170 | + print(f"Response body: {e.body}") |
| 171 | + print(f"Caught: {e}") |
| 172 | + |
| 173 | + |
| 174 | +print("Running async main function") |
| 175 | +asyncio.run(main()) |
| 176 | +print("Script completed") |
0 commit comments