You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用LiveKit多Agent工作流:调用generate_reply()后Agent转译失败

问题:LiveKit多Agent对话中调用generate_reply后无法转译至其他Agent

场景描述

基于LiveKit开发语音多Agent对话工作流,部分场景需要当前Agent生成语音回复后转译至其他Agent。使用generate_reply()生成回复后,转译操作失败,但改用say()函数播报后再转译可正常工作。

简化代码片段

@function_tool
async def some_function(self, context: RunContext_T) -> Agent:
    self.session.interrupt()
    await self.session.generate_reply(user_input="Where is Taj Mahal?")
    return await self._transfer_to_agent("support", context)

预期与实际行为

预期行为

  • 当前Agent中断会话
  • 调用generate_reply()生成并播报回复
  • 通过_transfer_to_agent()将会话转译至support Agent

实际行为

  • Agent成功生成并播报回复
  • 无法完成至support Agent的转译

技术栈

  • LLM模型:gpt-4o-2024-08-06
  • STT:Deepgram
  • TTS:Cartesia
  • VAD:Silero
  • 相关库版本:livekit==1.0.1、livekit-agents==1.0.0rc4

解决方案建议

1. 替换为显式文本生成+say()播报

generate_reply()内部可能未等待TTS播放完成就继续执行后续代码,导致转译被中断。改用先调用LLM生成回复文本,再用say()(会等待播放完成)播报,之后执行转译:

@function_tool
async def some_function(self, context: RunContext_T) -> Agent:
    self.session.interrupt()
    # 手动调用LLM生成回复内容
    reply_text = await self.llm.generate("Where is Taj Mahal?")
    # 等待播报完成
    await self.session.say(reply_text)
    return await self._transfer_to_agent("support", context)

2. 等待generate_reply的TTS播放完成

检查session对象是否有等待TTS播放完成的API,在generate_reply()后显式等待播放结束,再执行转译:

@function_tool
async def some_function(self, context: RunContext_T) -> Agent:
    self.session.interrupt()
    await self.session.generate_reply(user_input="Where is Taj Mahal?")
    # 等待TTS播放任务完成(根据LiveKit实际API调整)
    await self.session._tts_player.wait_done()
    return await self._transfer_to_agent("support", context)

3. 利用回调触发转译

通过generate_reply()的回调函数,在回复播放完成后执行转译操作,避免同步流程冲突:

@function_tool
async def some_function(self, context: RunContext_T) -> None:
    self.session.interrupt()
    
    async def on_reply_played():
        await self._transfer_to_agent("support", context)
    
    # 注册播放完成回调,确保转译在播报结束后执行
    await self.session.generate_reply(
        user_input="Where is Taj Mahal?",
        on_finished=on_reply_played
    )

4. 升级livekit-agents版本

当前使用的是候选版本(rc4),可能存在异步流程的已知bug,尝试升级到最新稳定版,看是否修复了generate_reply()与转译操作的冲突问题。

内容的提问来源于stack exchange,提问作者Meera Gohil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 05:38:10