I'm using a push-to-talk instance and have replace...
# agents
f
I'm using a push-to-talk instance and have replaced STT with azure.STT, but I'm having some issues. Could you help me out? When the input voice is very short, such as "hello" (quickly calling end_turn); The prompt "ignore empty user turn" appears. I see that STT has content. @refined-appointment-81829 @jolly-air-155
j
do you mean the STT returned the transcript after the turn committed? you may adjust the
transcript_timeout
here https://github.com/livekit/agents/blob/livekit-agents%401.2.8/examples/voice_agents/push_to_talk.py#L65C13-L65C31 to wait a longer time for the STT
f
I set transcript_timeout=10.0, but the above behavior still sometimes occurs. I've observed that STT has _on_recognizing and then processes the contents of _on_recognizing; _on_recognized is not triggered. The next time start_turn is called, the previous _on_recognized is called. Is this normal?
f
i used to append an silent audio with some duration to force generation 😂
f
Will STT stop working after calling session.input.set_audio_enabled(False)? I can't get stt.SpeechEventType.FINAL_TRANSCRIPT
j
Will STT stop working after calling session.input.set_audio_enabled(False)?
yes, the framework will add a 1s empty audio when commit the user turn, maybe that’s not enough for azure STT to finalize the transcript
you can also add a small delay before turn off the audio and
commit_turn
in the RPC handler
f
I also don't receive an INTERIM_TRANSCRIPT. Only when start_turn is called the next time do the contents of the previous round's INTERIM_TRANSCRIPT and FINAL_TRANSCRIPT appear immediately and are discarded. Why does the previous round's transcription appear in the next round?
j
This shouldn’t happen, can you share a reproducible example and your agents versions?
f
Copy code
import logging
import os

from dotenv import load_dotenv

from livekit import rtc
from livekit.agents import Agent, AgentSession, JobContext, JobRequest, RoomIO, WorkerOptions, cli
from livekit.agents.llm import ChatContext, ChatMessage, StopResponse
from livekit.plugins import azure, openai

logger = logging.getLogger("push-to-talk")
logger.setLevel(logging.DEBUG)

load_dotenv()


class MyAgent(Agent):
    def __init__(self) -> None:
        super().__init__(
            instructions=f"""你是专业的口译翻译器。请将输入内容准确翻译成英文。

要求:
1. 保持原文的语气、语调和情感
2. 使用自然流畅的口语表达
3. 适当处理文化差异和习语
4. 保留重要的语境信息
5. 只返回翻译结果,不添加任何解释或额外内容

注意:这是面对面对话翻译,要确保翻译自然、口语化。""",
            stt=azure.STT(
                language=["zh-CN"],
                segmentation_silence_timeout_ms=100,
            ),
            llm=openai.LLM(
                base_url=os.environ.get("LLM_BASE_URL"),
                api_key=os.environ.get("LLM_API_KEY"),
                model=os.environ.get("LLM_MODEL_NAME"),
            ),
            tts=azure.TTS(),
        )

    async def on_user_turn_completed(self, turn_ctx: ChatContext, new_message: ChatMessage) -> None:
        # callback before generating a reply after user turn committed
        if not new_message.text_content:
            # for example, raise StopResponse to stop the agent from generating a reply
            logger.info("ignore empty user turn")
            raise StopResponse()


async def entrypoint(ctx: JobContext):
    session = AgentSession(turn_detection="manual")
    room_io = RoomIO(session, room=ctx.room)
    await room_io.start()

    agent = MyAgent()
    await session.start(agent=agent)

    # disable input audio at the start
    session.input.set_audio_enabled(False)

    @ctx.room.local_participant.register_rpc_method("start_turn")
    async def start_turn(data: rtc.RpcInvocationData):
        session.interrupt()
        session.clear_user_turn()

        session.input.set_audio_enabled(True)

    @ctx.room.local_participant.register_rpc_method("end_turn")
    async def end_turn(data: rtc.RpcInvocationData):
        session.input.set_audio_enabled(False)
        session.commit_user_turn(
            transcript_timeout=10.0,
        )

    @ctx.room.local_participant.register_rpc_method("cancel_turn")
    async def cancel_turn(data: rtc.RpcInvocationData):
        session.input.set_audio_enabled(False)
        session.clear_user_turn()
        logger.info("cancel turn")


async def handle_request(request: JobRequest) -> None:
    await request.accept(
        identity="ptt-agent",
        # this attribute communicates to frontend that we support PTT
        attributes={"push-to-talk": "1"},
    )


if __name__ == "__main__":
    cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint, request_fnc=handle_request))
Copy code
livekit-agents~=1.0
livekit-plugins-openai~=1.0
livekit-plugins-azure~=1.0
livekit-plugins-silero~=1.0
livekit-plugins-turn-detector~=1.0
livekit==1.0.12
python-dotenv~=1.0
loguru==0.7.3
pydantic~=2.11.7
httpx~=0.28.1
aiohttp==3.12.15
j
Can you share the output of pip list | grep livekit?
f
Copy code
pip list | grep livekit
livekit                                  1.0.12
livekit-agents                           1.2.8
livekit-api                              1.0.5
livekit-blingfire                        1.0.0
livekit-plugins-azure                    1.2.8
livekit-plugins-openai                   1.2.8
livekit-plugins-silero                   1.2.8
livekit-plugins-turn-detector            1.2.8
livekit-protocol                         1.0.5
Need to use very short input, such as 1 to 2 Word.
j
okay I see your issue, it’s the empty audio (1s) appended when committing the turn is not enough for azure STT, it only generates the interim transcript but not the final one
it was fixed when I changed it to 2s, I think we can make this configurable, will create a pr for it
f
I added a log to STT and did not generate temporary text content, because if temporary text content was generated, it would also be processed after the timeout.
When the input is very short, it will be printed at the beginning of the next round
j
it should be fixed in https://github.com/livekit/agents/pull/3358, you can use a larger
stt_flush_duration
like 10s for azure STT
tho I have tested that it still cannot generate final transcript for short input all the time but much better than 1s
the issue about interim transcript should also fixed, if the final transcript is not received before timeout, the agent will use the interim transcript as the output of the user turn, maybe that the issue you got
f
Okay, thank you very much. May I ask when it will be completed?
Copy code
@ctx.room.local_participant.register_rpc_method("end_turn")
    async def end_turn(data: rtc.RpcInvocationData):
        session.input.set_audio_enabled(False)

        await asyncio.sleep(0.5) # Will adding a delay here improve the situation? Can I call commit_user_turn() first and then set_audio_enabled(False)?

        session.commit_user_turn(
            transcript_timeout=10.0,
        )