Browse Source

Merge pull request #952 from dajiaohuang/fix-agent-tool-error-reporting

[fix] train_agent: 区分「未知工具」与「工具执行失败」,不再把执行异常报成 tool not found
jingyaogong 2 ngày trước cách đây
mục cha
commit
5bd54f38ca
1 tập tin đã thay đổi với 4 bổ sung và 4 xóa
  1. 4 4
      trainer/train_agent.py

+ 4 - 4
trainer/train_agent.py

@@ -85,11 +85,11 @@ def parse_tool_calls(text):
 
 
 def execute_tool(name, args):
 def execute_tool(name, args):
     fn = MOCK_RESULTS.get(name)
     fn = MOCK_RESULTS.get(name)
-    if not fn: return None
+    if not fn: return {"error": f"未知工具: {name}"}
     try:
     try:
         return fn(args)
         return fn(args)
-    except Exception:
-        return None
+    except Exception as e:
+        return {"error": f"工具执行失败: {str(e)[:80]}"}
 
 
 # ======== 多轮 Rollout ========
 # ======== 多轮 Rollout ========
 def rollout_single(rollout_engine, tokenizer, messages, tools, max_turns=3, max_new_tokens=256, thinking_ratio=0.5, device="cuda"):
 def rollout_single(rollout_engine, tokenizer, messages, tools, max_turns=3, max_new_tokens=256, thinking_ratio=0.5, device="cuda"):
@@ -137,7 +137,7 @@ def rollout_single(rollout_engine, tokenizer, messages, tools, max_turns=3, max_
                 except: raw = {}
                 except: raw = {}
             if not isinstance(raw, dict): raw = {}
             if not isinstance(raw, dict): raw = {}
             result = execute_tool(name, raw)
             result = execute_tool(name, raw)
-            result_str = (json.dumps(result, ensure_ascii=False) if result else '{"error": "tool not found"}')[:2048]  # 防止天文数字撑爆tokenizer
+            result_str = json.dumps(result, ensure_ascii=False)[:2048]  # 防止天文数字撑爆tokenizer
             messages.append({"role": "tool", "content": result_str})
             messages.append({"role": "tool", "content": result_str})
 
 
         marker = f"<|agent_observation_{id(messages)}_{len(response_ids)}|>"
         marker = f"<|agent_observation_{id(messages)}_{len(response_ids)}|>"