智能体路由

使用“智能体路由”模板,让 AI 智能体选择它的第一个工具,并判断一项任务是否真的需要大语言模型。

目标: 在智能体花费一次大模型调用进行规划之前,以低成本决定它应该先使用哪个工具——只把需要多步推理的任务发送给 LLM。

创建决策

模板 → 智能体路由 → 创建决策:

部分内容
状态task(字符串,必填)——用户向智能体提出的请求
selected_toolchoice——search(需要来自网络的最新信息)、database(需要我们自己记录中的数据)、calculator(需要计算)、none(可以直接回答)、other
needs_reasoningprobability——这项任务是否需要大语言模型的多步推理?
策略1. needs_reasoning ≥ 0.8 → fallback

调用

curl -X POST https://api.dcision.io/v1/decisions/agent-routing \
  -H "Authorization: Bearer $DCISION_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "state": { "task": "How many orders did we ship to Germany last week?" } }'
200 OK
{
  "decision_id": "dec_Ka9Wd4Tm1Xq7Rz3Ln8Bv",
  "execution_id": "exec_Pq2Zr8Hv5Nc1Jt6Mw3Ds",
  "schema": "agent-routing",
  "version": 1,
  "result": { "selected_tool": "database", "needs_reasoning": 0.31 },
  "confidence": { "selected_tool": 0.8625, "needs_reasoning": 0.38 },
  "action": "continue",
  "action_reason": { "type": "default" },
  "metrics": { "latency_ms": 301, "engine": "jev", "model": "jev-1.13.0", "estimated_cost_usd": 0.00000882, "input_tokens": 210, "output_tokens": 22 }
}

这是在你自己的数据中进行查询,几乎不需要推理:直接调用 database 工具即可。而像“Compare our churn by plan over the last three quarters and suggest pricing changes”(按套餐比较过去三个季度的流失率并提出定价调整建议)这样的任务会返回较高的 needs_reasoning,规则 0 会返回 fallback。

据此行动

type Decision = {
  action: "continue" | "block" | "escalate" | "fallback";
  result: { selected_tool: "search" | "database" | "calculator" | "none" | "other"; needs_reasoning: number };
};

async function handle(task: string) {
  const response = await fetch("https://api.dcision.io/v1/decisions/agent-routing", {
    method: "POST",
    headers: { Authorization: `Bearer ${process.env.DCISION_API_KEY}`, "Content-Type": "application/json" },
    body: JSON.stringify({ state: { task } }),
  });
  if (!response.ok) return planner.run(task); // on errors, fall back to the full agent

  const decision = (await response.json()) as Decision;
  if (decision.action !== "continue") return planner.run(task); // fallback or escalate: full LLM planning

  switch (decision.result.selected_tool) {
    case "database":
      return tools.database(task);
    case "search":
      return tools.search(task);
    case "calculator":
      return tools.calculator(task);
    case "none":
      return llm.answer(task); // a direct, single-step answer
    default:
      return planner.run(task); // "other": no tool fits
  }
}

selected_tool 为 other 且没有匹配的策略时,会返回 fallback 行动(默认为 escalate)——由上面的 action !== "continue" 分支处理。

调优

  • 按每个工具能回答什么来描述它,并写明排除项:“我们的订单、客户和发票——不包括产品文档”。像 { "what": "…", "not_for": "…", "examples": ["…"] } 这样的 JSON 描述能让边界更加明确。
  • 把你的工具添加为选项——最多 254 个,外加 other——并保留 none,用于智能体可以直接回答的任务。Jev 可能偏向第一个选项:在 Playground 中调整工具的顺序,检查回答是否保持不变。
  • 调整阈值:如果你的工具经常在复杂任务上失败,把 needs_reasoning 规则降低到 0.7;如果想节省 LLM 调用,就提高它。
  • 传入对话内容:在状态中添加 history 或 user_role 等字段——未声明的字段会被转发给引擎。只发送最近几轮对话,而不是完整历史:无关的上下文会降低准确率。
  • 同时选择参数。 当某个工具接受取值有限的参数(地区、报告类型)时,在同一个决策中为每个参数添加一个 choice 问题——一次调用即可同时回答工具及其参数。这就是把意图路由模式应用到工具上。

本页内容