Просмотр исходного кода

fix: agent67 ReAct loop — don't break on text-only when file save pending

Root cause: when LLM outputs comparison table as plain text (no JSON
tool call), the ReAct loop immediately breaks. The LLM never gets a
chance to call WriteFile in the next step.

Fix: detect if user requested a file operation (保存/写入/save/write)
but LLM only output text. Feed the text back as observation with a
system reminder to call WriteFile, giving LLM another iteration.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
kenny67nju 7 месяцев назад
Родитель
Сommit
794afab14b
1 измененных файлов с 21 добавлено и 5 удалено
  1. 21 5
      agentexample/agent67/core/assistant.py

+ 21 - 5
agentexample/agent67/core/assistant.py

@@ -350,12 +350,28 @@ class PersonalAssistant:
 
                 observations.append(f"[工具调用] {tool_call}\n[执行结果] {result}")
             else:
-                # 纯文本回复(流式模式下已经显示过了)
-                if not is_streaming:
-                    final_response = str(llm_output)
+                # 纯文本回复,没有工具调用
+                # 检查是否应该继续(用户任务可能还没完成)
+                output_str = str(llm_output)
+                has_pending_action = any(kw in user_msg for kw in [
+                    "保存", "写入", "创建文件", "save", "write",
+                    "生成报告", "导出", "保存到", "写到",
+                ])
+
+                if has_pending_action and step < max_steps - 2 and not any(
+                    sig in output_str.lower() for sig in ["任务完成", "已保存", "已写入", "已创建"]
+                ):
+                    # 用户要求了文件操作但 LLM 只输出了文本,继续推动
+                    observations.append(
+                        f"[LLM回复] {output_str[:500]}\n"
+                        f"[系统提醒] 你已生成了内容但还没有保存到文件。"
+                        f"请立即调用WriteFile工具将内容写入用户要求的路径。"
+                    )
+                    print(f"  💭 {_step_label} 已生成内容,继续执行文件保存...")
                 else:
-                    final_response = str(llm_output)
-                break
+                    # 真的是最终回复
+                    final_response = output_str
+                    break
         else:
             final_response = f"(达到最大步数 {max_steps})\n最后的观察:\n" + (
                 observations[-1] if observations else "无"