推测式扇出
在一次调用中提出流程可能需要的所有问题——Jev 会并行回答——再由 AND 规则和你的代码决定哪些回答起作用。基于“工单分诊”模板构建。
在一次调用中提出流程可能需要的所有问题——Jev 会并行回答——再由规则和你的代码决定哪些回答起作用。与其先询问工单的类别,再在后续调用中询问缺陷严重程度,不如同时询问二者:如果工单不是缺陷报告,忽略严重程度即可。
好处:成本和速度。TypeSafe 的指南:推测式扇出。
为什么有效
- 一次往返。Jev 只读取一次 state,并独立、并行地针对它评估每个问题,因此额外的问题几乎不增加延迟。在 TypeSafe 的并行问题示例中,把 13 个问题合并到一次调用中,比每个问题调用一次便宜约 12 倍、快约 10 倍,而回答相同。
- 只计费一次决策。Dcision 按成功的调用计费,而不是按问题计费:一个有五个问题的决策在你的套餐中算一次决策。引擎成本只会随额外问题的输入 token 增长——参见
metrics.input_tokens。 - 没有隐藏的耦合。每个回答都是独立计算的;一个问题的回答永远不会成为另一个问题的上下文。之后由你的规则把它们组合起来。
何时使用
后续步骤依赖于先前回答的分诊场景——类别 → 缺陷严重程度 → 退款——以及任何原本需要链式调用(“如果 A,再问 B”)的流程。一个决策最多可容纳 64 个问题,但需在 Jev 的 token 预算之内:state 加最长问题约 32,000 个 token,state 加所有问题约 64,000 个 token。
如何对应到 Dcision
| 在 TypeSafe 的指南中 | 在 Dcision 中 |
|---|---|
| 所有问题放在一个请求中 | 所有问题放在一个决策中——一次 POST /v1/decisions/{slug} |
| 推测性问题 | 只在某些分支中起作用的问题,例如 bug_severity |
在你的代码中写 if category == "bug_report" and severity > … | 一条带 and 条件的规则:category = bug_report AND bug_severity ≥ 4 → escalate |
frustration.score > 1.5(等级从 0 开始计) | 一条基于加权等级的规则:frustration(加权级别)≥ 3.5 → escalate(等级从 1 开始计) |
规则承载必须一致且可审计的路由;你的代码仍然可以读取 result 中的每个回答。
构建
在应用中
打开模板,找到工单分诊(扇出)——在所有模板下,或点击推测式扇出卡片上的试试——并选择它。在命名中,将决策命名为 Ticket Triage:slug(即你的端点)由名称生成,变为 ticket-triage。(使用模板的完整名称会得到 support-ticket-triage-fan-out;在首次部署之前,你也可以在编辑器中修改 slug。)点击创建决策。
State 是一个 JSON 对象,包含 subject(string)和 body(string,必填)。
问题一次性涵盖整棵决策树:
| 问题 | 类型 | 回答 | 何时起作用 |
|---|---|---|---|
category | choice | bug_report、billing、feature_request、how_to、other | 始终 |
bug_severity | score | cosmetic、minor、major、critical | 缺陷报告(推测性) |
has_reproducible_steps | probability | 工单包含重现问题的步骤 | 缺陷报告(推测性) |
refund_requested | probability | 客户明确要求退款或抵扣 | 账单(推测性) |
frustration | score | calm、annoyed、frustrated、very angry | 始终 |
critical 等级是一个结构化等级——{ "label": "critical", "rubric": "outage, data loss, security issue or money at risk" }——因此引擎读取的是评分细则,而 API 返回的是标签 critical。
在策略中,每条规则读作 IF 条件 [AND 条件…] THEN 行动:
- IF
category· 回答 · = ·bug_report,然后 + ANDbug_severity· 回答 · ≥ · 4 · critical——THENescalate。 - IF
refund_requested· 回答 · ≥ · 0.8——THENescalate。 - IF
frustration· 加权级别 · ≥ · 3.5——THENescalate。
fallback 行动保持为 escalate,因此不属于任何类别(other)的工单也会交给人工。
在 Playground 中运行示例工单,检查回答和行动,然后部署 v1。编辑器的模式面板现在会把推测式扇出和意图路由标记为已使用。
决策 schema
编辑器的决策 schema 视图所显示的问题和规则(保留的 other 选项会自动添加到 category):
{
"questions": [
{
"key": "category",
"type": "choice",
"instructions": "What kind of support ticket is this?",
"options": [
{ "value": "bug_report", "description": "something in the product is broken or behaves incorrectly" },
{ "value": "billing", "description": "charges, invoices, refunds, plans, payment methods" },
{ "value": "feature_request", "description": "asks for something the product doesn't do yet" },
{ "value": "how_to", "description": "asks how to do something that already works" }
]
},
{
"key": "bug_severity",
"type": "score",
"instructions": "If this describes a bug, how severe is it for the customer?",
"scale": ["cosmetic", "minor", "major", { "label": "critical", "rubric": "outage, data loss, security issue or money at risk" }]
},
{ "key": "has_reproducible_steps", "type": "probability", "instructions": "Does the ticket include steps to reproduce the problem?" },
{ "key": "refund_requested", "type": "probability", "instructions": "Does the customer explicitly ask for a refund or credit?" },
{ "key": "frustration", "type": "score", "instructions": "How frustrated is the customer?", "scale": ["calm", "annoyed", "frustrated", "very angry"] }
],
"policies": [
{
"field": "category",
"on": "output",
"operator": "eq",
"value": "bug_report",
"and": [{ "field": "bug_severity", "on": "output", "operator": "gte", "value": 4 }],
"action": "escalate"
},
{ "field": "refund_requested", "on": "output", "operator": "gte", "value": 0.8, "action": "escalate" },
{ "field": "frustration", "on": "score", "operator": "gte", "value": 3.5, "action": "escalate" }
],
"settings": { "fallbackAction": "escalate" }
}使用 CLI,dcision init triage.json --template ticket-triage 会把完整的 schema 写入文件。
调用它
curl -X POST https://api.dcision.io/v1/decisions/ticket-triage \
-H "Authorization: Bearer $DCISION_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: ticket-90412" \
-d '{
"state": {
"subject": "Payouts failing",
"body": "Our payouts have been failing for 3 days and customers are complaining. Steps: open Payouts, click Retry, error 500. Please fix ASAP."
}
}'{
"decision_id": "dec_Tq4Wm8Lz2Xv6Rk1Np9Cs",
"execution_id": "exec_Jd7Hs2Qa5Wn8Lx3Kv1Mz",
"schema": "ticket-triage",
"version": 1,
"result": {
"category": "bug_report",
"bug_severity": "critical",
"has_reproducible_steps": 0.91,
"refund_requested": 0.04,
"frustration": "frustrated"
},
"confidence": {
"category": 0.9125,
"bug_severity": 0.56,
"has_reproducible_steps": 0.82,
"refund_requested": 0.92,
"frustration": 0.59
},
"scores": { "bug_severity": 3.56, "frustration": 2.97 },
"action": "escalate",
"action_reason": { "type": "policy", "rule": 0 },
"metrics": {
"latency_ms": 368,
"engine": "jev",
"model": "jev-1.13.0",
"estimated_cost_usd": 0.00002184,
"input_tokens": 520,
"output_tokens": 58
}
}一次调用得到五个回答。规则 0 匹配:该工单是 bug_report,并且其最可能的严重程度是 critical(等级 4)。scores 保存每个 score 问题的加权等级——bug_severity 为 3.56,介于 major 和 critical 之间。
据此行动
规则决定了是否需要人工立即查看;你的代码只在推测性回答起作用的分支中读取它们:
const decision = await response.json();
const { result } = decision;
if (decision.action === "escalate") {
return onCall.page(ticket, { reason: decision.action_reason, executionId: decision.execution_id });
}
switch (result.category) {
case "bug_report":
return result.has_reproducible_steps >= 0.6
? engineering.triage(ticket, { severity: result.bug_severity })
: bugBacklog.add(ticket, { severity: result.bug_severity });
case "billing":
return billing.assign(ticket, { refundLikely: result.refund_requested >= 0.7 });
case "feature_request":
return roadmap.log(ticket);
case "how_to":
return autoReply.withDocs(ticket);
}frustration 在每个分支中都有用——例如,当 decision.scores.frustration 为 3 或更高时提高优先级,即使它低于升级阈值。
调优
- 放心添加推测性问题。每个问题只消耗其输入 token,而不是一次往返。请保持在 64 个问题和 token 预算之内。
- 让每个问题保持原子化。每个问题只做一个判断,并按字面理解——参见编写好问题。
- 用
and把关,而不是用更长的指令。“如果这描述的是缺陷,严重程度如何……”仍然是一个简单的问题;由规则决定它的回答何时生效。 - 使用
on: "score"在等级之间设阈值:frustration ≥ 3.5会在权重落在frustration和very angry之间时触发,而不只是在very angry胜出时触发。 - 按从具体到一般的顺序排列规则:首个匹配生效。
- 只发送工单本身。冗长的对话串和无关字段会降低准确性并消耗 token 预算——请在你的代码中把它们过滤掉。