为什么你的 AI 不断忽视安全约束(以及我们如何通过工程化‘Intent’来解决)

发布: (2026年2月25日 GMT+8 16:31)
4 分钟阅读
原文: Dev.to

Source: Dev.to

如果你花过一点时间与大型语言模型(LLM)交互,你大概已经遇到过这种令人沮丧的情形:你让 AI 将“安全、清晰和简洁”放在首位。
当模型必须在让句子更清晰和让它更安全之间做选择时,标准提示会把这些目标视为同等优先级——实际上相当于抛硬币决定。

目前我们向 LLM 传递目标的方式是平铺的、逗号分隔的列表。AI 会把“安全”和“简洁”当作同等的优先级,没有内建机制告诉模型,医学安全约束远远高于对简练文风的请求。这种你想表达的意图与模型实际接收的内容之间的差距,是可靠 AI 的一大难题。

我们最近通过构建一种叫做 Intent Engineering(意图工程)的系统来解决了这个问题,核心依赖 Value Hierarchies(价值层级)。下面将详细说明它的工作原理、为何重要,以及如何为你的 AI 提供机器可读的“良知”。

Source:

问题:AI 目标是无序的

目标没有排名。例如:

optimize(goals="clarity, safety")

对两个目标一视同仁。

数据结构

from enum import Enum
from typing import List, Optional
from pydantic import BaseModel

class PriorityLabel(str, Enum):
    NON_NEGOTIABLE = "NON_NEGOTIABLE"  # Forces the smartest routing tier
    HIGH           = "HIGH"            # Forces at least a hybrid tier
    MEDIUM         = "MEDIUM"          # Prompt‑level guidance only
    LOW            = "LOW"             # Prompt‑level guidance only

class HierarchyEntry(BaseModel):
    goal: str
    label: PriorityLabel
    description: Optional[str] = None

class ValueHierarchy(BaseModel):
    name: Optional[str] = None
    entries: List[HierarchyEntry]
    conflict_rule: Optional[str] = None

通过这种方式组织数据,我们可以在两个关键层面将这些规则注入 AI 的行为中。

Level 1: 更改 AI 的“大脑”(Prompt Injection)

...existing system prompt...

INTENT ENGINEERING DIRECTIVES (user‑defined — enforce strictly):
When optimization goals conflict, resolve in this order:
  1. [NON_NEGOTIABLE] safety: Always prioritise safety
  2. [HIGH] clarity
  3. [MEDIUM] conciseness

Conflict resolution: Safety first, always.

技术说明: 我们使用 entry.label.value 是因为 Python 3.11+ 更改了字符串子类化枚举的工作方式。这确保提示获得确切的字符串 "NON_NEGOTIABLE"

第2级:“守门员”(路由层级)

我们构建了一个 路由层级底线。如果你将目标标记为 NON_NEGOTIABLE,系统会在数学上阻止该请求被路由到更低层级的模型。

# Calculate the base score for the prompt 
score = await self._calculate_routing_score(prompt, context, ...)

# The Floor: Only fires when a hierarchy is active:
if value_hierarchy and value_hierarchy.entries:
    has_non_negotiable = any(
        e.label == PriorityLabel.NON_NEGOTIABLE for e in value_hierarchy.entries
    )
    has_high = any(
        e.label == PriorityLabel.HIGH for e in value_hierarchy.entries
    )

    # Force the request to a smarter model tier based on priority
    if has_non_negotiable:
        score["final_score"] = max(score.get("final_score", 0.0), 0.72)  # Guaranteed LLM
    elif has_high:
        score["final_score"] = max(score.get("final_score", 0.0), 0.45)  # Guaranteed Hybrid
def _hierarchy_fingerprint(value_hierarchy) -> str:
    if not value_hierarchy or not value_hierarchy.entries:
        return ""   # empty string → same cache key as usual
    return hashlib.md5(
        json.dumps(
            [{"goal": e.goal, "label": str(e.label)} for e in value_hierarchy.entries],
            sort_keys=True
        ).encode()
    ).hexdigest()[:8]

实践应用 (MCP 集成)

{
  "tool": "define_value_hierarchy",
  "arguments": {
    "name": "Medical Safety Stack",
    "entries": [
      { "goal": "safety", "label": "NON_NEGOTIABLE", "description": "Always prioritise patient safety" },
      { "goal": "clarity", "label": "HIGH" },
      { "goal": "conciseness", "label": "MEDIUM" }
    ],
    "conflict_rule": "Safety first, always."
  }
}

TL;DR

如果你想尝试这种方法,请安装 Prompt Optimizer:

npm install -g mcp-prompt-optimizer

欢迎分享你在自己的流水线中如何处理冲突约束!

0 浏览
Back to Blog

相关文章

阅读更多 »