Răsfoiți Sursa

Merge pull request #295 from qingning7/main

fix: 修复模型break逻辑漏洞导致的 Action 解析错误
jjyaoao 5 luni în urmă
părinte
comite
7d405755b0

+ 5 - 2
code/chapter1/FirstAgentTest.ipynb

@@ -340,8 +340,11 @@
     "        # 解析行动\n",
     "        action_match = re.search(r\"Action: (.*)\", llm_output, re.DOTALL)\n",
     "        if not action_match:\n",
-    "            print(\"❌ 解析错误:模型输出中未找到 Action。\")\n",
-    "            break\n",
+    "            observation = \"错误: 未能解析到 Action 字段。请确保你的回复严格遵循 'Thought: ... Action: ...' 的格式。\"\n",
+    "            observation_str = f\"Observation: {observation}\"\n",
+    "            print(f\"{observation_str}\\n\" + \"=\"*40)\n",
+    "            assistant.prompt_history.append(observation_str)\n",
+    "            continue\n",
     "            \n",
     "        action_str = action_match.group(1).strip()\n",
     "        tool_name, kwargs = parse_action(action_str)\n",

+ 5 - 2
code/chapter1/FirstAgentTest.py

@@ -174,8 +174,11 @@ for i in range(5): # 设置最大循环次数
     # 3.3. 解析并执行行动
     action_match = re.search(r"Action: (.*)", llm_output, re.DOTALL)
     if not action_match:
-        print("解析错误:模型输出中未找到 Action。")
-        break
+        observation = "错误: 未能解析到 Action 字段。请确保你的回复严格遵循 'Thought: ... Action: ...' 的格式。"
+        observation_str = f"Observation: {observation}"
+        print(f"{observation_str}\n" + "="*40)
+        prompt_history.append(observation_str)
+        continue
     action_str = action_match.group(1).strip()
 
     if action_str.startswith("Finish"):

+ 5 - 2
docs/chapter1/Chapter1-Introduction-to-Agents.md

@@ -431,8 +431,11 @@ for i in range(5): # Set maximum number of loops
     # 3.3. Parse and execute action
     action_match = re.search(r"Action: (.*)", llm_output, re.DOTALL)
     if not action_match:
-        print("Parse error: Action not found in model output.")
-        break
+        observation = "Error: No action found. Please explicitly use Action: finish(...) or other actions."
+        observation_str = f"Observation: {observation}"
+        print(f"{observation_str}\n" + "="*40)
+        prompt_history.append(observation_str)
+        continue
     action_str = action_match.group(1).strip()
 
     if action_str.startswith("Finish"):

+ 5 - 2
docs/chapter1/第一章 初识智能体.md

@@ -436,8 +436,11 @@ for i in range(5): # 设置最大循环次数
     # 3.3. 解析并执行行动
     action_match = re.search(r"Action: (.*)", llm_output, re.DOTALL)
     if not action_match:
-        print("解析错误:模型输出中未找到 Action。")
-        break
+        observation = "错误: 未能解析到 Action 字段。请确保你的回复严格遵循 'Thought: ... Action: ...' 的格式。"
+        observation_str = f"Observation: {observation}"
+        print(f"{observation_str}\n" + "="*40)
+        prompt_history.append(observation_str)
+        continue
     action_str = action_match.group(1).strip()
 
     if action_str.startswith("Finish"):