future-beard-52446
09/06/2025, 8:45 AMjolly-air-155
09/06/2025, 8:58 AMtranscript_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 STTfuture-beard-52446
09/06/2025, 9:23 AMfew-analyst-46
09/06/2025, 10:11 AMfuture-beard-52446
09/06/2025, 10:16 AMjolly-air-155
09/06/2025, 10:32 AMWill 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
jolly-air-155
09/06/2025, 10:32 AMcommit_turn in the RPC handlerfuture-beard-52446
09/06/2025, 10:38 AMjolly-air-155
09/06/2025, 10:39 AMfuture-beard-52446
09/06/2025, 10:41 AMimport 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))future-beard-52446
09/06/2025, 10:42 AMlivekit-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.15jolly-air-155
09/06/2025, 10:43 AMfuture-beard-52446
09/06/2025, 10:43 AMpip 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.5future-beard-52446
09/06/2025, 10:46 AMjolly-air-155
09/06/2025, 11:01 AMjolly-air-155
09/06/2025, 11:02 AMfuture-beard-52446
09/06/2025, 11:07 AMfuture-beard-52446
09/06/2025, 11:08 AMjolly-air-155
09/06/2025, 11:24 AMstt_flush_duration like 10s for azure STTjolly-air-155
09/06/2025, 11:25 AMjolly-air-155
09/06/2025, 11:27 AMfuture-beard-52446
09/06/2025, 11:31 AMfuture-beard-52446
09/06/2025, 11:38 AM@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,
)