置信度门控路由
把置信度用作第二个维度。答案告诉你“是什么”;置信度告诉你“是否行动”。
TypeSafe 最强大的特性之一是置信度。通过有意识地设计基于置信度的决策门控方式,你可以构建既可靠又安全的系统。
示例:语音银行指令
设想你正在构建一个语音银行界面,让用户能够以语音方式与自己的账户交互。虽然你总是希望解读用户意图时具有足够的置信度,但有些操作比其他操作风险更高,因而需要更高的置信度阈值。
%%{init: {"fontFamily": "Inter, sans-serif", "flowchart": {"rankSpacing": 35, "wrappingWidth": 300, "subGraphTitleMargin": {"top": 12, "bottom": 36}}}}%%
flowchart LR
command["voice banking command"]
subgraph req["TypeSafe evaluates<br/>the question"]
intent["<b>Choice:</b> intent"]
end
command -- "one request<br/>command + intent<br/>question" --> req
req -- "one response<br/>intent answer +<br/>confidence" --> gate{"<b>confidence high enough?</b><br/>your code"}
gate -- "below 0.6<br/>or other intent" --> human["send to a support agent"]
gate -- "check_balance<br/>at least 0.6" --> balance["show the balance"]
gate -- "approve_transfer<br/>0.6 to 0.85" --> confirm["ask the user to confirm"]
gate -- "approve_transfer<br/>above 0.85" --> approve["approve the transfer"]%%{init: {"fontFamily": "Inter, sans-serif", "flowchart": {"rankSpacing": 35, "wrappingWidth": 300, "subGraphTitleMargin": {"top": 12, "bottom": 36}}}}%%
flowchart LR
command["voice banking command"]
subgraph req["TypeSafe evaluates<br/>the question"]
intent["<b>Choice:</b> intent"]
end
command -- "one request<br/>command + intent<br/>question" --> req
req -- "one response<br/>intent answer +<br/>confidence" --> gate{"<b>confidence high enough?</b><br/>your code"}
gate -- "below 0.6<br/>or other intent" --> human["send to a support agent"]
gate -- "check_balance<br/>at least 0.6" --> balance["show the balance"]
gate -- "approve_transfer<br/>0.6 to 0.85" --> confirm["ask the user to confirm"]
gate -- "approve_transfer<br/>above 0.85" --> approve["approve the transfer"]
第 1 步:确定用户的意图
questions
{
"questions": {
"intent": {
"type": "choice",
"instructions": "What action is the user requesting?",
"criteria": {
"check_balance": "Check the balance of an account",
"approve_transfer": "Approve the pending transfer request",
"other": "Something else"
}
}
}
}第 2 步:置信度门控路由
python
action = response.answers["intent"]
# Below 0.6 confidence on any action, route to a human
if action.confidence < 0.6:
route_to_support_agent(account_id)
elif action.choice == "check_balance":
# Low stakes. 0.6 confidence is sufficient.
show_balance(account_id)
elif action.choice == "approve_transfer":
if action.confidence > 0.85:
# High stakes, but high confidence. Safe to act automatically.
approve_transfer(account_id)
else:
# High stakes, moderate confidence. Verify intent first.
ask_user_to_confirm("Just to confirm: you would like to approve this transfer, is that correct?")
else:
route_to_support_agent(account_id)0.6 的下限会捕捉模型真正不确定的一切情况。在这个下限之上,每种操作类型都有各自的阈值,其依据是:如果分类错误就执行操作,后果有多严重。以 0.6 的置信度查询余额没有问题,因为最坏的情况不过是用户需要再听一遍余额播报。但批准转账需要非常高的置信度(>0.85),否则系统应当请用户确认。
关于如何在自己的系统中思考置信度的更多细节,参见置信度。
🔬 交互实验室
改一改 state 或问题再运行,观察概率与置信度如何变化。默认运行本地演示(离线可用、
非真实模型输出,仅演示答案形状);填入 API Key 可尝试直连真实 API(官方暂未开放跨域时会给出一键复制的本地调用代码);也可一键到官方 Playground 在线试跑。
配套讲解见本页上方内容与「深入学习」素材。
Key 仅保存在本浏览器(localStorage),不会上传到本站。留空 = 本地演示。
📓 内嵌教程 · 第 3 章 · 架构模式实验笔记本(四种模式全套可运行实验)
像运行 notebook 一样逐步学习:点「下一步」逐格展开——代码格会先出现再"运行"出结果。内容来自教程仓库配套笔记本。
📖 想看带真实 API Key 的完整运行与源文件:
第 3 章 · 架构模式实验笔记本(四种模式全套可运行实验)(GitHub)↗