ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

Pydantic AI 实时会话中模型重复同一句回复怎么处理

Pydantic AI 实时会话中模型重复同一句回复怎么处理 Pydantic AI 实时会话中模型重复同一句回复怎么处理【免费下载链接】pydantic-aiHow Python does AI. Agents, realtime voice, image generation, embeddings. Every model, every interface, typed end to end.项目地址: https://gitcode.com/GitHub_Trending/py/pydantic-ai在使用 Pydantic AI 的 realtime 会话RealtimeSession构建语音助手时有一种典型的故障现象模型把同一句回复说了两遍。官方排障文档把这个现象单列为一节“The model says the same thing twice”并给出了明确的根因和修法send(...)发送文本回合时已经在向模型请求一次回复如果紧接着又调用create_response()等于对同一轮输入提出了两次回复请求模型就可能把同一句话说两遍。本文按文档说明给出定位方法、正确写法和验证方式。根因文本回合被请求了两次realtime 会话的文本回合语义在 docs/realtime/turns.md 中有明确定义Sending a string creates a complete user turn and asks the model to reply.发送字符串即构成一个完整的用户回合并会请求模型回复。也就是说下面这种写法是问题所在await session.send(Greet the visitor.) # 已经请求了一次回复 await session.create_response() # 又请求了第二次回复 —— 多余turns.md原文指出“Do not callcreate_response()aftersend(...): the text turn already asks for a response, so the pair asks twice and can make the model say the same thing twice.”不要在send(...)之后调用create_response()文本回合已经请求了回复两者连用会请求两次可能导致模型把同一句话说两遍。send()的 docstringpydantic_ai_slim/pydantic_ai/realtime/_session.py也写得很直接Astris a complete text turn that the model replies to. Do not followsend(...)withcreate_response(): that asks for two responses.正确写法按输入类型区分回合请求修复方式不是加参数而是按你实际发送的内容选择正确的回合驱动方式。文档给出三类场景1. 发文本只用send()不要跟create_response()需要回复的文本await session.send(Greet the visitor.)只想补充上下文、不触发回复时用respondFalse而不是发完再靠后续操作控制from pydantic_ai.realtime import RealtimeSession async def send_turns(session: RealtimeSession) - None: # 正常文本回合请求回复 await session.send(Greet the visitor.) # 添加上下文不请求回复 await session.send(The visitor is called Ada., respondFalse)注意respond参数的边界同样来自turns.md与send()的说明图片默认只作为上下文要求对图片回复需传respondTrue且这要求模型支持手动回合控制音频不能配合respondTrue使用因为语音回合的结束由 VAD 或commit_audio()决定。2. 发音频自动回合检测靠 VAD也不要用create_response()自动回合检测默认开启。麦克风音频通过send_audio()送入服务端 VAD 检测到用户说完后模型会自动响应全程不需要、也不应该手动触发回复。3. 按键说话push-to-talkcommit_audio()create_response()才配对使用只有在关闭自动回合检测turn_detectionFalse需要模型 profile 声明supports_manual_turn_control的 push-to-talk 场景下create_response()才是必需的因为它取代了 VAD 的触发作用。文档说明“with turn detection off, committing the buffer only finalizes the users input; nothing triggers a reply until you ask for one.”关闭回合检测后commit_audio()只是确认用户输入结束必须显式请求才会产生回复。from pydantic_ai import Agent from pydantic_ai.realtime.openai import OpenAIRealtimeModel, OpenAIRealtimeModelSettings agent Agent() model OpenAIRealtimeModel( gpt-realtime, settingsOpenAIRealtimeModelSettings(turn_detectionFalse) ) async def main(): async with agent.realtime(model).session() as session: await session.send_audio(b...) await session.commit_audio() await session.create_response()这里的顺序不能乱先commit_audio()结束用户回合再create_response()请求回复。这也是排障文档中“模型不响应”一节的对应写法——push-to-talk 模式下漏掉其中一步表现是模型沉默而不是重复回复。如何确认修好了判断标准很简单一次用户输入只产生一次模型响应。验证时可以观察会话事件流事件流直接迭代session中RealtimeTurnCompleteEvent 标记回合结束——模型说完、工具调用完成后该事件到达一次就说明这一轮只产生了一个完整回复。启用 Logfire 时嵌套的chat {model}span 代表一次 provider 响应一个文本回合对应一个响应 span而不是两个。另外create_response()本身还有一个容易误判的行为值得知道如果调用时已有响应在进行中该请求会被挂起、等当前响应结束后再发出且如果期间用户插话则被丢弃因此create_response()返回不代表模型已经开始说话docs/realtime/turns.md。这解释了为什么“发完消息再手动补一个create_response()”这种代码即便看起来时序上晚于第一句回复仍然会产生第二次回复。边界与相关限制模型能力commit_audio()、clear_audio()、create_response()都受模型 profile 的supports_manual_turn_control限制调用不支持的方法会在发送控制消息前抛出UserErrordocs/realtime/turns.md。例如 Gemini 不通过 Pydantic AI 暴露手动回合指令turn_detectionFalse会在连接前抛出UserError。重复回复与回声问题的区分如果“说两遍”实际是模型被反复打断后重新回复或问候语没播完就被打断根因通常是扬声器回声被麦克风拾取属于另一类问题——需在设备/WebRTC 层加回声消除并在真实 barge-in 时停止本地播放docs/realtime/troubleshooting.md。这类现象可以通过事件流中的RealtimeResponseInterruptedEvent或 Logfire 痕迹加以区分。不要依赖固定延时等待问候语播完应等待其定稿的SpeechPart并让播放循环排空文档明确说“a fixed sleep tells you neither”固定 sleep 两种情况都判断不了。参考文档realtime 排障页、回合与打断、事件参考、音频与转录。【免费下载链接】pydantic-aiHow Python does AI. Agents, realtime voice, image generation, embeddings. Every model, every interface, typed end to end.项目地址: https://gitcode.com/GitHub_Trending/py/pydantic-ai创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表