Repository: livekit/agents
File: livekit-plugins/livekit-plugins-azure/livekit/plugins/azure/stt.py (Python)

Code before:
        region=config.speech_region if is_given(config.speech_region) else None,
        endpoint=config.speech_endpoint if is_given(config.speech_endpoint) else None,
        host=config.speech_host if is_given(config.speech_host) else None,
        auth_token=config.speech_auth_token if is_given(config.speech_auth_token) else None,
    )

    if config.segmentation_silence_timeout_ms:
        speech_config.set_property(
            speechsdk.enums.PropertyId.Speech_SegmentationSilenceTimeoutMs,
            str(config.segmentation_silence_timeout_ms),
        )
    if config.segmentation_max_time_ms:
        speech_config.set_property(
            speechsdk.enums.PropertyId.Speech_SegmentationMaximumTimeMs,
            str(config.segmentation_max_time_ms),
        )
    if config.segmentation_strategy:
        speech_config.set_property(
            speechsdk.enums.PropertyId.Speech_SegmentationStrategy,
            str(config.segmentation_strategy),
        )
    if is_given(config.profanity):
        speech_config.set_profanity(config.profanity)

    # Set punctuation behavior if specified
    if is_given(config.punctuation) and config.punctuation is False:
        speech_config.set_service_property(
            "punctuation", "explicit", speechsdk.ServicePropertyChannel.UriQueryParameter
        )

    kwargs: dict[str, Any] = {}
    if config.language and len(config.language) > 1:
        kwargs["auto_detect_source_language_config"] = (
            speechsdk.languageconfig.AutoDetectSourceLanguageConfig(languages=config.language)
        )
    elif config.language and len(config.language) == 1:
        kwargs["language"] = config.language[0]

    audio_config = speechsdk.audio.AudioConfig(stream=stream)
    speech_recognizer = speechsdk.SpeechRecognizer(
        speech_config=speech_config, audio_config=audio_config, **kwargs
    )

    # Add phrase list for keyword boosting if provided
    if is_given(config.phrase_list) and isinstance(config.phrase_list, list) and config.phrase_list:
        phrase_list_grammar = speechsdk.PhraseListGrammar.from_recognizer(speech_recognizer)
        for phrase in config.phrase_list:
            phrase_list_grammar.addPhrase(phrase)

    return speech_recognizer


Review comment (suggestion):
nit:
```suggestion
    if config.punctuation is False:
```

and according to https://learn.microsoft.com/en-us/azure/ai-services/speech-service/display-text-format?pivots=programming-language-python#punctuation, set it to `explicit` means the STT will recognize the explicitly read punctuations but not add it automatically.

should we call this param `explicit_punctuation: bool` instead of `punctuation: bool`? the later one makes people think it's fully disable the punctuation which is not the case.

Code after:
        subscription=config.speech_key if is_given(config.speech_key) else None,
        region=config.speech_region if is_given(config.speech_region) else None,
        endpoint=config.speech_endpoint if is_given(config.speech_endpoint) else None,
        host=config.speech_host if is_given(config.speech_host) else None,
        auth_token=config.speech_auth_token if is_given(config.speech_auth_token) else None,
    )

    if config.segmentation_silence_timeout_ms:
        speech_config.set_property(
            speechsdk.enums.PropertyId.Speech_SegmentationSilenceTimeoutMs,
            str(config.segmentation_silence_timeout_ms),
        )
    if config.segmentation_max_time_ms:
        speech_config.set_property(
            speechsdk.enums.PropertyId.Speech_SegmentationMaximumTimeMs,
            str(config.segmentation_max_time_ms),
        )
    if config.segmentation_strategy:
        speech_config.set_property(
            speechsdk.enums.PropertyId.Speech_SegmentationStrategy,
            str(config.segmentation_strategy),
        )
    if is_given(config.profanity):
        speech_config.set_profanity(config.profanity)

    # Set punctuation behavior if specified
    if config.explicit_punctuation:
        speech_config.set_service_property(
            "punctuation", "explicit", speechsdk.ServicePropertyChannel.UriQueryParameter
        )

    kwargs: dict[str, Any] = {}
    if config.language and len(config.language) > 1:
        kwargs["auto_detect_source_language_config"] = (
            speechsdk.languageconfig.AutoDetectSourceLanguageConfig(languages=config.language)
        )
    elif config.language and len(config.language) == 1:
        kwargs["language"] = config.language[0]

    audio_config = speechsdk.audio.AudioConfig(stream=stream)
    speech_recognizer = speechsdk.SpeechRecognizer(
        speech_config=speech_config, audio_config=audio_config, **kwargs
    )

    # Add phrase list for keyword boosting if provided
    if is_given(config.phrase_list) and isinstance(config.phrase_list, list) and config.phrase_list:
        phrase_list_grammar = speechsdk.PhraseListGrammar.from_recognizer(speech_recognizer)
        for phrase in config.phrase_list:
            phrase_list_grammar.addPhrase(phrase)

    return speech_recognizer