置信度是 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"]
第 1 步:判断用户意图
questions
json{
"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),否则系统应请用户确认。
关于如何在系统中思考置信度,更多细节请参阅置信度。