agent.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """
  2. LiveKit ASR Agent — publishes user speech transcriptions to the room.
  3. """
  4. from __future__ import annotations
  5. import logging
  6. from livekit import agents, rtc
  7. from livekit.agents import Agent, AgentSession, JobContext, cli, inference
  8. from livekit.agents.inference import TurnDetector
  9. from livekit.plugins import noise_cancellation, silero
  10. from qwen_stt import QwenSTT
  11. logger = logging.getLogger("asr-agent")
  12. LIVEKIT_URL = "ws://localhost:7880"
  13. LIVEKIT_API_KEY = "devkey"
  14. LIVEKIT_API_SECRET = "secretsecretsecretsecretsecret12"
  15. class ASRAgent(Agent):
  16. """
  17. Speech-to-text agent. Transcribes user speech and sends results
  18. back to the room via data messages / agent utterances.
  19. No LLM or TTS — pure transcription for now.
  20. """
  21. def __init__(self) -> None:
  22. super().__init__(
  23. instructions="将用户语音实时转写为文字。"
  24. )
  25. async def on_enter(self):
  26. # Send a welcome message to the room chat
  27. await self.session.send_utterance(
  28. instructions="ASR 转写已就绪,请开始说话。"
  29. )
  30. server = agents.AgentServer()
  31. @server.rtc_session()
  32. async def entrypoint(ctx: JobContext):
  33. logger.info(f"new session: room={ctx.room.name}")
  34. session = AgentSession(
  35. stt=QwenSTT(
  36. model_id="Qwen/Qwen3-ASR-0.6B",
  37. silence_repeats=3,
  38. silence_seconds=3.0,
  39. ),
  40. vad=inference.VAD(),
  41. turn_detection=TurnDetector(),
  42. )
  43. await session.start(
  44. agent=ASRAgent(),
  45. room=ctx.room,
  46. room_input_options=agents.RoomInputOptions(
  47. noise_cancellation=noise_cancellation.BVC(),
  48. ),
  49. )
  50. if __name__ == "__main__":
  51. cli.run_app(server)