意图路由
用一次快速调用对每个请求进行分类,并把它路由到能胜任的最便宜处理方——代码、专业 LLM 或人工。基于客户服务路由模板构建。
并非每个请求都需要同样的处理方。有些可以通过一次数据库查询来回答,有些需要带有领域上下文的 LLM,有些需要人工。把 Dcision 放在它们所有处理方之前,作为一个快速、便宜的分类器:先分类,再路由——只有当请求真正需要时,才为昂贵的处理方付费。
好处:成本和速度。TypeSafe 的指南:Intent routing。
为什么有效
- 选择题是一个封闭集合。回答始终是你的某个意图——从来不是需要解析的自由文本——并且每个选项都有一个概率。
- “以上都不是”也是一个回答。保留的
other选项让引擎可以表示某个请求不符合任何意图,而不是被迫选择错得最少的那个;随后 Dcision 返回 fallback 行动。 - 一次调用同时回答路由和风险。同时、并行地询问意图及其复杂度。
- 昂贵的部分最后运行。查询直接交给代码,不经过任何 LLM;专业模型只接收属于它们的请求,并带有各自的上下文。
何时使用
入站消息和工单、聊天和语音助手、选择第一个工具的智能体,以及任何请求所需处理方式各不相同的队列——在你为 LLM 调用付费之前。
如何对应到 Dcision
| 在 TypeSafe 的指南中 | 在 Dcision 中 |
|---|---|
| 用于意图的选择 | 一个选择题,外加保留的 other |
| 用于复杂度的评分 | 一个评分题;其加权级别(scores)可在规则中使用 |
if intent.confidence < 0.5: human | 意图问题上的 minConfidence,或针对其置信度的规则 |
complaint → 复杂或不确定时交给人工 | 对必须由人处理的情况执行 escalate 的规则 |
handle_order_status()、handle_with_llm(…) | 当行动为 continue 时,你的代码根据 result.intent 路由 |
构建
在应用中
打开模板,选择客户服务路由(意图路由)。将决策命名为 Customer Service Router,使其 slug 为 customer-service-router,然后点击创建决策。
State 为文本:客户的消息。
两个问题,在同一次调用中回答:
| 问题 | 类型 | 回答 |
|---|---|---|
intent | choice | order_status(通过数据库查询回答)、billing_question、technical_issue、complaint、other |
complexity | score | simple、moderate、complex |
在策略中,升级必须由人处理的情况:
- IF
intent· 回答 · = ·complaint——THENescalate。 - IF
intent· 回答 · = ·technical_issue,+ ANDcomplexity· 加权级别 · ≥ · 2.5——THENescalate。 - IF
complexity· 置信度 · < · 0.5——THENescalate:当决策无法判断一个请求有多难时,不要将其自动化。
fallback 行动为 escalate,因此 other 也会交给人工。
在 Playground 中测试真实的消息,然后点击部署 v1。
决策 schema
{
"stateSchema": { "kind": "text" },
"questions": [
{
"key": "intent",
"type": "choice",
"instructions": "What is the customer asking for?",
"options": [
{ "value": "order_status", "description": "where is my order, tracking, delivery date (answered by a database lookup)" },
{ "value": "billing_question", "description": "charges, invoices, refunds, payment methods" },
{ "value": "technical_issue", "description": "the product or app is not working as expected" },
{ "value": "complaint", "description": "dissatisfaction, wants to escalate or talk to a manager" }
]
},
{
"key": "complexity",
"type": "score",
"instructions": "How complex is it to resolve this request?",
"scale": ["simple", "moderate", "complex"]
}
],
"policies": [
{ "field": "intent", "on": "output", "operator": "eq", "value": "complaint", "action": "escalate" },
{
"field": "intent",
"on": "output",
"operator": "eq",
"value": "technical_issue",
"and": [{ "field": "complexity", "on": "score", "operator": "gte", "value": 2.5 }],
"action": "escalate"
},
{ "field": "complexity", "on": "confidence", "operator": "lt", "value": 0.5, "action": "escalate" }
],
"settings": { "fallbackAction": "escalate" }
}调用
curl -X POST https://api.dcision.io/v1/decisions/customer-service-router \
-H "Authorization: Bearer $DCISION_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "state": "My order #4821 was supposed to arrive yesterday. Can you tell me where it is?" }'{
"decision_id": "dec_Lr5Tw9Mc2Qx7Hb4Nz1Kg",
"execution_id": "exec_Fv1Wd6Kp3Zs8Qm2Tx7Lh",
"schema": "customer-service-router",
"version": 1,
"result": { "intent": "order_status", "complexity": "simple" },
"confidence": { "intent": 0.925, "complexity": 0.76 },
"scores": { "complexity": 1.16 },
"action": "continue",
"action_reason": { "type": "default" },
"metrics": {
"latency_ms": 233,
"engine": "jev",
"model": "jev-1.13.0",
"estimated_cost_usd": 0.00000966,
"input_tokens": 230,
"output_tokens": 21
}
}没有规则匹配:这是一个简单的订单状态问题,代码无需任何 LLM 就能回答。
采取行动
type Intent = "order_status" | "billing_question" | "technical_issue" | "complaint" | "other";
async function route(message: Message) {
const response = await fetch("https://api.dcision.io/v1/decisions/customer-service-router", {
method: "POST",
headers: { Authorization: `Bearer ${process.env.DCISION_API_KEY}`, "Content-Type": "application/json" },
body: JSON.stringify({ state: message.text }),
});
if (!response.ok) return humanQueue.add(message); // errors: a person decides
const decision = await response.json();
if (decision.action !== "continue") return humanQueue.add(message, { reason: decision.action_reason });
switch (decision.result.intent as Intent) {
case "order_status":
return orders.replyWithTracking(message); // deterministic code, no LLM
case "billing_question":
return llm.answer(message, BILLING_SPECIALIST);
case "technical_issue":
return llm.answer(message, SUPPORT_SPECIALIST); // complex ones were escalated by rule 1
default:
return humanQueue.add(message);
}
}调优
- 对意图本身设门槛。TypeSafe 的版本会把意图置信度低于 0.5 的所有请求交给人工:在
intent上设置"minConfidence": 0.5即可达到同样效果。 - 让边界明确。用每个意图涵盖什么、不涵盖什么来描述它——选项描述接受 JSON 评分细则,例如
{ "what": "…", "not_for": "…", "examples": ["…"] }。参见问题。 - 检查选项顺序。Jev 可能倾向于排在第一位的选项。在 Playground 中重新排列选项,检查回答是否保持不变。
- 当“以上都不是”对你有具体含义时,描述
other——“与订单、账单或应用无关”——这样离题的消息就会走 fallback 行动。 - 随着了解逐步扩充集合。一个选择题最多可容纳 254 个选项外加
other。在执行记录中关注other_option原因,找出你遗漏的意图。 - 用同样的方式路由工具。智能体路由模板把这一模式应用于 AI 智能体的第一个工具。