Prompt 约束技巧

2026-08-30 浏览 (1)

Prompt 约束技巧

如何通过 Prompt 有效约束大模型行为

核心原理

大模型的行为模式

大模型本质: 概率预测下一个 token

输入: "Rc cannot be sent"
模型思考: P("用 Arc") > P("分析领域约束后...")

默认行为: 选择概率最高的路径 (通常是最短、最常见的回答)

问题: 最高概率 ≠ 最佳答案

约束的作用

约束 = 改变概率分布

无约束: P("用 Arc") = 0.7, P("分析后回答") = 0.3
有约束: P("用 Arc") = 0.2, P("分析后回答") = 0.8

约束词提高了"遵循指令"路径的概率

约束词层级

强度等级

等级约束词效果使用场景
L5 最强CRITICAL, MUST, NEVER, MANDATORY几乎 100% 遵循核心规则,不可违反
L4 强IMPORTANT, REQUIRED, ALWAYS90%+ 遵循重要规则
L3 中should, recommended, prefer70%+ 遵循建议性规则
L2 弱can, may, consider50% 遵循可选项
L1 最弱optionally, if needed30% 遵循边缘情况

使用示例

# L5: 核心规则
CRITICAL: You MUST load both L1 and L3 skills.
NEVER skip the domain constraint analysis.
This is MANDATORY and NON-NEGOTIABLE.

# L4: 重要规则
IMPORTANT: Always include the reasoning chain.
You are REQUIRED to reference domain rules.

# L3: 建议
You should trace through all layers.
It is recommended to provide code examples.

# L2: 可选
You can include additional context.
You may reference related skills.

# L1: 边缘
Optionally, mention performance implications.

有效约束模式

1. CRITICAL + MUST 组合

CRITICAL: You MUST follow the meta-cognition framework.

效果:
- CRITICAL 提升注意力
- MUST 强制执行
- 组合使用 > 单独使用

2. 正反对比

DO:
- Load both L1 and L3 skills
- Output reasoning chain
- Reference domain constraints

DON'T:
- Skip domain analysis
- Output only "use Arc"
- Ignore the context

效果: 明确边界,减少歧义

3. 示例对比

CORRECT Response:

Reasoning Chain

+-- Layer 1: Send/Sync Error +-- Layer 3: Web Domain constraint +-- Layer 2: Design decision


WRONG Response:

Use Arc instead of Rc.


效果: 具体示例 > 抽象描述

4. 条件约束

**IF** domain keywords are present,
**THEN** you MUST load BOTH L1 and L3 skills.

**IF** error code detected,
**THEN** start from Layer 1 and trace UP.

效果: 清晰的触发条件

5. 步骤强制

## STEP 1: IDENTIFY (MANDATORY)
...

## STEP 2: LOAD SKILLS (MANDATORY)
...

## STEP 3: OUTPUT (MANDATORY)
...

效果: 步骤编号 + MANDATORY 确保顺序执行

格式化增强

大写强调

# 效果递增
should do this          → 弱
SHOULD do this          → 中
You SHOULD do this      → 中强
You MUST do this        → 强
CRITICAL: You MUST...   → 最强

结构化布局

# 好 - 清晰的视觉层级
## STEP 1: IDENTIFY LAYER

### Layer 1 Signals:
- Error codes: E0382, E0597
- Keywords: borrow, lifetime

### Layer 3 Signals:
- Domain keywords: Web API, HTTP

---

# 差 - 无结构
First identify the layer. Look for error codes like E0382
or keywords like borrow. Also check for domain keywords...

表格约束

# 好 - 表格清晰
| Keywords | Action |
|----------|--------|
| Web API, HTTP | Load domain-web |
| payment | Load domain-fintech |

# 差 - 文字描述
If you see Web API or HTTP, load domain-web.
If you see payment, load domain-fintech.

常用约束词汇表

强制执行类

含义示例
MUST必须You MUST include...
NEVER绝不NEVER skip...
ALWAYS总是ALWAYS check...
REQUIRED必需This is REQUIRED
MANDATORY强制MANDATORY step
NON-NEGOTIABLE不可商量This is NON-NEGOTIABLE

禁止类

含义示例
NEVER绝不NEVER output only...
DO NOT不要DO NOT skip...
AVOID避免AVOID generic answers
FORBIDDEN禁止This is FORBIDDEN
NOT ACCEPTABLE不可接受Partial compliance is NOT ACCEPTABLE

条件类

含义示例
IF...THEN如果...则IF domain detected, THEN load...
WHENWHEN error code present...
UNLESS除非UNLESS explicitly asked...
ONLY IF仅当ONLY IF user requests...

强调类

含义示例
CRITICAL关键CRITICAL: This is essential
IMPORTANT重要IMPORTANT: Note that...
NOTE注意NOTE: This affects...
WARNING警告WARNING: Do not...

rust-skills 中的实践

Hook 脚本约束

=== MANDATORY: META-COGNITION ROUTING ===

CRITICAL: You MUST follow the COMPLETE meta-cognition framework.
Partial compliance (only loading L1 skill) is NOT ACCEPTABLE.

# 使用技巧:
# 1. 标题用 === 包围,视觉突出
# 2. CRITICAL + MUST 组合
# 3. 明确说明什么是不可接受的

Skill Description 约束

description: "CRITICAL: Use for ALL Rust questions. Triggers on: ..."

# 使用技巧:
# 1. 以 CRITICAL 开头
# 2. 明确触发条件
# 3. 关键词用 Triggers on: 标记

输出格式约束

## STEP 3: MANDATORY OUTPUT FORMAT

Your response MUST include ALL of these sections:

### Reasoning Chain

+-- Layer 1: [specific error] | ^ +-- Layer 3: [domain constraint] | v +-- Layer 2: [design decision]


# 使用技巧:
# 1. MANDATORY + ALL 双重强调
# 2. 提供精确模板
# 3. 用代码块展示格式

正反对比约束

CORRECT Response:

Reasoning Chain

+-- Layer 1: Send/Sync Error ...


WRONG Response (stops at L1):

Problem: Rc is not Send Solution: Use Arc


# 使用技巧:
# 1. 并列展示正确/错误
# 2. 标注 (stops at L1) 说明错在哪
# 3. 让模型清楚知道什么是不想要的

常见错误

1. 约束太弱

# 差
You should probably include the reasoning chain.
It would be nice to reference domain constraints.

# 好
You MUST include the reasoning chain.
CRITICAL: Reference domain constraints.

2. 约束太模糊

# 差
Follow the meta-cognition framework.

# 好
STEP 1: Identify entry layer (L1/L2/L3)
STEP 2: Load appropriate skills using Skill() tool
STEP 3: Trace through layers (UP or DOWN)
STEP 4: Output with reasoning chain format

3. 没有示例

# 差
Output should include reasoning chain.

# 好
Output MUST include reasoning chain:

Reasoning Chain

+-- Layer 1: [error] +-- Layer 3: [constraint] +-- Layer 2: [decision]

4. 没有反例

# 差
Include domain analysis.

# 好
Include domain analysis.

WRONG (without domain analysis):
  "Use Arc instead of Rc"

CORRECT (with domain analysis):
  "From domain-web: handlers run on any thread,
   therefore Arc + State extractor is recommended"

约束效果验证

测试方法

1. 准备测试问题
   "Web API 报错 Rc cannot be sent"

2. 无约束测试
   → 观察默认输出

3. 加约束测试
   → 观察是否遵循

4. 调整约束强度
   → 找到最小有效约束

检查清单

□ 是否加载了指定的 Skills?
□ 是否执行了领域检测?
□ 是否输出了推理链?
□ 是否引用了领域约束?
□ 是否遵循了输出格式?

最佳实践总结

1. 约束强度选择

情况推荐强度
核心流程CRITICAL + MUST
重要规则IMPORTANT / REQUIRED
建议性should / recommended
可选项can / may

2. 约束结构

1. 先声明约束级别 (CRITICAL/IMPORTANT)
2. 再描述具体要求 (You MUST...)
3. 提供正确示例 (CORRECT:)
4. 提供反面示例 (WRONG:)
5. 条件触发 (IF...THEN...)

3. 格式化

✓ 使用大写强调关键词
✓ 使用表格组织规则
✓ 使用编号步骤
✓ 使用代码块展示格式
✓ 使用分隔线划分区域

4. 迭代优化

观察输出 → 识别偏差 → 增强约束 → 再次测试

常见调整:
- 约束词不够强 → 升级到 CRITICAL/MUST
- 规则不够具体 → 添加示例
- 遗漏情况 → 添加条件分支

一句话总结

约束的本质是改变模型的概率分布,让"遵循指令"的路径概率高于"默认行为"的路径。

关键技巧:

  1. 强度递增: should < IMPORTANT < CRITICAL + MUST
  2. 正反对比: CORRECT vs WRONG 示例
  3. 格式模板: 具体格式 > 抽象描述
  4. 条件触发: IF...THEN 明确边界

你可能感兴趣的文章

Meta-Cognition 示例: E0382 交易系统

Rust-Skills 功能概览

Skills Design Lessons Learned

Hook 机制详解

Context Optimization Guide

Claude Code 配置优先级

Skill 继承机制

Skills 编写最佳实践

What Is a Skill?

Forced Eval Hook 原理

  • 所属分类: AI
  • 本文标签: rust
  • 版权声明: 本文链接 https://seaxiang.com/blog/PJjhxgt0