LLM 输出护栏
检查 LLM 或智能体即将发送的内容——是否有来源依据、有无数据泄露、是否安全且符合策略——并在发出之前拦截、回退或升级。
目标:在你的 LLM 或智能体之后加一道类型化的检查。在回答到达用户之前——或在智能体的消息、电子邮件或工具结果发出之前——Dcision 会检查它是否有来源依据、没有泄露任何内容并遵循你的策略。不安全的回答会被拦截,缺乏依据的回答走 fallback,其余的照常发出。
Dcision 可以守在模型的两侧:输入护栏检查进入模型的内容;本模板检查模型输出的内容。
创建决策
模板 → LLM 输出护栏 → 创建决策:
| 部分 | 内容 |
|---|---|
| 状态 | user_message(字符串,必填)、output(字符串,必填)、sources(字符串) |
| 上下文 | 你的助手可以说什么、不可以说什么——按你的策略修改 |
grounded | probability——每一条陈述都有 sources 支持;最低置信度 0.5 |
data_leak | probability——他人的个人数据、凭据或内部指令 |
policy | choice——compliant、promise、advice、off_topic、other |
harmful | probability——有害、带有仇恨、骚扰性质或不安全 |
| 策略 | data_leak ≥ 0.5 → block;harmful ≥ 0.5 → block;grounded < 0.5 → fallback;policy ≠ compliant → escalate;fallback 行动为 escalate |
调用
发送用户的消息、模型的回答草稿以及它所依据的来源(检索到的文档、工具结果):
curl -X POST https://api.dcision.io/v1/decisions/llm-output-guardrail \
-H "Authorization: Bearer $DCISION_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"state": {
"user_message": "When will my order 1042 arrive?",
"output": "Your order 1042 ships tomorrow and you get a 20% discount for the delay.",
"sources": "Order 1042: shipped today, carrier estimate 2 business days. Discounts need approval by a manager."
}
}'{
"schema": "llm-output-guardrail",
"result": { "grounded": 0.08, "data_leak": 0.01, "policy": "promise", "harmful": 0.01 },
"action": "fallback",
"action_reason": { "type": "policy", "rule": 2 }
}草稿编造了发货日期,还承诺了折扣:grounded 很低,因此这条回答永远不会到达客户。
据此行动
const draft = await llm.answer(userMessage, sources);
const check = await dcisionDecide("llm-output-guardrail", { user_message: userMessage, output: draft, sources });
if (check.action === "continue") return draft;
if (check.action === "fallback") return llm.answer(userMessage, sources, { strict: true }); // regenerate, grounded only
if (check.action === "escalate") return handToAgent(userMessage, draft); // a person reviews it
return "Sorry, I can't share that."; // block对于智能体,以同样的方式在每条外发消息或每个副作用(电子邮件、工单回复、退款)之前调用它。
调优
- 把你的策略写进
context:助手可以承诺什么,哪些话题是禁区。 - 为
policy添加选项来表达你自己的规则(竞争对手定价、路线图日期);每个不是compliant的回答都会升级。 - 只要有来源就发送:
grounded的可靠程度取决于它能核对的证据。 - 与输入护栏搭配使用,让模型在输入和输出两端都受到检查。