|
2 | 2 | import base64 |
3 | 3 | import subprocess |
4 | 4 | import typing |
| 5 | +import urllib.parse |
5 | 6 | from enum import Enum |
6 | 7 | from typing import overload |
7 | 8 |
|
@@ -371,26 +372,27 @@ def _build_websocket_url( |
371 | 372 | base = self.base_url.replace("https://", "wss://").replace("http://", "ws://") |
372 | 373 |
|
373 | 374 | # Build query parameters |
374 | | - params = [ |
375 | | - f"model_id={model_id}", |
376 | | - f"audio_format={audio_format}", |
377 | | - f"commit_strategy={commit_strategy}" |
| 375 | + params: list[tuple[str, str]] = [ |
| 376 | + ("model_id", model_id), |
| 377 | + ("audio_format", audio_format), |
| 378 | + ("commit_strategy", commit_strategy), |
378 | 379 | ] |
379 | 380 |
|
380 | | - # Add optional VAD parameters |
381 | | - if vad_silence_threshold_secs is not None: |
382 | | - params.append(f"vad_silence_threshold_secs={vad_silence_threshold_secs}") |
383 | | - if vad_threshold is not None: |
384 | | - params.append(f"vad_threshold={vad_threshold}") |
385 | | - if min_speech_duration_ms is not None: |
386 | | - params.append(f"min_speech_duration_ms={min_speech_duration_ms}") |
387 | | - if min_silence_duration_ms is not None: |
388 | | - params.append(f"min_silence_duration_ms={min_silence_duration_ms}") |
389 | | - if language_code is not None: |
390 | | - params.append(f"language_code={language_code}") |
| 381 | + # Add optional parameters |
| 382 | + optional: list[tuple[str, typing.Any]] = [ |
| 383 | + ("vad_silence_threshold_secs", vad_silence_threshold_secs), |
| 384 | + ("vad_threshold", vad_threshold), |
| 385 | + ("min_speech_duration_ms", min_speech_duration_ms), |
| 386 | + ("min_silence_duration_ms", min_silence_duration_ms), |
| 387 | + ("language_code", language_code), |
| 388 | + ] |
| 389 | + for key, value in optional: |
| 390 | + if value is not None: |
| 391 | + params.append((key, str(value))) |
| 392 | + |
391 | 393 | if include_timestamps is not None: |
392 | | - params.append(f"include_timestamps={str(include_timestamps).lower()}") |
| 394 | + params.append(("include_timestamps", str(include_timestamps).lower())) |
393 | 395 |
|
394 | | - query_string = "&".join(params) |
| 396 | + query_string = urllib.parse.urlencode(params) |
395 | 397 | return f"{base}/v1/speech-to-text/realtime?{query_string}" |
396 | 398 |
|
0 commit comments