OpenAI Prompt Engineering-3. 工程迭代

OpenAI的Prompt Engineering 课程

2. 工程迭代

2.1 解决问题的过程

如果你看过老师的机器学习的课程,你可能会见过这张图,你经常有一个想法然后实现它,所以你去写代码,获取数据,训练你的模型,这就能获得一个实验性的结果,然后再根据这个结果做错误分析,看看它在什么地方起作用了或者不起作用。然后甚至可能改变你的想法,然后再通过这个循环继续寻找解决方案,如此反复。最终得到一个有效的机器学习模型。写提示词的过程也跟这个过程差不多,提示词也需要运用我们前面学习到的准则和策略,经过不断的迭代才能取得更理想的效果。吴恩达老师并没有过多的关系网上说的 30 个完美提示词的文章,因为他认为没有一个完美的提示词适用于世间万物。

下面继续通过一个案例来演示提示词的迭代过程,是如何一步步打造提示词最终满足我们的需求的:

案例是为一张椅子写一份信息表,用于展示在零售网站上,作为一个产品描述。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
fact_sheet_chair = """
OVERVIEW
- Part of a beautiful family of mid-century inspired office
including filing cabinets,desks,bookcases,meeting tables,
- Several options of shell color and base finishes.
- Available with plastic back and front upholstery(SWC-100)
or Eull upholstery(SWC-110)in 10 fabric and 6 leather option
- Base finish options are: stainless steel,matte black,
gloss white, or chrome.
- Chair is available with or without armrests.
- Suitable for home or business settings.
- Qualified for contract use.
CONSTRUCTION
- 5-wheel plastic coated aluminum base.
- Pneumatic chair adjust for easy raise/lower action.
DIMENSIONS
- WIDTH 53 CM| 20.87"
- DEPTI 51 CM 20.08"
- HEIGAT 80 CM 31.50"
- SEAT HEIGHT 44 CM | 17.32"
- SEAT DEPTH 41 CM | 16.14"
OPTIONS
- Soft or hard-floor caster options.
- Two choices of seat foam densities:
medium(1.8 1b/ft3)or high(2.8 1b/ft3)
- Armless or 8 position PU armrests
MATERIALS
SHELL BASE GLIDER
- Cast Aluminum with modified nylon PA6/PA66 coating.
- Shell thickness: 10 mm.
SEAT
- HD36 foam
COUNTRY OF ORIGIN
- Italy
"""

prompt = f"""
Your task is to help a marketing team create a
description for a retail website of a product based
on a technical fact sheet.
Write a product description based on the information
provided in the technical specifications delimited by
triple backticks.
Technical specifications:'''{fact_sheet_chair}'''
"""

大模型输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
**Mid-Century Inspired Office Chair – Elegant, Versatile & Durable**  

Elevate your workspace with our stunning mid-century inspired office chair, part of a sophisticated collection that includes desks, bookcases, and filing cabinets. Designed for both style and functionality, this chair blends timeless aesthetics with modern comfort, making it perfect for home offices, corporate settings, or contract use.

**Customizable Design**
Choose from a variety of shell colors and base finishes—stainless steel, matte black, gloss white, or chrome—to match your décor. Personalize further with optional armrests and upholstery choices: select plastic-backed (SWC-100) or full fabric/leather upholstery (SWC-110) in 10 fabric and 6 premium leather options.

**Engineered for Comfort & Support**
- **Adjustable Height**: Pneumatic lift allows effortless height adjustments.
- **Seat Options**: Pick between medium (1.8 lb/ft³) or high-density (2.8 lb/ft³) foam for tailored comfort.
- **Durable Base**: 5-wheel glides with soft- or hard-floor caster options ensure smooth mobility.

**Premium Construction**
- **Shell & Base**: Heavy-duty cast aluminum with nylon coating (10mm thickness) for lasting durability.
- **Seat**: High-resilience HD36 foam provides exceptional support for long hours of use.

**Dimensions (W x D x H)**
53 cm (20.87") x 51 cm (20.08") x 80 cm (31.50") | Seat Height: 44 cm (17.32") | Seat Depth: 41 cm (16.14")

**Crafted in Italy**
A testament to quality and design, this chair is proudly made in Italy, blending artisanal craftsmanship with ergonomic innovation.

**Ideal for Any Space**
Whether you need a sleek addition to your home office or a reliable chair for high-traffic business environments, this versatile piece delivers style, comfort, and resilience.

Upgrade your workspace today with a chair that’s as functional as it is beautiful!

可以看到大模型的输出也很好,做了很全面的描述。但是现在我觉得它有点太长了,最好是能短点,继续优化提示词,加上最大字符数限定的描述:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
fact_sheet_chair = """
OVERVIEW
- Part of a beautiful family of mid-century inspired office
including filing cabinets,desks,bookcases,meeting tables,
- Several options of shell color and base finishes.
- Available with plastic back and front upholstery(SWC-100)
or Eull upholstery(SWC-110)in 10 fabric and 6 leather option
- Base finish options are: stainless steel,matte black,
gloss white, or chrome.
- Chair is available with or without armrests.
- Suitable for home or business settings.
- Qualified for contract use.
CONSTRUCTION
- 5-wheel plastic coated aluminum base.
- Pneumatic chair adjust for easy raise/lower action.
DIMENSIONS
- WIDTH 53 CM| 20.87"
- DEPTI 51 CM 20.08"
- HEIGAT 80 CM 31.50"
- SEAT HEIGHT 44 CM | 17.32"
- SEAT DEPTH 41 CM | 16.14"
OPTIONS
- Soft or hard-floor caster options.
- Two choices of seat foam densities:
medium(1.8 1b/ft3)or high(2.8 1b/ft3)
- Armless or 8 position PU armrests
MATERIALS
SHELL BASE GLIDER
- Cast Aluminum with modified nylon PA6/PA66 coating.
- Shell thickness: 10 mm.
SEAT
- HD36 foam
COUNTRY OF ORIGIN
- Italy
"""

prompt = f"""
Your task is to help a marketing team create a
description for a retail website of a product based
on a technical fact sheet.
Write a product description based on the information
provided in the technical specifications delimited by
triple backticks.

Use at most 50 words.

Technical specifications:'''{fact_sheet_chair}'''
"""

然后我们再运行一下,看看 DeepSeek 的输出:

1
2
3
4
5
6
**Mid-Century Inspired Office Chair**  

Elevate your workspace with this stylish, versatile chair. Choose from multiple shell colors, base finishes, and upholstery options (fabric or leather). Features a 5-wheel base, pneumatic height adjustment, and optional armrests. Durable construction, suitable for home or office. Made in Italy.

*Options: casters, foam density, and armrest styles available.*

可以看到已经按照提示简短了不少,我们调用len 函数看看字数是多少:

1
字数:55

虽然没有严格按照要求来,但是已经做的很不错了。大模型在遵循关于非常精确的字数指令方面,效果不是那么好,有的时候会输出 60 或者 65 个字,这些是在合理范围之内的。那我们再来试试,最多输出三句话试试:

1
2
3
4
5
6
7
8
9
10
11
12
prompt = f"""
Your task is to help a marketing team create a
description for a retail website of a product based
on a technical fact sheet.
Write a product description based on the information
provided in the technical specifications delimited by
triple backticks.

Use at most 3 sentences.

Technical specifications:'''{fact_sheet_chair}'''
"""

大模型输出:

1
**Elevate your workspace with this mid-century inspired office chair, part of a stylish collection featuring versatile design options. Choose from multiple shell colors, base finishes, and upholstery choices (fabric or leather), with armrest and foam density customization. Built for durability with a 5-wheel aluminum base and pneumatic height adjustment, it’s perfect for home or professional settings—crafted in Italy for timeless quality.**

这其实都是在告诉大模型你想要输出的长度的不同方法,我们可以看到这次做的不错,刚好就三句。有的人也会尝试通过限定字符数来约束最大长度,比如:

1
2
3
4
5
6
7
8
9
10
11
12
prompt = f"""
Your task is to help a marketing team create a
description for a retail website of a product based
on a technical fact sheet.
Write a product description based on the information
provided in the technical specifications delimited by
triple backticks.

Use at most 300 characters.

Technical specifications:'''{fact_sheet_chair}'''
"""

我们限制了 300 个字符,大模型解释文本的方式是通过一种叫做标记器的东西,所以它在计算字符方面能力一般般。我们看看这次大模型是怎么输出的:

1
2
3
**Mid-Century Inspired Office Chair**  

Elevate your workspace with this stylish, versatile chair, part of a mid-century office collection. Choose from multiple shell colors, base finishes (stainless steel, matte black, etc.), and upholstery options (fabric or leather). Features a 5-wheel base, pneumatic height adjustment, and optional armrests. Perfect for home or business. Made in Italy.

这次直接输出了 413个字符,误差稍微有些大。(课程里约束了 280 个字符,实际输出 281 个字符,吴恩达老师也说通常情况下,一个大语言模型是无法达到这么接近的)。

1
字符数:413

到目前为止我们演示了通过不同的方式限制了输出的最大文本长度。后面我们的需求可能还会不断的变化,比如为我们的网站继续完善这个文本的时候我们决定这个网站并不是直接面向消费者的,可能是要展示给家具零售商看,他们可能会对椅子的技术细节和材料等更感兴趣,我们继续修改提示词,让它对技术细节更加精确。

1
2
3
4
5
6
7
8
9
10
11
12
13
prompt_1= f"""
Your task is to help a marketing team create a
description for a retail website of a product based
on a technical fact sheet.
write a product description based on the information
provided in the technical specifications delimited by
triple backticks.
The description is intended for furniture retailers,
so should be technical in nature and focus on the
materials the product is constructed from.
use at most 50 words.
Technical specifications: ```{fact_sheet_chair}```
"""

再来看看输出:

1
2
3
**Mid-Century Office Chair – Technical Description**  

Crafted with a 10 mm cast aluminum shell (PA6/PA66 coated) and a 5-wheel aluminum base, this chair offers durability and style. Choose between plastic or full upholstery (10 fabric/6 leather options) and multiple base finishes (stainless steel, matte black, etc.). Features HD36 foam seating, pneumatic height adjustment, and contract-grade construction. Made in Italy.

可以看到,这次的输出确实是更侧重于椅子的技术参数。所以,我们可以通过调整提示词让大模型更专注于特定内容。

所以上面的演示过程就是想说明,一个好的提示词并不是一蹴而就的,往往需要很多次的调整,以达到越来越接近我们理想中的效果。

为了节目效果,我们演示一个更加复杂的情况,我们在提示词中加入一些额外的指令,在描述之后,给出一个产品尺寸的表格,然后把所有的东西格式化为 HTML。提示词如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
prompt_2 = f"""
Your task is to help a marketing team create a
description for a retail website of a product based
on a technical fact sheet.
write a product description based on the information
provided in the technical specifications delimited by
triple backticks.
The description is intended for furniture retailers,
so should be technical in nature and focus on the
materials the product is constructed from.
At the end of the description, include every 7-character
Product ID in the technical specification.
After the description,include a table that gives the
product's dimensions. zhe table should have two columns.
In the first column include the name of the dimension.
In the second column include the measurements in inches onl)
Give the table ti title 'Product Dimensions'.
Format everything as HTML that can be used in a website.
Place the description in a <div> element.
Technical specifications: ```{fact_sheet_chair}```
"""

看看DeepSeek 是怎么输出的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Here's the HTML-formatted product description based on the technical specifications:

```html
<div>
<h2>Mid-Century Inspired Office Chair</h2>
<p>Part of our premium mid-century office furniture collection, this chair combines elegant design with robust construction. The chair features a durable cast aluminum shell with a modified nylon PA6/PA66 coating (10mm thickness), supported by a 5-wheel plastic-coated aluminum base for stability and smooth movement.</p>

<p>The chair offers multiple customization options including shell colors, base finishes (stainless steel, matte black, gloss white, or chrome), and upholstery choices (plastic back/front or full upholstery in 10 fabric and 6 leather options). The seat is constructed with high-density HD36 foam, available in two density options (medium 1.8 lb/ft³ or high 2.8 lb/ft³).</p>

<p>Designed for both comfort and functionality, it features pneumatic height adjustment and is available with or without 8-position PU armrests. Suitable for both home and commercial use, this chair is qualified for contract applications. Choose between soft or hard-floor caster options to suit your environment.</p>

<p>Product IDs: SWC-100, SWC-110</p>

<h3>Product Dimensions</h3>
<table border="1">
<tr>
<th>Dimension</th>
<th>Inches</th>
</tr>
<tr>
<td>Width</td>
<td>20.87"</td>
</tr>
<tr>
<td>Depth</td>
<td>20.08"</td>
</tr>
<tr>
<td>Height</td>
<td>31.50"</td>
</tr>
<tr>
<td>Seat Height</td>
<td>17.32"</td>
</tr>
<tr>
<td>Seat Depth</td>
<td>16.14"</td>
</tr>
</table>
</div>
```

This HTML includes:
1. A detailed technical description focusing on materials and construction
2. All product IDs (SWC-100 and SWC-110)
3. A table with all dimensions converted to inches
4. Proper HTML formatting with div container and table structure
5. Clear section headings for easy scanning

The description emphasizes the chair's materials (aluminum shell with nylon coating, HD36 foam seat) and construction features while including all the technical specifications from the fact sheet.

2.2 总结

所以,演示了这么些就是为了让你能够明白提示词的开发是一个迭代的过程,不断的尝试,看看它为什么还不能完全满足你的要求,然后根据我们上节说的清晰的描述和给模型更多的时间思考原则,以使它更接近于你想要的结果。我们今天指示使用了一个案例来演示,在实际情况中,你可能需要迭代一个提示词,然后根据大量的案例对它的效果进行评估,了解这个提示词在最好和最坏的情况下的表现,然后根据指标来对它进行全面的评估。但是往往是在应用程序比较成熟的时候才会做这个事情。