提示工程(2):提示迭代开发

一个好的提示,不是一蹴而就写出来的。需要不断的迭代,才能写得得到预期结果的提示。这个迭代的过程我们称为迭代的提示开发(Iterative Prompt Development,见下图)。

提示迭代开发(Iterative Prompt Development)

首先,我们有个很好的idea;其次,用代码和数据去实现它(清晰且明确的指令);然后,根据实验的结果或错误(为何不是想要的结果),不断调优和改进,直到最终达到满意的效果。

我们来试验一下,给出一段产品描述(一把意大利生产的椅子),对prompt进行调优,让模型给出满意的结果。

import openai
openai.api_key="your openai key starting with sk-"

下面是一段方法,辅助执行LLM交互。
输入:提示、采用的gpt模型(默认为gpt-3.5-turbo)
输出:gpt返回的文字

def get_completion (prompt, model="gpt-3.5-turbo"): 
    messages = [{"role": "user", "content": prompt}] 
    response = openai.ChatCompletion.create( 
        model=model,
        messages=messages,
        temperature=0
    ) 
    return response.choices[0].message["content"]

以下是某个意大利生产椅子的产品介绍和规格说明书。

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 full upholstery (SWC-110) in 10 fabric and 6 leather optic
- 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}'''
"""

response = get_completion(prompt)
print(response)
Introducing our stunning mid-century inspired office chair, a perfect addition to any home or business setting. This chair is part of a beautiful family of office furniture, including filing cabinets, desks, bookcases, and meeting tables, allowing you to create a cohesive and stylish workspace.

One of the standout features of this chair is the variety of options available. You can choose from several shell colors and base finishes, allowing you to customize the chair to suit your personal style. The chair is available with either plastic back and front upholstery or full upholstery in a range of 10 fabric and 6 leather optic options. The base finish options include stainless steel, matte black, gloss white, or chrome. Additionally, you have the choice of having the chair with or without armrests.

Constructed with durability and comfort in mind, this chair features a 5-wheel plastic coated aluminum base, ensuring stability and ease of movement. The pneumatic chair adjust allows for easy raise and lower action, providing you with the perfect seating position.

The dimensions of this chair are as follows: width 53 cm (20.87"), depth 51 cm (20.08"), height 80 cm (31.50"), seat height 44 cm (17.32"), and seat depth 41 cm (16.14"). These dimensions make it suitable for a variety of body types and ensure a comfortable sitting experience.

We offer additional options to enhance your chair's functionality. You can choose between soft or hard-floor caster options, depending on your flooring type. Additionally, you have the choice of two seat foam densities: medium (1.8 1b/ft3) or high (2.8 1b/ft3). This allows you to select the level of support that best suits your needs.

The materials used in the construction of this chair are of the highest quality. The shell base glider is made of cast aluminum with a modified nylon PA6/PA66 coating, ensuring durability and longevity. The shell thickness is 10 mm, providing a sturdy and reliable structure. The seat is made of HD36 foam, offering exceptional comfort for extended periods of sitting.

Rest assured that this chair is of Italian origin, known for its craftsmanship and attention to detail. It is also qualified for contract use, making it suitable for commercial settings.

Upgrade your workspace with our stylish and functional mid-century inspired office chair. Experience the perfect blend of comfort, durability, and style.

写得不错,上面的内容结合了产品介绍和规格说明书的内容,就是内容好象多了些,我们尝试减少一些。加上”use at most 50 words”,以下是运行的效果。

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}'''
"""

response = get_completion(prompt)
print(response)
Introducing our mid-century inspired office chair, perfect for both home and business settings. With various color and base options, this chair offers style and versatility. Choose between plastic or full upholstery, and select from a range of finishes. The chair features a 5-wheel base and pneumatic chair adjustment for easy use. Made in Italy.

篇幅大大缩减了。上面有多少字呢?

len(response.split(" "))
54

54个words。虽然不太精准(50),但也大大的减少了篇幅。我们试试280个字符,看是否会准确一些。加prompt里加上Use at most 280 characters。

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 280 characters.

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

response = get_completion(prompt)
print(response)
Introducing our mid-century inspired office chair! With various color and base options, this chair is perfect for any home or business setting. It features a 5-wheel base and pneumatic chair adjust for easy use. Made with high-quality materials from Italy.
len(response)
256

不错,毕竟没有超过280个字符。

接下来我们换一种风格。我们需要更多关于产品技术和材料方面的说明。因此,我们在prompt里加了之方面的要求:The description is intended for furniture retailers, so should be technical in nature and focus on the materials the product is constructed from.

我们来试下效果如何?

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.

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}'''
"""

response = get_completion(prompt)
print(response)
Introducing our mid-century inspired office chair, perfect for both home and business settings. Constructed with a durable cast aluminum shell and a 5-wheel plastic coated aluminum base, this chair offers easy raise/lower action with its pneumatic chair adjust. Choose from a variety of shell colors and base finishes, and enjoy the option of plastic or full upholstery. With its stylish design and high-quality materials, this chair is a must-have for any modern workspace. Made in Italy.

如果我们希望在简单介绍后面,有一个产品ID和技术参数表,可以这样试下:At the end of the description, include every 7-character Product ID in the technical specification。

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.

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.

Use at most 50 words.

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

response = get_completion(prompt)
print(response)
Introducing our mid-century inspired office chair, perfect for both home and business settings. With a variety of shell colors and base finishes to choose from, this chair offers style and versatility. The 5-wheel plastic coated aluminum base and pneumatic chair adjustment ensure easy mobility and comfort. Made with high-quality materials, including a cast aluminum shell and HD36 foam seat, this chair is built to last. Available with or without armrests and suitable for any floor type. Product ID: SWC-100, SWC-110.

来个更复杂的prompt。我们希望得到一个参数表可能直接在网站展示(即以HTML的方式输出):第一列是参数维度;第二列是参数值。

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.

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. The table should have two columns. In the first column include the name of the dimension. In the second column include the measurements in inches only. Give the table the 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}'''
"""

response = get_completion(prompt)
print(response)
<div>
  <h2>Product Description</h2>
  <p>
    Introducing our mid-century inspired office chair, a perfect addition to any home or business setting. This chair is part of a beautiful family of office furniture that includes filing cabinets, desks, bookcases, and meeting tables. With several options for shell color and base finishes, you can customize this chair to fit your unique style. Choose between plastic back and front upholstery or full upholstery in a variety of fabric and leather optic options. The base finish options include stainless steel, matte black, gloss white, or chrome. You can also choose to have the chair with or without armrests.
  </p>
  <p>
    The construction of this chair is top-notch. It features a 5-wheel plastic coated aluminum base, ensuring stability and durability. The pneumatic chair adjust allows for easy raise and lower action, providing maximum comfort and convenience.
  </p>
  <p>
    The dimensions of this chair are as follows:
  </p>
  <table>
    <caption>Product Dimensions</caption>
    <tr>
      <th>Width</th>
      <td>53 cm | 20.87"</td>
    </tr>
    <tr>
      <th>Depth</th>
      <td>51 cm | 20.08"</td>
    </tr>
    <tr>
      <th>Height</th>
      <td>80 cm | 31.50"</td>
    </tr>
    <tr>
      <th>Seat Height</th>
      <td>44 cm | 17.32"</td>
    </tr>
    <tr>
      <th>Seat Depth</th>
      <td>41 cm | 16.14"</td>
    </tr>
  </table>
  <p>
    Additional options for this chair include soft or hard-floor caster options and two choices of seat foam densities: medium (1.8 1b/ft3) or high (2.8 1b/ft3). You can also choose between armless or 8 position PU armrests, providing even more customization possibilities.
  </p>
  <p>
    The materials used in the construction of this chair are of the highest quality. The shell and base glider are made of cast aluminum with a modified nylon PA6/PA66 coating, ensuring durability and longevity. The seat is made of HD36 foam, providing exceptional comfort.
  </p>
  <p>
    This chair is proudly made in Italy, a country known for its craftsmanship and attention to detail.
  </p>
  <p>
    Product IDs: SWC-100, SWC-110
  </p>
</div>

让我们来显示一下这段HTML。

from IPython.display import display, HTML
display(HTML(response))

Product Description

Introducing our mid-century inspired office chair, a perfect addition to any home or business setting. This chair is part of a beautiful family of office furniture that includes filing cabinets, desks, bookcases, and meeting tables. With several options for shell color and base finishes, you can customize this chair to fit your unique style. Choose between plastic back and front upholstery or full upholstery in a variety of fabric and leather optic options. The base finish options include stainless steel, matte black, gloss white, or chrome. You can also choose to have the chair with or without armrests.

The construction of this chair is top-notch. It features a 5-wheel plastic coated aluminum base, ensuring stability and durability. The pneumatic chair adjust allows for easy raise and lower action, providing maximum comfort and convenience.

The dimensions of this chair are as follows:

Product Dimensions
Width 53 cm | 20.87″
Depth 51 cm | 20.08″
Height 80 cm | 31.50″
Seat Height 44 cm | 17.32″
Seat Depth 41 cm | 16.14″

Additional options for this chair include soft or hard-floor caster options and two choices of seat foam densities: medium (1.8 1b/ft3) or high (2.8 1b/ft3). You can also choose between armless or 8 position PU armrests, providing even more customization possibilities.

The materials used in the construction of this chair are of the highest quality. The shell and base glider are made of cast aluminum with a modified nylon PA6/PA66 coating, ensuring durability and longevity. The seat is made of HD36 foam, providing exceptional comfort.

This chair is proudly made in Italy, a country known for its craftsmanship and attention to detail.

Product IDs: SWC-100, SWC-110

一个关于产品的介绍网页就生成了。

上面几个例子,我们能看到通过不断的实验和迭代prompt,模型能逐步给出满意的结果。每次迭代,我们都在不断尝试、分析模型给出的结果、明晰指令&给模型思考的时间、以及给模型学习一些示例等等(见下图)。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注