Skip to content

Commit d4049d2

Browse files
committed
chore: update ignore files for manual tests
- Add tests/manual to .fernignore - Add .pytest_cache/ and .env to .gitignore
1 parent f9f4511 commit d4049d2

37 files changed

+3033
-1
lines changed

.fernignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ src/deepgram/client.py
1414
# - conftest.py: Changed from generated DeepgramApi to BaseClient for WireMock compatibility
1515
tests/wire/test_listen_v1_media.py
1616
tests/wire/test_speak_v1_audio.py
17-
tests/wire/conftest.py
17+
tests/wire/conftest.py
18+
19+
# Manual standalone tests
20+
tests/manual

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
__pycache__/
44
dist/
55
poetry.toml
6+
.pytest_cache/
7+
.env
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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")
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import threading
2+
import time
3+
4+
from dotenv import load_dotenv
5+
6+
print("Starting agent v1 connect example script")
7+
load_dotenv()
8+
print("Environment variables loaded")
9+
10+
from deepgram import DeepgramClient
11+
from deepgram.core.events import EventType
12+
from deepgram.agent.v1.types import (
13+
AgentV1AgentAudioDone,
14+
AgentV1AgentStartedSpeaking,
15+
AgentV1AgentThinking,
16+
AgentV1ConversationText,
17+
AgentV1Error,
18+
AgentV1FunctionCallRequest,
19+
AgentV1InjectionRefused,
20+
AgentV1PromptUpdated,
21+
AgentV1ReceiveFunctionCallResponse,
22+
AgentV1Settings,
23+
AgentV1SettingsAgent,
24+
AgentV1SettingsAgentListen,
25+
AgentV1SettingsAgentListenProvider,
26+
AgentV1SettingsAgentSpeak,
27+
AgentV1SettingsAgentSpeakOneItem,
28+
AgentV1SettingsAgentSpeakOneItemProvider_Deepgram,
29+
AgentV1SettingsAgentThink,
30+
AgentV1SettingsAgentThinkProviderZero,
31+
AgentV1SettingsAudio,
32+
AgentV1SettingsAudioInput,
33+
AgentV1SettingsApplied,
34+
AgentV1SpeakUpdated,
35+
AgentV1UserStartedSpeaking,
36+
AgentV1Warning,
37+
AgentV1Welcome,
38+
)
39+
from typing import Union
40+
41+
AgentV1SocketClientResponse = Union[
42+
AgentV1ReceiveFunctionCallResponse,
43+
AgentV1PromptUpdated,
44+
AgentV1SpeakUpdated,
45+
AgentV1InjectionRefused,
46+
AgentV1Welcome,
47+
AgentV1SettingsApplied,
48+
AgentV1ConversationText,
49+
AgentV1UserStartedSpeaking,
50+
AgentV1AgentThinking,
51+
AgentV1FunctionCallRequest,
52+
AgentV1AgentStartedSpeaking,
53+
AgentV1AgentAudioDone,
54+
AgentV1Error,
55+
AgentV1Warning,
56+
str,
57+
]
58+
59+
print("Initializing DeepgramClient")
60+
client = DeepgramClient()
61+
print("DeepgramClient initialized")
62+
63+
try:
64+
print("Establishing agent connection")
65+
with client.agent.v1.connect() as agent:
66+
print("Agent connection context entered")
67+
68+
# Send minimal settings to configure the agent per the latest spec
69+
print("Creating agent settings configuration")
70+
settings = AgentV1Settings(
71+
audio=AgentV1SettingsAudio(
72+
input=AgentV1SettingsAudioInput(
73+
encoding="linear16",
74+
sample_rate=44100,
75+
)
76+
),
77+
agent=AgentV1SettingsAgent(
78+
listen=AgentV1SettingsAgentListen(
79+
provider=AgentV1SettingsAgentListenProvider(
80+
type="deepgram",
81+
model="nova-3",
82+
smart_format=True,
83+
)
84+
),
85+
think=AgentV1SettingsAgentThink(
86+
provider=AgentV1SettingsAgentThinkProviderZero(
87+
type="open_ai",
88+
model="gpt-4o-mini",
89+
temperature=0.7,
90+
),
91+
prompt='Reply only and explicitly with "OK".',
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=44100")
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+
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 in a background thread for demo purposes
146+
# In production, you would typically call agent.start_listening() directly
147+
# which blocks until the connection closes, or integrate into your async event loop
148+
print("Starting listening thread")
149+
threading.Thread(target=agent.start_listening, daemon=True).start()
150+
print("Waiting 3 seconds for events...")
151+
time.sleep(3) # EXAMPLE ONLY: Wait briefly to see some events before exiting
152+
print("Exiting agent connection context")
153+
except Exception as e:
154+
print(f"Error occurred: {type(e).__name__}")
155+
# Log request headers if available
156+
if hasattr(e, "request_headers"):
157+
print(f"Request headers: {e.request_headers}")
158+
elif hasattr(e, "request") and hasattr(e.request, "headers"):
159+
print(f"Request headers: {e.request.headers}")
160+
# Log response headers if available
161+
if hasattr(e, "headers"):
162+
print(f"Response headers: {e.headers}")
163+
# Log status code if available
164+
if hasattr(e, "status_code"):
165+
print(f"Status code: {e.status_code}")
166+
# Log body if available
167+
if hasattr(e, "body"):
168+
print(f"Response body: {e.body}")
169+
print(f"Caught: {e}")
170+
print("Script completed")

0 commit comments

Comments
 (0)