工具调用把关

在智能体运行工具之前,使用工具调用把关模板决定是运行、请用户确认还是拒绝——有风险的操作需要更高的置信度。

目标:在安全时让你的智能体自主行动,而当一次错误的调用会造成金钱、数据或信任的损失时,先询问用户。

创建决策

模板 → 工具调用把关(置信度门控) → 创建决策:

部分内容
Stateuser_request、tool、arguments(string,必填)
verdictchoice——run、ask_user、reject、other
riskscore——read-only、low、medium、high
matches_requestprobability——这次调用是否恰好执行了用户要求的操作?
策略reject → block;ask_user → escalate;run 且 risk ≥ 3 且置信度 < 0.9 → escalate;fallback 为 escalate
目的地run_tool——在 continue 时调用函数 runTool;confirm_with_user——在 escalate 时返回带 Yes / No 按钮的回复;在 block 时返回 rejected_reply

调用

curl -X POST https://api.dcision.io/v1/decisions/tool-call-gating \
  -H "Authorization: Bearer $DCISION_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "state": {
      "user_request": "Refund the duplicate charge on my last invoice.",
      "tool": "issue_refund",
      "arguments": "{\"invoice_id\": \"inv_2291\", \"amount\": 1200, \"currency\": \"BRL\"}"
    }
  }'
200 OK (abridged)
{
  "schema": "tool-call-gating",
  "result": { "verdict": "run", "risk": "high", "matches_request": 0.83 },
  "confidence": { "verdict": 0.81 },
  "action": "escalate",
  "destinations": [
    { "key": "confirm_with_user", "type": "reply", "status": "completed", "text": "Before I continue: should I run issue_refund with these details?", "buttons": ["Yes, go ahead", "No, cancel"] }
  ]
}

采取行动

const d = await dcisionDecide("tool-call-gating", { user_request, tool: call.name, arguments: JSON.stringify(call.args) });
if (d.action === "continue") return runTool(call.name, call.args);
const reply = d.destinations.find((x) => x.type === "reply");
return agent.say(reply.text, reply.buttons); // on "Yes", run the tool; the agent never runs it on its own

调优

  • 在 context 中描述你的工具——哪些是只读的,哪些会转移资金或联系他人。
  • 针对风险最高的工具收紧第三条规则:risk ≥ 4 且置信度 < 0.95。
  • Dcision 永远无法访问工具本身:function 目的地只说明你的代码应该调用哪个函数。

本页内容