技术

迭代改进

通过迭代改进提示词

提示工程很少是一次完成的过程。最好的提示是通过迭代产生的——测试、观察和优化,直到获得理想的结果。

初稿,而非终稿

把你的第一个提示当作初稿。即使是经验丰富的提示工程师也很少能一次就写对。

迭代循环

有效的提示优化遵循一个可预测的循环:编写、测试、分析和改进。每次迭代都会让你更接近一个能可靠产生所需结果的提示。

迭代改进演示观察提示词的演变
版本 1 / 4
提示词v1
Write a product description.
输出:
This is a great product. It has many features. You should buy it.
质量
20%
问题: Too vague, no specific details

常见优化模式

大多数提示失败都可以归类为几种情况。学会识别这些模式可以让你快速诊断和修复问题,而无需从头开始。

问题:输出太长

这是最常见的问题之一。如果没有明确的约束,模型倾向于提供详尽而非简洁的回答。

原始版本
Explain how photosynthesis works.
优化版本
Explain how photosynthesis works in 3-4 sentences suitable for a 10-year-old.

问题:输出太模糊

模糊的提示会产生模糊的输出。模型无法读取你的想法,不知道"更好"意味着什么,也不知道哪些方面对你最重要。

原始版本
Give me tips for better presentations.
优化版本
Give me 5 specific, actionable tips for improving technical presentations to non-technical stakeholders. For each tip, include a concrete example.

问题:语气不对

语气是主观的,会因上下文而异。模型认为的"专业"可能与你组织的风格或与收件人的关系不匹配。

原始版本
Write an apology email for missing a deadline.
优化版本
Write a professional but warm apology email for missing a project deadline. The tone should be accountable without being overly apologetic. Include a concrete plan to prevent future delays.

问题:缺少关键信息

开放式的请求会得到开放式的回应。如果你需要特定类型的反馈,必须明确地提出要求。

原始版本
Review this code.
优化版本
Review this Python code for: 1. Bugs and logical errors 2. Performance issues 3. Security vulnerabilities 4. Code style (PEP 8) For each issue found, explain the problem and suggest a fix. [code]

问题:格式不一致

如果没有模板,模型会以不同的方式组织每个回应,使比较变得困难,自动化也无法实现。

原始版本
Analyze these three products.
优化版本
Analyze these three products using this exact format for each: ## [Product Name] **Price:** $X **Pros:** [bullet list] **Cons:** [bullet list] **Best For:** [one sentence] **Rating:** X/10 [products]

系统化优化方法

随机的修改会浪费时间。系统化的方法可以帮助你快速识别问题并高效地修复它们。

第一步:诊断问题

在修改任何内容之前,先确定到底出了什么问题。使用这个诊断表将症状映射到解决方案:

症状可能原因解决方案
太长没有长度限制添加字数/句子限制
太短缺少详细说明要求要求详细阐述
跑题指令模糊更加具体
格式错误未指定格式定义确切结构
语气不对受众不明确指定受众/风格
不一致没有提供示例添加少样本示例

第二步:进行针对性修改

抵制重写一切的冲动。同时修改多个变量会让你无法知道什么有帮助、什么有害。每次只修改一个地方,测试后再继续:

迭代 1:添加长度限制
迭代 2:指定格式
迭代 3:添加示例
迭代 4:优化语气说明

第三步:记录有效的内容

提示工程的知识很容易丢失。记录你尝试过的内容和原因。这在你以后重新查看提示或面临类似挑战时可以节省时间:

## 提示:客户邮件回复

### 版本 1(太正式)
"Write a response to this customer complaint."

### 版本 2(语气更好,但结构仍然缺失)
"Write a friendly but professional response to this complaint. 
Show empathy first."

### 版本 3(最终版 - 效果良好)
"Write a response to this customer complaint. Structure:
1. Acknowledge their frustration (1 sentence)
2. Apologize specifically (1 sentence)  
3. Explain solution (2-3 sentences)
4. Offer additional help (1 sentence)

Tone: Friendly, professional, empathetic but not groveling."

真实世界的迭代示例

让我们完整地走一遍迭代循环,看看每次优化是如何在前一次基础上构建的。注意每个版本是如何解决前一个版本的具体不足的。

任务:生成产品名称

版本 1太笼统,没有上下文
Generate names for a new productivity app.
版本 1版本 2
添加了上下文,仍然笼统
版本 1
Generate names for a new productivity app.
版本 2
Generate names for a new productivity app. The app uses AI to automatically schedule your tasks based on energy levels and calendar availability.
版本 2版本 3
添加了约束和推理
版本 2
Generate names for a new productivity app. The app uses AI to automatically schedule your tasks based on energy levels and calendar availability.
版本 3
Generate 10 unique, memorable names for a productivity app with these characteristics: - Uses AI to schedule tasks based on energy levels - Target audience: busy professionals aged 25-40 - Brand tone: modern, smart, slightly playful - Avoid: generic words like "pro", "smart", "AI", "task" For each name, explain why it works.
版本 3版本 4(最终版)
结构化格式,具体要求
版本 3
Generate 10 unique, memorable names for a productivity app with these characteristics: - Uses AI to schedule tasks based on energy levels - Target audience: busy professionals aged 25-40 - Brand tone: modern, smart, slightly playful - Avoid: generic words like "pro", "smart", "AI", "task" For each name, explain why it works.
版本 4(最终版)
Generate 10 unique, memorable names for a productivity app. Context: - Uses AI to schedule tasks based on energy levels - Target: busy professionals, 25-40 - Tone: modern, smart, slightly playful Requirements: - 2-3 syllables maximum - Easy to spell and pronounce - Available as .com domain (check if plausible) - Avoid: generic words (pro, smart, AI, task, flow) Format: Name | Pronunciation | Why It Works | Domain Availability Guess

按任务类型的优化策略

不同的任务会以可预测的方式失败。了解常见的失败模式可以帮助你更快地诊断和修复问题。

内容生成

内容生成通常会产生通用的、偏离目标的或格式不佳的输出。修复方法通常包括更具体地说明约束、提供具体示例或明确定义你的品牌声音。

太通用

添加具体约束和上下文

"Write about dogs" → "Write about golden retrievers for first-time owners, focusing on training and exercise needs"

太长

设置字数/段落限制

添加:"Keep response under 150 words" 或 "Maximum 3 paragraphs"

风格不对

提供风格示例

"Write in the style of this example: [paste sample text]"

不符合品牌

包含品牌声音指南

"Use friendly, casual tone. Avoid jargon. Address reader as 'you'."

代码生成

代码输出可能在技术上失败(语法错误、错误的语言特性)或在架构上失败(糟糕的模式、遗漏的情况)。技术问题需要版本/环境细节;架构问题需要设计指导。

语法错误

指定语言版本

"Use Python 3.11+ syntax with type hints" 或 "ES2022 JavaScript"

方法不对

描述首选模式

"Use functional approach, avoid classes" 或 "Follow repository pattern"

缺少边界情况

列出需要处理的场景

"Handle: empty input, null values, network timeouts, invalid formats"

命名不佳

包含命名约定

"Use camelCase for variables, PascalCase for classes, UPPER_SNAKE for constants"

分析任务

分析任务通常会产生表面或无结构的结果。用特定框架(SWOT、波特五力)指导模型,要求多个视角,或为输出结构提供模板。

太浅显

要求使用特定框架

"Analyze using SWOT framework" 或 "Apply Porter's Five Forces"

有偏见

要求多个视角

"Present arguments for and against" 或 "Include skeptic's viewpoint"

缺少数据

指定分析内容

"Focus on: market size, growth rate, key players, entry barriers"

无结构

提供分析模板

"Format as: Summary → Key Findings → Implications → Recommendations"

问答任务

问答可能太简短或太冗长,可能缺少置信度指标或来源。指定你需要的详细程度,以及是否希望引用来源或表达不确定性。

太简短

要求详细阐述

"Explain in detail with examples" 或 "Elaborate on each point"

太冗长

要求简洁回答

"Answer in 2-3 sentences" 或 "Give me the TL;DR"

不确定

要求置信度级别

"Rate your confidence 1-10" 或 "Note any assumptions made"

无来源

要求引用

"Cite sources for claims" 或 "Include references where possible"

反馈循环技术

这是一个元技术:使用模型本身来帮助改进你的提示。分享你尝试的内容、得到的结果和你想要的内容。模型通常可以建议你没有想到的改进。

I used this prompt:
"[your prompt]"

And got this output:
"[model output]"

I wanted something more [describe gap]. How should I modify 
my prompt to get better results?

A/B 测试提示

对于将重复使用或大规模使用的提示,不要只选择第一个有效的版本。测试变体以找到最可靠和最高质量的方法。

Prompt A: "Summarize this article in 3 bullet points."
Prompt B: "Extract the 3 most important insights from this article."
Prompt C: "What are the key takeaways from this article? List 3."

多次运行每个提示,比较:

  • 输出的一致性
  • 信息的质量
  • 与你需求的相关性

何时停止迭代

完美是"足够好"的敌人。知道什么时候你的提示已经可以使用,什么时候你只是在为递减的回报而打磨。

可以发布

输出始终满足要求

边界情况处理得当

格式可靠且可解析

进一步改进显示递减回报

继续迭代

不同运行的输出不一致

边界情况导致失败

关键要求被遗漏

你还没有测试足够多的变体

提示的版本控制

提示就是代码。对于任何在生产中使用的提示,要以同样的严格性对待:版本控制、变更日志,以及在出现问题时回滚的能力。

内置版本控制

prompts.chat 包含提示的自动版本历史。每次编辑都会保存,因此你可以比较版本并一键恢复之前的迭代。

对于自行管理的提示,使用文件夹结构:

prompts/
├── customer-response/
│   ├── v1.0.txt    # 初始版本
│   ├── v1.1.txt    # 修复语气问题
│   ├── v2.0.txt    # 重大重构
│   └── current.txt # 指向活动版本的符号链接
└── changelog.md    # 记录变更

总结

关键要点

从简单开始,仔细观察,一次只改一件事,记录有效的内容,并知道何时停止。最好的提示不是写出来的——它们是通过系统化的迭代发现的。

当优化一个产生错误结果的提示时,最佳方法是什么?

练习:改进这个提示

尝试自己改进这个薄弱的提示。编辑它,然后使用 AI 将你的版本与原版进行比较:

优化这个邮件提示

将这个模糊的邮件提示转变为能产生专业、有效结果的提示。

Write an email.

在下一章中,我们将探索用于结构化数据应用的 JSON 和 YAML 提示技术。