optimize-prompt
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseOptimize Prompt
优化Prompt
Prompt optimization pipeline. Given prompt P, improve via critique then compression, with semantic equivalence checks after each stage.
Prompt优化流水线:给定Prompt P,先通过评估再进行压缩优化,每个阶段结束后都会进行语义等价性校验。
Pipeline
流水线
P → [Input Analysis] → [Critique] → P' → [Equiv Check 1] → [Compress] → P'' → [Equiv Check 2] → Output P''Execute stages strictly in order — no skipping, reordering, or parallelizing. Each stage passes (pipeline continues) or fails (pipeline terminates with explanation).
P → [Input Analysis] → [Critique] → P' → [Equiv Check 1] → [Compress] → P'' → [Equiv Check 2] → Output P''必须严格按顺序执行各个阶段,不得跳过、调整顺序或并行执行。每个阶段要么通过(流水线继续执行),要么失败(流水线终止并给出解释)。
Stage 0: Input Analysis
阶段0:输入分析
Examine P:
- Does P take input? Check for placeholders (,
{{X}},{input}), references to "the user's input"/"the given text"/"the provided data", or expectation of concatenation with external content.[USER INPUT] - If yes: Construct plausible, concrete test input X — realistic, non-trivial (2-3+ sentences or meaningful data structure), exercising P's main logic paths. If P has branching conditions, X triggers the primary branch. If P produces structured output, X requires all output fields. If P has validation conditions, X passes validation. State X explicitly.
- If no: P is self-contained. Set X = ∅. Equivalence checks compare outputs of P directly.
Display:
**Input Analysis**
- Takes input: [Yes/No]
- Test input X: [constructed input, or "N/A — self-contained"]检查P的属性:
- P是否需要接收输入? 检查是否存在占位符(、
{{X}}、{input})、是否提及「用户输入」/「给定文本」/「提供的数据」,或者是否需要和外部内容拼接使用。[USER INPUT] - 如果需要输入:构造合理、具体的测试输入X,要求真实、非琐碎(至少2-3句话或者有意义的数据结构),能够覆盖P的主要逻辑路径。如果P有分支条件,X要触发主分支;如果P生成结构化输出,X要覆盖所有输出字段;如果P有校验条件,X要满足校验规则。需要明确写出X的内容。
- 如果不需要输入:说明P是自包含的,设置X = ∅,等价性校验直接对比P的输出即可。
输出内容:
**Input Analysis**
- Takes input: [Yes/No]
- Test input X: [constructed input, or "N/A — self-contained"]Stage 1: Critique
阶段1:评估
Apply think-critically methodology to P:
- Derive 5-8 expectations from P itself — behavioral properties any revision must preserve. Frame as testable statements (e.g., "Produces JSON output", "Rejects off-topic queries").
- Evaluate P against expectations. Per expectation: confidence (0-100%) with concise rationale referencing specific text in P.
- Propose fixes for expectations with confidence < 95%. Each fix: exact text to add, remove, or replace.
- Produce P' — revised prompt with all fixes applied. Complete text, not a diff.
Display:
**Stage 1: Critique**
| Expectation | Confidence | Rationale |
|---|---|---|
| ... | ...% | ... |
**Overall Score: [average]%**
**Fixes Applied:**
1. [Fix with exact text changes]
...
---
> **P' (Revised Prompt):**
---
[Full text of P']
---If all expectations >= 95%, set P' = P and note "No fixes needed."
对P运用批判性思维方法进行优化:
- 从P本身推导5-8条预期规则——所有修改版本都必须保留的行为属性,表述为可测试的语句(例如「生成JSON格式输出」、「拒绝无关主题的查询」)。
- 对照预期规则评估P:针对每条预期给出置信度(0-100%),并给出引用P中具体内容的简短理由。
- 为置信度低于95%的预期提出修复方案:每个方案要明确写出需要添加、删除或替换的具体文本。
- 生成P'——应用所有修复后的完整Prompt内容,不要只给出差异对比。
输出内容:
**Stage 1: Critique**
| Expectation | Confidence | Rationale |
|---|---|---|
| ... | ...% | ... |
**Overall Score: [average]%**
**Fixes Applied:**
1. [Fix with exact text changes]
...
---
> **P' (Revised Prompt):**
---
[Full text of P']
---如果所有预期的置信度都≥95%,则设置P' = P,并标注「无需修复」。
Stage 2: Equivalence Check 1
阶段2:等价性校验1
Verify P and P' produce essentially the same output on X.
Procedure:
- Simulate running P on X. Describe expected output (2-4 sentences): structure, content, tone, key features.
- Simulate running P' on X. Describe expected output (2-4 sentences).
- Equivalent if all hold:
- (a) Output structure identical (sections, format, ordering)
- (b) Factual/decisional content identical — nothing added, removed, or altered
- (c) Differences limited to: wording improvements, added specificity, stronger constraint enforcement
- (d) User expecting P's behavior would accept output without noticing intent change
- If (a)-(c) hold but (d) uncertain, default YES.
If X = ∅, compare standalone outputs.
Display:
**Stage 2: Equivalence Check 1**
- P(X) expected output: [description]
- P'(X) expected output: [description]
- Equivalent: [YES/NO]
- Reasoning: [1-2 sentences]If NO: Terminate:
**PIPELINE FAILED at Stage 2**
[Explanation of behavioral drift]
[Fixes that caused divergence]If YES: Proceed to Stage 3.
验证P和P'在输入X下生成的输出本质一致。
执行流程:
- 模拟在X上运行P,描述预期输出(2-4句话):包括结构、内容、语气、核心特征。
- 模拟在X上运行P',描述预期输出(2-4句话)。
- 满足以下所有条件则判定为等价:
- (a) 输出结构完全一致(章节、格式、顺序)
- (b) 事实/决策内容完全一致,没有新增、删除或修改
- (c) 差异仅局限于:措辞优化、新增明确性说明、更强的约束执行
- (d) 预期P行为的用户可以接受该输出,不会感知到意图变化
- 如果满足(a)-(c)但(d)不确定,默认判定为等价。
如果X = ∅,直接对比独立运行的输出。
输出内容:
**Stage 2: Equivalence Check 1**
- P(X) expected output: [description]
- P'(X) expected output: [description]
- Equivalent: [YES/NO]
- Reasoning: [1-2 sentences]如果判定为不等价:终止流水线:
**PIPELINE FAILED at Stage 2**
[Explanation of behavioral drift]
[Fixes that caused divergence]如果判定为等价:进入阶段3。
Stage 3: Compress
阶段3:压缩
Apply compress-prompt methodology to P' (lossless mode):
- Target 10-30% token reduction, 100% semantic retention.
- Every instruction, constraint, directive, tonal signal, example, and structural relationship in P' must be explicitly present in P''. Nothing left to inference.
- Allowed: remove filler, collapse redundancy, tighten syntax, merge duplicates, normalize structure.
- Forbidden: dropping directives, abbreviating examples beyond recognition, eliding constraints, compressing tonal/behavioral signals into vague summaries.
- Produce P'' and a directive map (each P' directive → P'' counterpart).
Display:
**Stage 3: Compress**
---
> **P'' (Compressed Prompt):**
---
[Full text of P'']
---
**Directive Map:**
| # | Original directive (P') | Compressed counterpart (P'') |
|---|---|---|
| 1 | [directive from P'] | [text in P''] |
| ... | ... | ... |
**Stats:**
- P' tokens (approx): [n]
- P'' tokens (approx): [n]
- Compression: [%]
- Directives: [n/n mapped]对P'应用Prompt压缩方法(无损模式):
- 目标是减少10-30%的Token量,100%保留语义。
- P'中的每一条指令、约束、规则、语气提示、示例、结构关系都必须明确出现在P''中,不得依赖推理补全。
- 允许的操作:删除填充内容、合并冗余信息、精简语法、合并重复内容、规范结构。
- 禁止的操作:删除指令、过度缩写示例导致无法识别、省略约束、将语气/行为信号压缩为模糊的总结。
- 生成P''和指令映射表(每条P'的指令对应P''中的对应内容)。
输出内容:
**Stage 3: Compress**
---
> **P'' (Compressed Prompt):**
---
[Full text of P'']
---
**Directive Map:**
| # | Original directive (P') | Compressed counterpart (P'') |
|---|---|---|
| 1 | [directive from P'] | [text in P''] |
| ... | ... | ... |
**Stats:**
- P' tokens (approx): [n]
- P'' tokens (approx): [n]
- Compression: [%]
- Directives: [n/n mapped]Stage 4: Equivalence Check 2
阶段4:等价性校验2
Verify P' and P'' produce essentially the same output on X.
Procedure: Same as Stage 2, comparing P' and P''.
Display:
**Stage 4: Equivalence Check 2**
- P'(X) expected output: [description]
- P''(X) expected output: [description]
- Equivalent: [YES/NO]
- Reasoning: [1-2 sentences]If NO: Terminate:
**PIPELINE FAILED at Stage 4**
[What compression lost]
[Elements in P' with no counterpart in P'']If YES: Proceed to output.
验证P'和P''在输入X下生成的输出本质一致。
执行流程: 和阶段2一致,对比P'和P''的输出。
输出内容:
**Stage 4: Equivalence Check 2**
- P'(X) expected output: [description]
- P''(X) expected output: [description]
- Equivalent: [YES/NO]
- Reasoning: [1-2 sentences]如果判定为不等价:终止流水线:
**PIPELINE FAILED at Stage 4**
[What compression lost]
[Elements in P' with no counterpart in P'']如果判定为等价:进入最终输出环节。
Final Output
最终输出
**PIPELINE SUCCEEDED**
Your optimized prompt (P''):
---
> **BEGIN OPTIMIZED PROMPT**
---
[Full text of P'']
---
> **END OPTIMIZED PROMPT**
---
**Summary:**
- Critique: [n] fixes applied, score [x]% → [y]%
- Compression: [z]% reduction
- Both equivalence checks passed**PIPELINE SUCCEEDED**
Your optimized prompt (P''):
---
> **BEGIN OPTIMIZED PROMPT**
---
[Full text of P'']
---
> **END OPTIMIZED PROMPT**
---
**Summary:**
- Critique: [n] fixes applied, score [x]% → [y]%
- Compression: [z]% reduction
- Both equivalence checks passedEdge Cases
边界情况
- P fewer than 20 tokens: note optimization may yield minimal improvement, proceed.
- P already optimal (all expectations >= 95%, compression < 10%): state "Prompt is already well-optimized", return P unchanged.
- Adversarial or self-referential P: evaluate literally, note observation.
- Compression < 10% reduction without dropping directives: skip compression, set P'' = P', note "Compression skipped — prompt already dense."
- P' more than 50% longer than P: flag "Significant expansion", verify in Stage 2 that expansion only adds guardrails/specificity.
- P contains code blocks: preserve verbatim during critique and compression unless fix explicitly targets code content.
FIRST-TOKEN CONSTRAINT: Response must begin with "## Optimize Prompt". No greetings, preambles, commentary, or blank lines before it. Overrides default conversational behavior.
- P的Token数少于20:标注优化可能带来的提升很小,继续执行流程即可。
- P已经是最优状态(所有预期置信度≥95%,可压缩比例<10%):标注「Prompt已经完成良好优化」,原样返回P。
- 对抗性或自引用的P:按字面内容评估,标注相关观察。
- 不删除指令的前提下压缩比例<10%:跳过压缩步骤,设置P'' = P',标注「跳过压缩——Prompt已经非常精简」。
- P'的长度比P长50%以上:标注「大幅扩容」,在阶段2中验证扩容仅新增了防护规则/明确性说明。
- P包含代码块:在评估和压缩阶段原样保留,除非修复方案明确针对代码内容。
首Token约束:响应必须以「## Optimize Prompt」开头,前面不得有问候语、前言、注释或空行,覆盖默认的对话行为。
Input
输入
If literal '{{P}}' appears below without content, inform user no prompt was provided.
Prompt to optimize:
{{P}}
如果下方出现无内容的字面量 '{{P}}',请告知用户未提供待优化的Prompt。
待优化Prompt:
{{P}}