Skip to content

Commit a6fdb9f

Browse files
Merge pull request #290 from dvonthenen/live-client-custom-options
Implement Custom Options for Live Clients
2 parents 10a7850 + 3102d94 commit a6fdb9f

File tree

5 files changed

+59
-16
lines changed

5 files changed

+59
-16
lines changed

Diff for: deepgram/clients/live/v1/async_client.py

+23-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,17 @@ def __init__(self, config: DeepgramClientOptions):
4545
self._event_handlers = {event: [] for event in LiveTranscriptionEvents}
4646
self.websocket_url = convert_to_websocket_url(self.config.url, self.endpoint)
4747

48-
async def start(self, options: LiveOptions = None, addons: dict = None, **kwargs):
48+
async def start(
49+
self,
50+
options: LiveOptions = None,
51+
addons: dict = None,
52+
members: dict = None,
53+
**kwargs,
54+
):
4955
self.logger.debug("AsyncLiveClient.start ENTER")
5056
self.logger.info("kwargs: %s", options)
5157
self.logger.info("addons: %s", addons)
58+
self.logger.info("members: %s", members)
5259
self.logger.info("options: %s", kwargs)
5360

5461
if options is not None and not options.check():
@@ -57,8 +64,13 @@ async def start(self, options: LiveOptions = None, addons: dict = None, **kwargs
5764
raise DeepgramError("Fatal transcription options error")
5865

5966
self.options = options
60-
if addons is not None:
61-
self.__dict__.update(addons)
67+
self.addons = addons
68+
69+
# add "members" as members of the class
70+
if members is not None:
71+
self.__dict__.update(members)
72+
73+
# add "kwargs" as members of the class
6274
if kwargs is not None:
6375
self.kwargs = kwargs
6476
else:
@@ -68,7 +80,14 @@ async def start(self, options: LiveOptions = None, addons: dict = None, **kwargs
6880
self.logger.info("LiveOptions switching class -> json")
6981
self.options = self.options.to_dict()
7082

71-
url_with_params = append_query_params(self.websocket_url, self.options)
83+
combined_options = dict(self.options)
84+
if addons is not None:
85+
self.logger.info("merging addons to options")
86+
combined_options.update(addons)
87+
self.logger.info("new options: %s", combined_options)
88+
self.logger.debug("combined_options: %s", combined_options)
89+
90+
url_with_params = append_query_params(self.websocket_url, combined_options)
7291
try:
7392
self._socket = await _socket_connect(url_with_params, self.config.headers)
7493
asyncio.create_task(self._start())

Diff for: deepgram/clients/live/v1/client.py

+24-5
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,20 @@ def __init__(self, config: DeepgramClientOptions):
5050
self._event_handlers = {event: [] for event in LiveTranscriptionEvents}
5151
self.websocket_url = convert_to_websocket_url(self.config.url, self.endpoint)
5252

53-
def start(self, options: LiveOptions = None, addons: dict = None, **kwargs):
53+
def start(
54+
self,
55+
options: LiveOptions = None,
56+
addons: dict = None,
57+
members: dict = None,
58+
**kwargs,
59+
):
5460
"""
5561
Starts the WebSocket connection for live transcription.
5662
"""
5763
self.logger.debug("LiveClient.start ENTER")
5864
self.logger.info("kwargs: %s", options)
59-
self.logger.info("addon: %s", addons)
65+
self.logger.info("addons: %s", addons)
66+
self.logger.info("members: %s", members)
6067
self.logger.info("options: %s", kwargs)
6168

6269
if options is not None and not options.check():
@@ -65,8 +72,13 @@ def start(self, options: LiveOptions = None, addons: dict = None, **kwargs):
6572
raise DeepgramError("Fatal transcription options error")
6673

6774
self.options = options
68-
if addons is not None:
69-
self.__dict__.update(addons)
75+
self.addons = addons
76+
77+
# add "members" as members of the class
78+
if members is not None:
79+
self.__dict__.update(members)
80+
81+
# set kwargs as members of the class
7082
if kwargs is not None:
7183
self.kwargs = kwargs
7284
else:
@@ -81,7 +93,14 @@ def start(self, options: LiveOptions = None, addons: dict = None, **kwargs):
8193
self.logger.debug("LiveClient.start LEAVE")
8294
raise DeepgramWebsocketError("Websocket already started")
8395

84-
url_with_params = append_query_params(self.websocket_url, self.options)
96+
combined_options = dict(self.options)
97+
if addons is not None:
98+
self.logger.info("merging addons to options")
99+
combined_options.update(addons)
100+
self.logger.info("new options: %s", combined_options)
101+
self.logger.debug("combined_options: %s", combined_options)
102+
103+
url_with_params = append_query_params(self.websocket_url, combined_options)
85104
self._socket = connect(url_with_params, additional_headers=self.config.headers)
86105

87106
self.exit = False

Diff for: examples/advanced/streaming/microphone-inheritance/main.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ def main():
8989
utterance_end_ms="1000",
9090
vad_events=True,
9191
)
92-
liveClient.start(options, addons=dict(myattr="hello"), test="hello")
92+
liveClient.start(
93+
options,
94+
members=dict(myattr="hello", mytest="goodbye"),
95+
addons=dict(smart_format=True),
96+
test="hello",
97+
)
9398

9499
# Open a microphone stream
95100
microphone = Microphone(liveClient.send)

Diff for: examples/streaming/async_http/main.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@
1313

1414
API_KEY = os.getenv("DG_API_KEY")
1515

16-
options = LiveOptions(
17-
model="nova-2",
18-
language="en-US",
19-
)
20-
2116
# URL for the realtime streaming audio you would like to transcribe
2217
URL = "http://stream.live.vc.bbcmedia.co.uk/bbc_world_service"
2318

@@ -54,6 +49,11 @@ async def on_error(self, error, **kwargs):
5449
dg_connection.on(LiveTranscriptionEvents.Error, on_error)
5550

5651
# connect to websocket
52+
options = LiveOptions(
53+
model="nova-2",
54+
language="en-US",
55+
)
56+
5757
await dg_connection.start(options)
5858

5959
# Send streaming audio from the URL to Deepgram

Diff for: examples/streaming/microphone/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def on_error(self, error, **kwargs):
6666
utterance_end_ms="1000",
6767
vad_events=True,
6868
)
69-
dg_connection.start(options, addons=dict(myattr="hello"), test="hello")
69+
dg_connection.start(options)
7070

7171
# Open a microphone stream on the default input device
7272
microphone = Microphone(dg_connection.send)

0 commit comments

Comments
 (0)